Java Can';无法使用LocationManager获取坐标
我正在开发一个使用Google Maps V2 API的应用程序,但我在获取坐标并将其放入LatLng对象时遇到了麻烦。。。这是我的密码:Java Can';无法使用LocationManager获取坐标,java,android,coordinates,google-maps-android-api-2,Java,Android,Coordinates,Google Maps Android Api 2,我正在开发一个使用Google Maps V2 API的应用程序,但我在获取坐标并将其放入LatLng对象时遇到了麻烦。。。这是我的密码: public class TelaMapa extends android.support.v4.app.FragmentActivity implements OnMapClickListener, OnCameraChangeListener{ // Google Map protected GoogleMap
public class TelaMapa extends android.support.v4.app.FragmentActivity
implements OnMapClickListener, OnCameraChangeListener{
// Google Map
protected GoogleMap googleMap;
private SupportMapFragment mapFragment;
protected AndroidLocationSource locationSource;
@Override
protected void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.activity_mapa);
}
@Override
protected void onResume(){
super.onResume();
configureMap();
}
/**
* function to load map. If map is not created it will create it for you
* */
private void configureMap() {
if (googleMap == null) {
mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
googleMap = mapFragment.getMap();
googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
googleMap.setMyLocationEnabled(true);
LocationManager LM = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
String bestProvider = LM.getBestProvider(new Criteria(),true);
Location localAtu;
/*Location myLocation = LM.getLastKnownLocation(bestProvider);
double lat= myLocation.getLatitude();
double lng = myLocation.getLongitude();
LatLng latLng = new LatLng(lat, lng);
googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, 20));*/
if(bestProvider != null){
localAtu = LM.getLastKnownLocation(bestProvider);
} else {
localAtu = LM.getLastKnownLocation(LocationManager.GPS_PROVIDER);
}
LatLng latLng;
if(localAtu != null){
latLng = new LatLng(localAtu.getLatitude(), localAtu.getLongitude());
final CameraPosition position = new CameraPosition.Builder()
.target(latLng)
.bearing(0)
.tilt(0)
.zoom(17)
.build();
CameraUpdate update = CameraUpdateFactory.newCameraPosition(position);
googleMap.moveCamera(update);
adicionarMarcador(googleMap, latLng);
locationSource = new AndroidLocationSource();
googleMap.setLocationSource(locationSource);
locationSource.setLocation(latLng);
} else {
Toast.makeText(getApplicationContext(),
"GPS desligado ou indisponível!", Toast.LENGTH_LONG)
.show();
}
// check if map is created successfully or not
if (googleMap == null) {
Toast.makeText(getApplicationContext(),
"Desculpe! Não foi possível criar o mapa!", Toast.LENGTH_SHORT)
.show();
}
}
}
就是这样,我把它安装在我的摩托罗拉Atrix上,它总是在else里向我展示烤面包片
块谢谢你的阅读 Sanket Kachhela回答! 只要使用本教程,它就会很好地工作!
androidhive.info/2012/07/android gps位置管理器教程您可以使用它将代码示例缩小到产生问题的最小值,并指定您实际看到的问题是什么?很难从你的帖子中看出你真正的问题是什么。谢谢问题是最好的提供程序为Null,而localAtu也为Null。。。所以,toast消息总是显示在Hanks Sanket中,我将在教程中尝试该代码,它可能会起作用。。。如果没有,问题是我的手机代码对我有效,谢谢Sanket!