1

我正在使用 Android Query 来检测用户的位置。

出于某种原因,当我尝试检索我的纬度和经度时,它会显示我之前的位置,它在几百米外,就好像它被缓存了一样。

这里可能是什么问题?

public class LocationHelper {
    private Context mContext;
    private double latitude;
    private double longitude;

    public LocationHelper(Context mContext) {
        this.mContext = mContext;
        location_ajax();
    }

    public double getLatitude() {
        return latitude;
    }

    public double getLongitude() {
        return longitude;
    }

    private void location_ajax() {
        // Get the location
        LocationAjaxCallback cb = new LocationAjaxCallback();
        cb.weakHandler(this, "locationCb");
        cb.async(mContext);
    }

    public void locationCb(String url, Location loc, AjaxStatus status) {
        if (loc != null) {
            //Toast.makeText(mContext, loc.toString(), Toast.LENGTH_LONG).show();
            this.latitude = loc.getLatitude();
            this.longitude = loc.getLongitude();            
        } else {
            Toast.makeText(mContext, mContext.getString(R.string.msg_could_not_retrieve_location), Toast.LENGTH_LONG).show();
        }
    }   
}

日志猫:

03-19 23:03:17.146: W/AQuery(29954): reporting:java.lang.reflect.InvocationTargetException
03-19 23:03:17.146: W/AQuery(29954):    at java.lang.reflect.Method.invokeNative(Native Method)
03-19 23:03:17.146: W/AQuery(29954):    at java.lang.reflect.Method.invoke(Method.java:511)
03-19 23:03:17.146: W/AQuery(29954):    at com.androidquery.util.AQUtility.invokeMethod(AQUtility.java:206)
03-19 23:03:17.146: W/AQuery(29954):    at com.androidquery.util.AQUtility.invokeHandler(AQUtility.java:183)
03-19 23:03:17.146: W/AQuery(29954):    at com.androidquery.callback.AbstractAjaxCallback.callback(AbstractAjaxCallback.java:577)
03-19 23:03:17.146: W/AQuery(29954):    at com.androidquery.callback.LocationAjaxCallback.failure(LocationAjaxCallback.java:192)
03-19 23:03:17.146: W/AQuery(29954):    at com.androidquery.callback.LocationAjaxCallback.access$3(LocationAjaxCallback.java:183)
03-19 23:03:17.146: W/AQuery(29954):    at com.androidquery.callback.LocationAjaxCallback$Listener.run(LocationAjaxCallback.java:308)
03-19 23:03:17.146: W/AQuery(29954):    at java.util.Timer$TimerImpl.run(Timer.java:284)
03-19 23:03:17.146: W/AQuery(29954): Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
03-19 23:03:17.146: W/AQuery(29954):    at android.os.Handler.<init>(Handler.java:121)
03-19 23:03:17.146: W/AQuery(29954):    at android.widget.Toast$TN.<init>(Toast.java:361)
03-19 23:03:17.146: W/AQuery(29954):    at android.widget.Toast.<init>(Toast.java:97)
03-19 23:03:17.146: W/AQuery(29954):    at android.widget.Toast.makeText(Toast.java:254)
03-19 23:03:17.146: W/AQuery(29954):    at uk.ac.gla.project.helpers.LocationHelper.locationCb(LocationHelper.java:46)
03-19 23:03:17.146: W/AQuery(29954):    ... 9 more
03-19 23:03:20.001: W/AQuery(29954): reporting:java.lang.reflect.InvocationTargetException
03-19 23:03:20.001: W/AQuery(29954):    at java.lang.reflect.Method.invokeNative(Native Method)
03-19 23:03:20.001: W/AQuery(29954):    at java.lang.reflect.Method.invoke(Method.java:511)
03-19 23:03:20.001: W/AQuery(29954):    at com.androidquery.util.AQUtility.invokeMethod(AQUtility.java:206)
03-19 23:03:20.001: W/AQuery(29954):    at com.androidquery.util.AQUtility.invokeHandler(AQUtility.java:183)
03-19 23:03:20.001: W/AQuery(29954):    at com.androidquery.callback.AbstractAjaxCallback.callback(AbstractAjaxCallback.java:577)
03-19 23:03:20.001: W/AQuery(29954):    at com.androidquery.callback.LocationAjaxCallback.failure(LocationAjaxCallback.java:192)
03-19 23:03:20.001: W/AQuery(29954):    at com.androidquery.callback.LocationAjaxCallback.access$3(LocationAjaxCallback.java:183)
03-19 23:03:20.001: W/AQuery(29954):    at com.androidquery.callback.LocationAjaxCallback$Listener.run(LocationAjaxCallback.java:308)
03-19 23:03:20.001: W/AQuery(29954):    at java.util.Timer$TimerImpl.run(Timer.java:284)
03-19 23:03:20.001: W/AQuery(29954): Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
03-19 23:03:20.001: W/AQuery(29954):    at android.os.Handler.<init>(Handler.java:121)
03-19 23:03:20.001: W/AQuery(29954):    at android.widget.Toast$TN.<init>(Toast.java:361)
03-19 23:03:20.001: W/AQuery(29954):    at android.widget.Toast.<init>(Toast.java:97)
03-19 23:03:20.001: W/AQuery(29954):    at android.widget.Toast.makeText(Toast.java:254)
03-19 23:03:20.001: W/AQuery(29954):    at uk.ac.gla.project.helpers.LocationHelper.locationCb(LocationHelper.java:46)
03-19 23:03:20.001: W/AQuery(29954):    ... 9 more
03-19 23:03:20.671: W/AQuery(29954): reporting:java.lang.reflect.InvocationTargetException
03-19 23:03:20.671: W/AQuery(29954):    at java.lang.reflect.Method.invokeNative(Native Method)
03-19 23:03:20.671: W/AQuery(29954):    at java.lang.reflect.Method.invoke(Method.java:511)
03-19 23:03:20.671: W/AQuery(29954):    at com.androidquery.util.AQUtility.invokeMethod(AQUtility.java:206)
03-19 23:03:20.671: W/AQuery(29954):    at com.androidquery.util.AQUtility.invokeHandler(AQUtility.java:183)
03-19 23:03:20.671: W/AQuery(29954):    at com.androidquery.callback.AbstractAjaxCallback.callback(AbstractAjaxCallback.java:577)
03-19 23:03:20.671: W/AQuery(29954):    at com.androidquery.callback.LocationAjaxCallback.failure(LocationAjaxCallback.java:192)
03-19 23:03:20.671: W/AQuery(29954):    at com.androidquery.callback.LocationAjaxCallback.access$3(LocationAjaxCallback.java:183)
03-19 23:03:20.671: W/AQuery(29954):    at com.androidquery.callback.LocationAjaxCallback$Listener.run(LocationAjaxCallback.java:308)
03-19 23:03:20.671: W/AQuery(29954):    at java.util.Timer$TimerImpl.run(Timer.java:284)
03-19 23:03:20.671: W/AQuery(29954): Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
03-19 23:03:20.671: W/AQuery(29954):    at android.os.Handler.<init>(Handler.java:121)
03-19 23:03:20.671: W/AQuery(29954):    at android.widget.Toast$TN.<init>(Toast.java:361)
03-19 23:03:20.671: W/AQuery(29954):    at android.widget.Toast.<init>(Toast.java:97)
03-19 23:03:20.671: W/AQuery(29954):    at android.widget.Toast.makeText(Toast.java:254)
03-19 23:03:20.671: W/AQuery(29954):    at uk.ac.gla.project.helpers.LocationHelper.locationCb(LocationHelper.java:46)
03-19 23:03:20.671: W/AQuery(29954):    ... 9 more
03-19 23:03:20.906: W/AQuery(29954): reporting:java.lang.reflect.InvocationTargetException
03-19 23:03:20.906: W/AQuery(29954):    at java.lang.reflect.Method.invokeNative(Native Method)
03-19 23:03:20.906: W/AQuery(29954):    at java.lang.reflect.Method.invoke(Method.java:511)
03-19 23:03:20.906: W/AQuery(29954):    at com.androidquery.util.AQUtility.invokeMethod(AQUtility.java:206)
03-19 23:03:20.906: W/AQuery(29954):    at com.androidquery.util.AQUtility.invokeHandler(AQUtility.java:183)
03-19 23:03:20.906: W/AQuery(29954):    at com.androidquery.callback.AbstractAjaxCallback.callback(AbstractAjaxCallback.java:577)
03-19 23:03:20.906: W/AQuery(29954):    at com.androidquery.callback.LocationAjaxCallback.failure(LocationAjaxCallback.java:192)
03-19 23:03:20.906: W/AQuery(29954):    at com.androidquery.callback.LocationAjaxCallback.access$3(LocationAjaxCallback.java:183)
03-19 23:03:20.906: W/AQuery(29954):    at com.androidquery.callback.LocationAjaxCallback$Listener.run(LocationAjaxCallback.java:308)
03-19 23:03:20.906: W/AQuery(29954):    at java.util.Timer$TimerImpl.run(Timer.java:284)
03-19 23:03:20.906: W/AQuery(29954): Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
03-19 23:03:20.906: W/AQuery(29954):    at android.os.Handler.<init>(Handler.java:121)
03-19 23:03:20.906: W/AQuery(29954):    at android.widget.Toast$TN.<init>(Toast.java:361)
03-19 23:03:20.906: W/AQuery(29954):    at android.widget.Toast.<init>(Toast.java:97)
03-19 23:03:20.906: W/AQuery(29954):    at android.widget.Toast.makeText(Toast.java:254)
03-19 23:03:20.906: W/AQuery(29954):    at uk.ac.gla.project.helpers.LocationHelper.locationCb(LocationHelper.java:46)
03-19 23:03:20.906: W/AQuery(29954):    ... 9 more
03-19 23:03:21.061: W/AQuery(29954): reporting:java.lang.reflect.InvocationTargetException
03-19 23:03:21.061: W/AQuery(29954):    at java.lang.reflect.Method.invokeNative(Native Method)
03-19 23:03:21.061: W/AQuery(29954):    at java.lang.reflect.Method.invoke(Method.java:511)
03-19 23:03:21.061: W/AQuery(29954):    at com.androidquery.util.AQUtility.invokeMethod(AQUtility.java:206)
03-19 23:03:21.061: W/AQuery(29954):    at com.androidquery.util.AQUtility.invokeHandler(AQUtility.java:183)
03-19 23:03:21.061: W/AQuery(29954):    at com.androidquery.callback.AbstractAjaxCallback.callback(AbstractAjaxCallback.java:577)
03-19 23:03:21.061: W/AQuery(29954):    at com.androidquery.callback.LocationAjaxCallback.failure(LocationAjaxCallback.java:192)
03-19 23:03:21.061: W/AQuery(29954):    at com.androidquery.callback.LocationAjaxCallback.access$3(LocationAjaxCallback.java:183)
03-19 23:03:21.061: W/AQuery(29954):    at com.androidquery.callback.LocationAjaxCallback$Listener.run(LocationAjaxCallback.java:308)
03-19 23:03:21.061: W/AQuery(29954):    at java.util.Timer$TimerImpl.run(Timer.java:284)
03-19 23:03:21.061: W/AQuery(29954): Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
03-19 23:03:21.061: W/AQuery(29954):    at android.os.Handler.<init>(Handler.java:121)
03-19 23:03:21.061: W/AQuery(29954):    at android.widget.Toast$TN.<init>(Toast.java:361)
03-19 23:03:21.061: W/AQuery(29954):    at android.widget.Toast.<init>(Toast.java:97)
03-19 23:03:21.061: W/AQuery(29954):    at android.widget.Toast.makeText(Toast.java:254)
03-19 23:03:21.061: W/AQuery(29954):    at uk.ac.gla.project.helpers.LocationHelper.locationCb(LocationHelper.java:46)
03-19 23:03:21.061: W/AQuery(29954):    ... 9 more
03-19 23:03:21.486: W/AQuery(29954): reporting:java.lang.reflect.InvocationTargetException
03-19 23:03:21.486: W/AQuery(29954):    at java.lang.reflect.Method.invokeNative(Native Method)
03-19 23:03:21.486: W/AQuery(29954):    at java.lang.reflect.Method.invoke(Method.java:511)
03-19 23:03:21.486: W/AQuery(29954):    at com.androidquery.util.AQUtility.invokeMethod(AQUtility.java:206)
03-19 23:03:21.486: W/AQuery(29954):    at com.androidquery.util.AQUtility.invokeHandler(AQUtility.java:183)
03-19 23:03:21.486: W/AQuery(29954):    at com.androidquery.callback.AbstractAjaxCallback.callback(AbstractAjaxCallback.java:577)
03-19 23:03:21.486: W/AQuery(29954):    at com.androidquery.callback.LocationAjaxCallback.failure(LocationAjaxCallback.java:192)
03-19 23:03:21.486: W/AQuery(29954):    at com.androidquery.callback.LocationAjaxCallback.access$3(LocationAjaxCallback.java:183)
03-19 23:03:21.486: W/AQuery(29954):    at com.androidquery.callback.LocationAjaxCallback$Listener.run(LocationAjaxCallback.java:308)
03-19 23:03:21.486: W/AQuery(29954):    at java.util.Timer$TimerImpl.run(Timer.java:284)
03-19 23:03:21.486: W/AQuery(29954): Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
03-19 23:03:21.486: W/AQuery(29954):    at android.os.Handler.<init>(Handler.java:121)
03-19 23:03:21.486: W/AQuery(29954):    at android.widget.Toast$TN.<init>(Toast.java:361)
03-19 23:03:21.486: W/AQuery(29954):    at android.widget.Toast.<init>(Toast.java:97)
03-19 23:03:21.486: W/AQuery(29954):    at android.widget.Toast.makeText(Toast.java:254)
03-19 23:03:21.486: W/AQuery(29954):    at uk.ac.gla.project.helpers.LocationHelper.locationCb(LocationHelper.java:46)

