【问题标题】:Google doesn't show my locationGoogle 不显示我的位置
【发布时间】:2014-08-08 09:17:10
【问题描述】:

我想在地图上找到我正确位置的标记。在来自标记的信息中应该是地址。我想我确实混淆了一些东西或忘记了一些东西,因为使用我的代码地图没有显示我的位置..你能帮我吗?

@SuppressLint("NewApi")
public class MainActivity extends FragmentActivity implements LocationListener {
     GoogleMap map;
     MarkerOptions mp;
    public Address location;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

        lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, this);

        map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map))
         .getMap();

       }


     private class ReverseGeocodingTask extends AsyncTask<LatLng, Void, String>{
            Context mContext;

            public ReverseGeocodingTask(Context context){
                super();
                mContext = context;
            }

            // Finding address using reverse geocoding
            @Override
            protected String doInBackground(LatLng... params) {
                Geocoder geocoder = new Geocoder(mContext);
                double latitude = params[0].latitude;
                double longitude = params[0].longitude;

                List<Address> addresses = null;
                String addressText="";

                try {
                    addresses = geocoder.getFromLocation(latitude, longitude,1);
                } catch (IOException e) {
                    e.printStackTrace();
                }

                if(addresses != null && addresses.size() > 0 ){
                    Address address = addresses.get(0);

                    addressText = String.format("%s, %s, %s",
                    address.getMaxAddressLineIndex() > 0 ? address.getAddressLine(0) : "",
                    address.getLocality(),
                    address.getCountryName());
                }

                return addressText;

            }


    @Override
      public void onPostExecute(String addressText) {

        map.clear();


        mp.title(addressText);


        mp.position(new LatLng(location.getLatitude(), location.getLongitude()));

        map.addMarker(mp);


        map.animateCamera(CameraUpdateFactory.newLatLngZoom(
         new LatLng(location.getLatitude(), location.getLongitude()), 16));

       }



    }

    @Override
    public void onLocationChanged(Location location) {
           double lat = location.getLatitude();
           double lon = location.getLongitude();
           LatLng point = new LatLng(lat, lon);
           new ReverseGeocodingTask().execute(point);

        }

    }

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

    }

    @Override
    public void onProviderEnabled(String provider) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onProviderDisabled(String provider) {
        // TODO Auto-generated method stub

    }}

这是新的 logcat..

 08-08 11:51:19.521: E/dalvikvm(23602): Could not find class 'gpq', referenced from method gpr.a
    08-08 11:51:19.521: E/dalvikvm(23602): Could not find class 'gpq', referenced from method gpr.a
    08-08 11:51:19.521: E/dalvikvm(23602): Could not find class 'gpq', referenced from method gpr.a
    08-08 11:51:19.671: E/dalvikvm(23602): Could not find class 'com.google.android.gms.location.internal.ParcelableGeofence', referenced from method gls.a
    08-08 11:51:24.886: E/AndroidRuntime(23602): FATAL EXCEPTION: AsyncTask #1
    08-08 11:51:24.886: E/AndroidRuntime(23602): Process: ch.swisscom.manuel, PID: 23602
    08-08 11:51:24.886: E/AndroidRuntime(23602): java.lang.RuntimeException: An error occured while executing doInBackground()
    08-08 11:51:24.886: E/AndroidRuntime(23602):    at android.os.AsyncTask$3.done(AsyncTask.java:300)
    08-08 11:51:24.886: E/AndroidRuntime(23602):    at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
    08-08 11:51:24.886: E/AndroidRuntime(23602):    at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
    08-08 11:51:24.886: E/AndroidRuntime(23602):    at java.util.concurrent.FutureTask.run(FutureTask.java:242)
    08-08 11:51:24.886: E/AndroidRuntime(23602):    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
    08-08 11:51:24.886: E/AndroidRuntime(23602):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
    08-08 11:51:24.886: E/AndroidRuntime(23602):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
    08-08 11:51:24.886: E/AndroidRuntime(23602):    at java.lang.Thread.run(Thread.java:841)
    08-08 11:51:24.886: E/AndroidRuntime(23602): Caused by: java.lang.NullPointerException
    08-08 11:51:24.886: E/AndroidRuntime(23602):    at android.location.GeocoderParams.<init>(GeocoderParams.java:50)
    08-08 11:51:24.886: E/AndroidRuntime(23602):    at android.location.Geocoder.<init>(Geocoder.java:83)
    08-08 11:51:24.886: E/AndroidRuntime(23602):    at android.location.Geocoder.<init>(Geocoder.java:95)
    08-08 11:51:24.886: E/AndroidRuntime(23602):    at ch.swisscom.manuel.MainActivity$ReverseGeocodingTask.doInBackground(MainActivity.java:55)
    08-08 11:51:24.886: E/AndroidRuntime(23602):    at ch.swisscom.manuel.MainActivity$ReverseGeocodingTask.doInBackground(MainActivity.java:1)
    08-08 11:51:24.886: E/AndroidRuntime(23602):    at android.os.AsyncTask$2.call(AsyncTask.java:288)
    08-08 11:51:24.886: E/AndroidRuntime(23602):    at java.util.concurrent.FutureTask.run(FutureTask.java:237)
    08-08 11:51:24.886: E/AndroidRuntime(23602):    ... 4 more

