我的代码有什么问题。我想用gps获取当前位置,但每次lat和lng都是0.0。
locationManager=(LocationManager)this.getSystemService(Context.LOCATION_SERVICE);
locationListener=new LocationListener() {
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onLocationChanged(Location location) {
latitude=location.getLatitude();
longitude=location.getLongitude();
CurrentPostion();
Toast.makeText(getApplicationContext(), latitude+"<(Lat,Lng)>"+longitude, Toast.LENGTH_SHORT).show();
}
};
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 15000, 0, locationListener);
方法CurrentPostion在我当前位置的地图上绘制图标。
public void CurrentPostion(){
if(latitude!=0.0 && longitude!=0.0){
MarkerOptions options=new MarkerOptions();
options.position(new LatLng(latitude, longitude));
options.icon(BitmapDescriptorFactory.fromResource(R.drawable.myicon));
googleMap.addMarker(options);
CameraPosition cameraPosition = new CameraPosition.Builder().target(new LatLng(latitude,longitude)).zoom(17).build();
googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
}
}
我拥有所有权限:
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<!-- Required to show current location -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
结果:
0.0≤(纬度,经度)GT; 0.0
答案 0 :(得分:2)
尝试更改此行
latitude=location.getLatitude();
longitude=location.getLongitude();
这个
latitude = Double.toString(location.getLatitude());
longitude= Double.toString(location.getLongitude())