我正在开发谷歌地图应用程序接口v2。我有一个向我当前位置添加标记的按钮。
地图可以获得正确的位置,但有时(不是每次)当我想要添加标记到该位置时,标记会远离蓝点。
我想在蓝点上添加我的记号笔。

这是我的findMe按钮代码:
ImageButton findMyLocation_btn = (ImageButton) findViewById(R.id.findme);
if (findMyLocation_btn != null) {
findMyLocation_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (!manager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
buildAlertMessageNoGps();
} else {
currentLocation = mMap.getMyLocation();
if ( marker1 == null && currentLocation != null) {
new onMyLocationClick().execute();
}
}
}
});和onMyLocationClick()
private class onMyLocationClick extends AsyncTask{
@Override
protected Object doInBackground(Object[] params) {
Geocoder gc = new Geocoder(MainActivity.this);
List<Address> list = null;
try {
list = gc.getFromLocation(currentLocation.getLatitude(), currentLocation.getLongitude(), 1);
} catch (Exception ex) {
ex.printStackTrace();
}
if (list != null) {
add = list.get(0);
}
try {
origin = new LatLng(add.getLatitude(),add.getLongitude());
} catch (Exception ex) {
ex.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute (Object o){
createMarker(add.getLatitude(), add.getLongitude(), add.getLocality(), add.getSubLocality());
}
}发布于 2017-03-07 15:44:05
基于Google Maps文档:
此方法已弃用。请改用com.google.android.gms.location.FusedLocationProviderApi。FusedLocationProviderApi提供了改进的位置查找和电源使用,并由“我的位置”蓝点使用。
获取当前位置的代码如下所示:
LocationManager locationManager = (LocationManager)
getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
Location location = locationManager.getLastKnownLocation(locationManager
.getBestProvider(criteria, false));
double latitude = location.getLatitude();
double longitude = location.getLongitude();对于更高级的解决方案,你可以使用谷歌提到的FusedLocationProvider:https://developer.android.com/training/location/retrieve-current.html#GetLocation
https://stackoverflow.com/questions/42642684
复制相似问题