如何在Google Maps Android API V2中获取位置及其更新

时间:2014-10-29 17:28:39

标签: android google-maps-android-api-2

我想在Google Maps Android API v2中启用位置。我想要的是:

  1. 以固定纬度/经度为中心加载地图。
  2. 开始收听位置更改,并在提供商(网络/ GPS)获取位置后,将地图置于此位置的中心并为其设置动画(动画仅运行一次)。同时,位置监听器在指定的时间后更新用户的位置,
  3. 存储此位置数据并使用它通过我的API获取附近的景点。
  4. 我对如何定期更新,选择时间间隔的数量感到困惑。现在,我已设置项目以显示地图并使用

    googleMap.setMyLocationEnabled(true);
    googleMap.setOnMyLocationChangeListener(myLocationChangeListener);
    
    private GoogleMap.OnMyLocationChangeListener myLocationChangeListener = new GoogleMap.OnMyLocationChangeListener() {
        @Override
        public void onMyLocationChange(Location location) {
            LatLng loc = new LatLng(location.getLatitude(), location.getLongitude());
            if(googleMap != null){
                if(!firstTime){
                    CameraPosition cameraPosition = new CameraPosition.Builder()
                    .target(loc).zoom(zoom).build();
                    googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition)); 
                    firstTime = true;
                }
    
                lat = location.getLatitude();
                lng = location.getLongitude();
                Toast.makeText(context, "Location is: " + String.valueOf(lat) + ", " 
                        + String.valueOf(lng), Toast.LENGTH_LONG).show();
            }
        }
    };
    

    在变量中设置lat / lng。 我的问题是:

    1. 这是正确的做法吗?或者可以做得更好吗?
    2. 位置更改侦听器经常更新自身。我看到几秒钟内就会制作吐司。如何规范?
    3. 当活动消失(暂停/停止)时,如何阻止侦听器获取位置? 谢谢!

3 个答案:

答案 0 :(得分:5)

问题2:

使用requestLocationUpdates(long minTime, float minDistance, Criteria criteria, PendingIntent intent)

前两个参数是:

参数 minTime:位置更新之间的最小时间间隔,以毫秒为单位 minDistance:位置更新之间的最小距离,以米为单位

           private static final long POLLING_FREQ = 1000 * 10;
           private static final float MIN_DISTANCE = 10.0f;

           // Reference to the LocationManager and LocationListener
           private LocationManager mLocationManager;
           private LocationListener mLocationListener;

           // Register for network location updates
            if (null != mLocationManager
                    .getProvider(LocationManager.NETWORK_PROVIDER)) {
                mLocationManager.requestLocationUpdates(
                        LocationManager.NETWORK_PROVIDER, POLLING_FREQ,
                        MIN_DISTANCE, mLocationListener);
            }

            // Register for GPS location updates
            if (null != mLocationManager
                    .getProvider(LocationManager.GPS_PROVIDER)) {
                mLocationManager.requestLocationUpdates(
                        LocationManager.GPS_PROVIDER, POLLING_FREQ,
                        MIN_DISTANCE, mLocationListener);
            }

您可以根据自己的需要进行调整。

问题3:

使用:removeUpdates(PendingIntent intent)删除指定待处理意图的所有位置更新。

问题1。

你的方法似乎很合理。如果你需要不断的位置更新,那么我可以建议其他的东西来减少电池消耗。

     mLocationListener = new LocationListener() {

    // Called back when location changes

    public void onLocationChanged(Location location) {


        // Get your location here
        // estimate

    }

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

    public void onProviderEnabled(String provider) {
        // NA
    }

    public void onProviderDisabled(String provider) {
        // NA
    }
};

答案 1 :(得分:0)

要降低更新的频率,请将参数传递给requestLocationUpdates。你可能会为第二个参数传递0,它会尽快更新。传递更适合您应用的内容(以毫秒为单位)。

总的来说,你的方法似乎很好。但是,在找到位置修复之前可能需要一些时间。为了适应这种情况,我通常会得到最后一个已知的位置(这可能是非常古老和无用的,但是#34;通常"足够接近)。这样用户即可获得即时更新:

String locationProvider = LocationManager.GPS_PROVIDER;
Location lastKnownLocation = locationManager.getLastKnownLocation(locationProvider);

要停止侦听更新,请使用onPause()方法:

locationManager.removeUpdates(locationListener);

答案 2 :(得分:0)

我已经在这里回答了类似的问题。How to get the current location in Google Maps Android API v2?

由于GoogleApiClient的弃用,使用OnMyLocationChangeListener

private LocationRequest  mLocationRequest;
private GoogleApiClient  mGoogleApiClient;
private LocationListener mLocationListener;

private void initGoogleApiClient(Context context)
{
    mGoogleApiClient = new GoogleApiClient.Builder(context).addApi(LocationServices.API).addConnectionCallbacks(new GoogleApiClient.ConnectionCallbacks()
    {
        @Override
        public void onConnected(Bundle bundle)
        {
            mLocationRequest = LocationRequest.create();
            mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
            mLocationRequest.setInterval(1000);

            setLocationListener();
        }

        @Override
        public void onConnectionSuspended(int i)
        {
            Log.i("LOG_TAG", "onConnectionSuspended");
        }
    }).build();

    if (mGoogleApiClient != null)
        mGoogleApiClient.connect();

}

private void setLocationListener()
{
    mLocationListener = new LocationListener()
    {
        @Override
        public void onLocationChanged(Location location)
        {
            String lat = String.valueOf(location.getLatitude());
            String lon = String.valueOf(location.getLongitude());
            Log.i("LOG_TAG", "Latitude = " + lat + " Longitude = " + lon);
        }
    };

    LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, mLocationListener);
}

private void removeLocationListener()
{
    LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, mLocationListener);
}

使用谷歌地图时,不要忘记调用mGoogleApiClient.connect();removeLocationListener();方法。