gpt4 book ai didi

java - 无法使用 LocationManager 获取坐标

转载 作者:行者123 更新时间:2023-12-01 12:40:31 24 4
gpt4 key购买 nike

我正在开发一个使用 Google Maps V2 API 的应用程序,但在获取坐标并将其放入 LatLng 对象中时遇到问题...这是我的代码:

 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 中,它总是向我显示其他内容中的 toast block ...感谢您的阅读!

最佳答案

Sanket Kachhela 回答!只要使用这个教程,它就会很好用!androidhive.info/2012/07/android-gps-location-manager-tutorial

关于java - 无法使用 LocationManager 获取坐标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25147631/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com