【问题标题】:faster updating geolocation on map更快地更新地图上的地理位置
【发布时间】:2018-04-28 18:19:41
【问题描述】:

我正在使用地理定位编写 android 导航应用程序,但我在频率更新位置方面遇到问题。

picture

大约 20 秒后,我在 log.d() 中得到响应。恐怕它太慢了,例如当我在开车时使用它时。假设标记应该平滑移动。

NavigationActivity 类:

package com.nowinski.kamil.drivertool;

import android.Manifest;
import android.content.pm.PackageManager;
import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Handler;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

import com.google.android.gms.common.api.Status;
import com.google.android.gms.location.places.Place;
import com.google.android.gms.location.places.ui.PlaceAutocompleteFragment;
import com.google.android.gms.location.places.ui.PlaceSelectionListener;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
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 java.io.IOException;
import java.util.List;
import java.util.zip.ZipEntry;

import utils.LatLngInterpolator;
import utils.MarkerAnimation;

public class NavigationActivity extends FragmentActivity implements 
OnMapReadyCallback, LocationListener {

private GoogleMap mMap;
private LocationManager locationManager;
private Location location;
private double latitude;
private double longitude;
private PlaceAutocompleteFragment placeAutocompleteFragment;
private Marker marker;
private Marker markerCurrentLocation = null;
private final float ZOOM = 12.2f;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_navigation);
    // Obtain the SupportMapFragment and get notified when the map is ready to be used.
    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);

    //initialize placeAutocompleteFragment and set on listener
    placeAutocompleteFragment = (PlaceAutocompleteFragment) getFragmentManager().findFragmentById(R.id.place_autocomplete_fragment);
    placeAutocompleteFragment.setOnPlaceSelectedListener(new PlaceSelectionListener() {
        @Override
        public void onPlaceSelected(Place place) {
            final LatLng latLngLoc = place.getLatLng();

            if(marker!=null){
                marker.remove();
            }
            marker = mMap.addMarker(new MarkerOptions().position(latLngLoc).title(place.getName().toString()));
            mMap.animateCamera(CameraUpdateFactory.zoomTo(12), 2000, null);
        }

        @Override
        public void onError(Status status) {
            Toast.makeText(NavigationActivity.this, ""+status.toString(), Toast.LENGTH_SHORT).show();
        }
    });

    //check permissions
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        // TODO: Consider calling
        //    ActivityCompat#requestPermissions
        // here to request the missing permissions, and then overriding
        //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
        //                                          int[] grantResults)
        // to handle the case where the user grants the permission. See the documentation
        // for ActivityCompat#requestPermissions for more details.
        return;
    }

    //initialize locationManager to get the location system service
    locationManager = (LocationManager) this.getSystemService(LOCATION_SERVICE);
    locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1000, 0, this);
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 0, this);
    location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

    if(location != null){
        latitude = location.getLatitude();
        longitude = location.getLongitude();
    }
}

@Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;
}

private void updateMarkerPosition(Location newLocation){
    //test
    Log.d("locationChanged",  Double.toString(newLocation.getLatitude())+" "+Double.toString(newLocation.getLongitude()));


    LatLng newLatLng = new LatLng(newLocation.getLatitude(), newLocation.getLongitude());

    //if marker used first time addMarker to map and move camera
    if(markerCurrentLocation == null){
        markerCurrentLocation = mMap.addMarker(new MarkerOptions().position(newLatLng));
        mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(newLatLng, ZOOM));
    } else {
        MarkerAnimation.animateMarkerToICS(markerCurrentLocation, newLatLng, new LatLngInterpolator.Spherical());
        mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(newLatLng, ZOOM));
    }
}

@Override
public void onLocationChanged(Location location) {
    latitude = location.getLatitude();
    longitude = location.getLongitude();
    this.location = location;
    updateMarkerPosition(location);
}

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

@Override
public void onProviderEnabled(String provider) {}

@Override
public void onProviderDisabled(String provider) {}

//activity life cycle
@Override
protected void onDestroy(){
    super.onDestroy();
    //stop GPS
    locationManager.removeUpdates(this);
}
}

【问题讨论】:

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


    【解决方案1】:

    在用户走路、开车或跑步时检测到活动。 Google doc.

    但我建议您使用这些经过良好测试和维护的库来实现这一点,例如

    1.https://github.com/akexorcist/Android-GoogleDirectionLibrary

    2.https://github.com/jd-alexander/Google-Directions-Android

    它为您提供了许多库函数供您使用。并且您不需要对位置监听的所有内容进行 RnD。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-28
      • 1970-01-01
      • 1970-01-01
      • 2017-07-21
      • 1970-01-01
      • 2021-10-20
      相关资源
      最近更新 更多