有没有可靠的替代品?

4

7 回答 7

3

为什么这么复杂?使用谷歌播放服务。它们是免费的,并且使用复杂的位置策略

于 2014-03-25T12:41:31.127 回答
1
@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Intent intent=new Intent("android.location.GPS_ENABLED_CHANGE");
        intent.putExtra("enabled", true);
        sendBroadcast(intent);  
        LocationManager mlocManager = (LocationManager)this.getSystemService(Context.LOCATION_SERVICE);
        LocationListener mlocListener = new MyLocationListener();
         mlocManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 0, 0, mlocListener);
    }

  /* Class My Location Listener */
        public class MyLocationListener implements LocationListener
        {

          @Override
          public void onLocationChanged(Location loc)
          {
    try{
           lat=loc.getLatitude();
            longs=loc.getLongitude();
            getAddress(lat, longs);
            String Text = "My current location is: " +
            "Latitud = " + loc.getLatitude() +
            "Longitud = " + loc.getLongitude();
    }catch (NullPointerException e) {

    }
           // Toast.makeText( getActivity().getApplicationContext(), Text, Toast.LENGTH_SHORT).show();
          }

          @Override
          public void onProviderDisabled(String provider)
          {
            Toast.makeText(getApplicationContext(), "Gps Disabled", Toast.LENGTH_SHORT ).show();
          }

          @Override
          public void onProviderEnabled(String provider)
          {
            Toast.makeText(getApplicationContext(), "Gps Enabled", Toast.LENGTH_SHORT).show();
          }

          @Override
          public void onStatusChanged(String provider, int status, Bundle extras)
          {

          }
        }
