1

下面是我正在使用的位置监听器和调用它的方法。当我运行此地图时,地图会不断放大和缩小。我目前在我的活动中从 onCreate 调用它。我在这里做错了什么?

private void showCurrentLocationOnMap(){
    LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
    LocationListener ll = new Mylocationlistener();
    boolean isGPS = lm.isProviderEnabled(LocationManager.GPS_PROVIDER);

    if (!isGPS){
        Intent intent = new Intent("android.location.GPS_ENABLED_CHANGE");
        intent.putExtra("enabled", true);
        sendBroadcast(intent);
    }

    lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, ll);




}


/**
 *Mylocationlistener class will give the current GPS location 
 *with the help of Location Listener interface 
 */
private class Mylocationlistener implements LocationListener {

    @Override
    public void onLocationChanged(Location location) {

        if (location != null) {
            // ---Get current location latitude, longitude, altitude & speed ---

            Log.d("LOCATION CHANGED", location.getLatitude() + "");
            Log.d("LOCATION CHANGED", location.getLongitude() + "");
            currentLocation = new LatLng(location.getLatitude(), location.getLongitude());
            HAMBURG = new LatLng(location.getLatitude(), location.getLongitude());
            //This only shows the initial marker
                  @SuppressWarnings("unused")
      Marker hamburg = map.addMarker(new MarkerOptions().position(currentLocation).title("Current Location"));
            // Move the camera instantly to hamburg with a zoom of 15.
            map.moveCamera(CameraUpdateFactory.newLatLngZoom(HAMBURG, 15));
            // Zoom in, animating the camera.
            map.animateCamera(CameraUpdateFactory.zoomTo(12), 2000, null);              


        }
    }
4

1 回答 1

0

我发现每隔 0 秒我就根据 requestLocationUpdates 方法请求位置更新,这只是我的一个错字。和往常一样,这是一件小事让我很生气。所以而不是:

 lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, ll);

我应该有:

 lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 10000, 10, ll);

10000 相当于十秒。一旦我改变了这个,我就能够成功地运行应用程序,并且更新正确更新。

于 2013-03-21T14:28:37.903 回答