这是我连接locationClient后调用的onConnected()方法。此方法称为ok,因此GPS正常连接。但是,getLastLocation()返回null,这可能是由于尝试过早获取位置而导致的,并且根本没有调用locationListener方法。 它是在三星Galaxy S3上运行的。当我打开谷歌地图应用程序时,它会立即修复我的位置,因此GPS必须正常。
@Override
public void onConnected(Bundle arg0)
{
Toast.makeText(context, "GPS connected", Toast.LENGTH_SHORT).show();
connector = new Connector();
connector.execute("clues", null, null);
listener = new LocationListener()
{
@Override
public void onLocationChanged(Location location)
{
Log.d("location", "listener activated");
try
{
coords = locationCoords(location);
if(coords[0] != 0)
{
meMarker = map.addMarker(new MarkerOptions()
.position(new LatLng(coords[0], coords[1]))
.title("Me")
.snippet("Your position")
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE)));
map.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(coords[0], coords[1]), (float) 19));
}
Toast.makeText(context, coords[0] + ", " + coords[1], Toast.LENGTH_LONG).show();
}
catch(NullPointerException e)
{
Log.d("nulls", "null");
}
}
};
mLocationClient.requestLocationUpdates(mLocationRequest, listener);
try
{
coords = locationCoords(new Location(mLocationClient.getLastLocation()));
map.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(coords[0], coords[1]), (float) 19));
}
catch(NullPointerException e)
{
Toast.makeText(context,"location null", Toast.LENGTH_LONG).show();
}
//Toast.makeText(context, coords[0] + ", " + coords[1], Toast.LENGTH_LONG).show();
Log.d("location", "location updates initiated");
}
定义请求参数:
public dbConnector(Context con, Activity acti, GoogleMap map)
{
context = con;
activity = acti;
mLocationRequest = LocationRequest.create();
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
setGPSUpdates(10000, 5000);
mLocationRequest.setInterval(updateRate);
mLocationRequest.setFastestInterval(fastestUpdate);
mLocationRequest.setSmallestDisplacement(1);
Log.d("location", "location request constructed");
this.map = map;
}
public void setGPSUpdates(int update, int fastupdate)
{
updateRate = update;
fastestUpdate = fastupdate;
}
答案 0 :(得分:0)
Google地图应用程序同时使用GPS和网络定位。
使用GPS,第一次修复有一段时间到达是正常的。此外,它在外面运行良好,内部非常糟糕。 (另外,确保已启用GPS)
此代码适用于我:
public class MainActivity extends Activity implements LocationListener {
private LocationManager locationManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
if (!locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
// GPS is disabled
}
}
@Override
protected void onStart() {
super.onStart();
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0,
0, this);
}
@Override
protected void onStop() {
locationManager.removeUpdates(this);
super.onStop();
}
@Override
public void onLocationChanged(Location location) {
// do something with the fix
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
}
}