【发布时间】:2016-06-04 10:43:31
【问题描述】:
我正在为我的应用程序使用 Google Maps API。即使设备在同一个地方,我的 GPS 位置也在变化,据说该位置每 6 秒更新一次,因此它会不断更新,但每次更新它也会移动一点,有时移动很多。我的代码如下。我还会添加截图。
package b.a.location_tracker;
import android.graphics.Color;
import android.location.Location;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import android.util.Log;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.location.LocationListener;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.location.LocationServices;
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.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
import com.google.android.gms.maps.model.Polyline;
import com.google.android.gms.maps.model.PolylineOptions;
public class Map extends FragmentActivity implements OnMapReadyCallback, LocationListener, GoogleApiClient.ConnectionCallbacks {
private GoogleMap mMap;
Marker m;
private GoogleApiClient mGoogleApiClient;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_map);
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
this.buildGoogleApiClient();
mGoogleApiClient.connect();
}
private synchronized void buildGoogleApiClient() {
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this).addApi(LocationServices.API)
.build();
}
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
LatLng sydney = new LatLng(28.6139, 77.2090);
m=mMap.addMarker(new MarkerOptions().position(sydney).title("Delhi City"));
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(sydney, 20));
}
@Override
public void onConnected(Bundle bundle) {
LocationRequest mLocationRequest = createLocationRequest();
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, (LocationListener) this);
}
@Override
public void onConnectionSuspended(int i) {
Log.d("TAG", "Connection to Google API suspended");
}
private LocationRequest createLocationRequest() {
LocationRequest mLocationRequest = new LocationRequest();
mLocationRequest.setInterval(6000);
mLocationRequest.setFastestInterval(1000);
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
return mLocationRequest;
}
@Override
public void onLocationChanged(Location location) {
LatLng sydney = new LatLng(location.getLatitude(), location.getLongitude());
LatLng a=m.getPosition();
m.remove();
m=mMap.addMarker(new MarkerOptions().position(sydney).title("Your Location"));
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(sydney, mMap.getCameraPosition().zoom));
Polyline line = mMap.addPolyline(new PolylineOptions().add(a,sydney).width(5).color(Color.RED));
}
}
请记住我的手机在同一个地方,因此它不应该移动。
【问题讨论】:
标签: android google-maps gps locationlistener