我正在为我的应用程序使用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));
}
}
请记住我的手机在同一个地方因此不应该移动。
答案 0 :(得分:1)
您是否尝试将minDisplacementDistance添加到LocationRequest?
private LocationRequest createLocationRequest() {
LocationRequest mLocationRequest = new LocationRequest();
mLocationRequest.setInterval(6000);
mLocationRequest.setFastestInterval(1000);
mLocationRequest.setSmallestDisplacement(200);
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
return mLocationRequest;
}
如果您将设置此参数(默认情况下它等于0),则每次GPS查找的新位置位于前方找到位置的200米范围内时,将不会调用LocationChanged侦听器。
现在你的onLocationChange监听器每6秒调用一次,最小距离等于0.由于GPS不是100%正确,你会得到很多不同的位置。将smallestDisplacement更改为更高版本后,将不会经常调用。
答案 1 :(得分:0)
有时由于精度设置较低,数据将从Wifi网络接收。检查Android手机上的设置/位置/使用无线网络复选框。
答案 2 :(得分:0)
从代码中删除这两行:
mLocationRequest.setInterval(6000);
mLocationRequest.setFastestInterval(1000);
答案 3 :(得分:0)
您是否尝试过mLocationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);
当我使用LocationRequest.PRIORITY_HIGH_ACCURACY
时,Gps始终在使用并始终在建筑物中进行更改。我改变了准确度,然后改变了位置。
你可以看看这个:
/*
Priority update interval Battery drain per hour (%) Accuracy
HIGH_ACCURACY 5 seconds 7.25% ~10 meters
BALANCED_POWER 20 seconds 0.6% ~40 meters
NO_POWER N/A small ~1 mile
*/
使用:
public static void createLocationRequest() {
mLocationRequest = new LocationRequest();
mLocationRequest.setInterval(UPDATE_INTERVAL);
mLocationRequest.setFastestInterval(FASTEST_INTERVAL);
mLocationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);
mLocationRequest.setSmallestDisplacement(DISPLACEMENT);
}
private static int UPDATE_INTERVAL = 20000; // 20 second
private static int FASTEST_INTERVAL = 10000; // 10 second
private static int DISPLACEMENT = 50; // minimum change to get location