于 2014-03-29T09:56:12.790 回答
1

根据您的 logCat,当您的 LocationAjaxCallback$Listener 触发并调用此方法时,您有一个 RuntimeException 。

locationCb(String url, Location loc, AjaxStatus status)

由于这无法更新您的变量

private double latitude;
private double longitude;

您将被退回之前存储在其中的任何内容。(您的旧位置)

您需要修复导致此错误的任何原因

at com.androidquery.callback.LocationAjaxCallback$Listener.run(LocationAjaxCallback.java:308)
at java.util.Timer$TimerImpl.run(Timer.java:284)
Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()

之后,您的更新方法将正确触发并更新 lat 和 long

于 2014-03-22T18:00:41.403 回答
1

cb.uiCallback(true);之前添加cb.async(mContext);

为什么

我可以告诉您LocationHelper从没有 a 的后台线程创建实例,Looper因此您不能使用它来获取异步回调。

基本上,如果你想打电话cb.async(mContext);而不打电话cb.uiCallback(true);,你必须从 UI Thread*

* 或任何其他具有Looper

于 2014-03-25T16:32:30.287 回答
1
Try this one.

package com.example.tracker;

import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;

import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;

import android.content.Context;
import android.content.Intent;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.provider.Settings;
import android.support.v4.app.FragmentActivity;
import android.widget.Toast;