【问题讨论】:

  • 你在哪里???需要在onLocationChnaged(.....)方法中添加标记并尝试
  • 你已经把它放在你的主要活动中了..
  • 我应该在哪里放置 onLocationChanged 方法?在另一个项目中,我这样做了 public void onLocationChanged(Location location) { map.clear(); MarkerOptions mp = new MarkerOptions(); mp.position(new LatLng(location.getLatitude(), location.getLongitude())); mp.title("我的职位"); map.addMarker(mp); ...

标签: android google-maps maps geocoding reverse-geocoding


【解决方案1】:

我在任何地方都没有看到对 ReverseGeocodingTask 类的函数调用

当 onLocationChanged 中的位置发生变化时调用它

改变onLocationChanged方法如下

 @Override
    public void onLocationChanged(Location location) {
        // TODO Auto-generated method stub
            double lat = location.getLatitude();
            double lon = location.getLongitude();
            LatLng point = new LatLng(lat, lon);
            new ReverseGeocodingTask().execute(point);

    }

希望这有效...干杯!!

【讨论】:

  • 尝试将 mcontext 更改为此
  • 如果没有后端服务,getfromlocation可能会返回null。大多数情况下可以通过重启解决
  • 有人有其他想法吗?
  • 那么您的代码中第 55 行的语句是什么?
  • 对不起,先生,我无法关注您。 Android 对我来说是一个新领域。第 55 行是 Geocoder geocoder = new Geocoder(this);分别Geocoder geocoder = new Geocoder(mContext);
【解决方案2】:

现在可以了。我想为有同样问题的人发布正确的代码。

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

    lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, this);


    map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map))
            .getMap();
}

private class ReverseGeocodingTask extends AsyncTask<LatLng, Void, String> {

    @Override
    protected String doInBackground(LatLng... params) {
        Geocoder geocoder = new Geocoder(getBaseContext());
        double latitude = params[0].latitude;
        double longitude = params[0].longitude;

        List<Address> addresses = null;

        try {
            addresses = geocoder.getFromLocation(latitude, longitude, 1);
        } catch (IOException e) {
            e.printStackTrace();
        }
        String addressText = null;
        if (addresses != null && addresses.size() > 0) {
            Address address = addresses.get(0);

             addressText= String.format(
                    "%s, %s, %s",
                    address.getMaxAddressLineIndex() > 0 ? address
                            .getAddressLine(0) : "", address.getLocality(),
                    address.getCountryName());
        }

        final MarkerOptions mp = new MarkerOptions();

        final LatLng userLocation = new LatLng(latitude, longitude);

        mp.position(userLocation);
        mp.title(addressText);
        runOnUiThread(new Runnable() {
            public void run() {
                Marker marker = map.addMarker(mp);
                map.animateCamera(CameraUpdateFactory.newLatLngZoom(userLocation,
                        16));       
                marker.showInfoWindow();
            }
        });

        return addressText;
    }

}

@Override
public void onLocationChanged(Location location) {

    LatLng userLocation = new LatLng(location.getLatitude(),
            location.getLongitude());
    ReverseGeocodingTask rgt = new ReverseGeocodingTask();

    rgt.execute(userLocation);

}

【讨论】:

    猜你喜欢
    • 2015-07-04
    • 1970-01-01
    • 2022-11-15
    • 2012-02-26
    • 2016-01-03
    • 2016-09-08
    • 1970-01-01
    • 2016-08-05
    • 1970-01-01
    相关资源
    最近更新 更多