更快地更新地图上的地理位置

时间:2018-04-28 18:19:42

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

即时通讯使用地理定位编写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);
}
}

1 个答案:

答案 0 :(得分:0)

当用户步行,驾驶或跑步时,会检测到活动。 Google doc

但我建议您使用这些经过良好测试和维护的库(如

)来实现这一点

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

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

为您提供许多库函数。并且你不需要在所有位置监听上使用RnD。