public class MainActivity extends FragmentActivity implements LocationListener {
    private GoogleMap map;
    private static final LatLng ROMA = new LatLng(42.093230818037,
            11.7971813678741);
    private LocationManager locationManager;
    private String provider;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        map = ((SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map)).getMap();

        LocationManager service = (LocationManager) getSystemService(LOCATION_SERVICE);
        boolean enabledGPS = service
                .isProviderEnabled(LocationManager.GPS_PROVIDER);
        boolean enabledWiFi = service
                .isProviderEnabled(LocationManager.NETWORK_PROVIDER);

        // Check if enabled and if not send user to the GSP settings
        // Better solution would be to display a dialog and suggesting to
        // go to the settings
        if (!enabledGPS) {
            Toast.makeText(this, "GPS signal not found", Toast.LENGTH_LONG)
                    .show();
            Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
            startActivity(intent);
        }

        locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        // Define the criteria how to select the locatioin provider -> use
        // default
        Criteria criteria = new Criteria();
        provider = locationManager.getBestProvider(criteria, false);
        Location location = locationManager.getLastKnownLocation(provider);

        // Initialize the location fields
        if (location != null) {
            Toast.makeText(this, "Selected Provider " + provider,
                    Toast.LENGTH_SHORT).show();
            onLocationChanged(location);
        } else {

            // do something
        }

    }

    /* Request updates at startup */
    @Override
    protected void onResume() {
        super.onResume();
        locationManager.requestLocationUpdates(provider, 400, 1, this);
    }

    /* Remove the locationlistener updates when Activity is paused */
    @Override
    protected void onPause() {
        super.onPause();
        locationManager.removeUpdates(this);
    }

    @Override
    public void onLocationChanged(Location location) {
        double lat = location.getLatitude();
        double lng = location.getLongitude();
        Toast.makeText(this, "Location " + lat + "," + lng, Toast.LENGTH_LONG)
                .show();
        LatLng coordinate = new LatLng(lat, lng);
        Toast.makeText(this,
                "Location " + coordinate.latitude + "," + coordinate.longitude,
                Toast.LENGTH_LONG).show();
        Marker startPerc = map.addMarker(new MarkerOptions()
                .position(coordinate)
                .title("Start")
                .snippet("Inizio del percorso")
                .icon(BitmapDescriptorFactory
                        .fromResource(R.drawable.ic_launcher)));
    }

    @Override
    public void onProviderDisabled(String provider) {
        Toast.makeText(this, "Enabled new provider " + provider,
                Toast.LENGTH_SHORT).show();

    }

    @Override
    public void onProviderEnabled(String provider) {
        Toast.makeText(this, "Disabled provider " + provider,
                Toast.LENGTH_SHORT).show();

    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
        // TODO Auto-generated method stub

    }

}
于 2014-03-29T09:10:53.633 回答
1

为什么不是经典的:

locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);

// Define a listener that responds to location updates

locationListener = new LocationListener() {

        public void onLocationChanged(Location location) {

                // Called when a new location is found by the network location provider.

                longitude = String.valueOf(location.getLongitude());

                latitude = String.valueOf(location.getLatitude());

                Log.d(TAG, "changed Loc : " + longitude + ":" + latitude);

        }

        public void onStatusChanged(String provider, int status, Bundle extras) {

        }

        public void onProviderEnabled(String provider) {

        }

        public void onProviderDisabled(String provider) {

        }

};

// getting GPS status

isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);

// check if GPS enabled

if (isGPSEnabled) {

        Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);

        if (location != null) {

                longitude = String.valueOf(location.getLongitude());

                latitude = String.valueOf(location.getLatitude());

                locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);

        } else {

                location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

                if (location != null) {

                        longitude = String.valueOf(location.getLongitude());

                        latitude = String.valueOf(location.getLatitude());

                        locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);

                } else {

                        longitude = "0.00";

                        latitude = "0.00";

                }

        }

}

取自这里http://androidadvance.com/android_snippets.php

于 2014-03-25T09:43:08.673 回答
0

下一个错误表明您已经启动了一个将接收异步消息的处理程序。为此,您需要在主线程上,或者您可以启动一个 looper:

无法在未调用 Looper.prepare() 的线程内创建处理程序

我认为关键是您正在创建一个 AJAX 处理程序,它似乎期望来自 GeoLoc API 的一些异步消息。

您可以尝试在 Activity 中实例化此处理程序或尝试启动 Looper:

Looper.prepare();
LocationAjaxCallback cb = new LocationAjaxCallback();
Looper.loop();

如果你启动一个弯针要小心,所以当你完成时需要关闭它。认为 Looper 就像在其当前线程中运行的侦听器(因此它不会在调用线程中运行)并且它有一个内部队列来存储新消息。每当有新消息到达时,队列都会更新,并且 Looper 线程会处理此事件(当它完成当前事件时会接收下一个事件或阻塞,直到有新消息到达)。

出于这个原因,您应该启动 looper(创建一个将处理消息的新线程)并在完成时关闭(释放该线程 + 消息队列)。

于 2014-03-29T12:15:53.833 回答