我已将反向地理编码添加到我的地图并将地图设置为可点击。一切都正常,但是当我点击地图时地址没有显示在标记上方。
我的latLan是一个数组,但这是允许我使用变量的唯一方法。我认为问题出在这个区域,但不能把手放在上面。
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
mMap.setMapType(GoogleMap.MAP_TYPE_HYBRID); // changes view to hybrid
mMap.setMyLocationEnabled(true); // shows location on map
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); // uses GPS only to get location
double currentLatitude = location.getLatitude();
double currentLongitude = location.getLongitude();
final LatLng[] latLng = {(new LatLng(currentLatitude, currentLongitude))};
mMap.animateCamera(
CameraUpdateFactory.newLatLngZoom(latLng[0], 18)); // This will zoom camera to updated lat and long without constant updates
mMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() {// Setting a click event handler for the map
@Override
public void onMapClick(LatLng arg0) {
Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); // uses GPS only to get location
double currentLatitude = location.getLatitude();
double currentLongitude = location.getLongitude();
//LatLng latLng;
// Getting the Latitude and Longitude of the touched location
latLng[0] = arg0;
// Clears the previously touched position
mMap.clear();
// Animating to the touched position
mMap.animateCamera(CameraUpdateFactory.newLatLng(latLng[0]));
// Creating a marker
markerOptions = new MarkerOptions();
// Setting the position for the marker
markerOptions.position(latLng[0]);
// Placing a marker on the touched position
mMap.addMarker(markerOptions);
// Adding Marker on the touched location with address
new ReverseGeocodingTask(getBaseContext()).execute(latLng[0]);
}
});
//new ReverseGeocodingTask(getBaseContext()).execute(latLng);
}
private class ReverseGeocodingTask extends AsyncTask<LatLng, Void, String> {
Context mContext;
public ReverseGeocodingTask(Context context) {
super();
mContext = context;
}
@Override
protected String doInBackground(LatLng... params) {
Geocoder geocoder = new Geocoder(mContext);
double latitude = params[0].latitude;
double longitude = params[0].longitude;
List<android.location.Address> addresses = null;
String addressText = "";
try {
addresses = geocoder.getFromLocation(latitude, longitude, 1);
} catch (IOException e) {
e.printStackTrace();
}
if (addresses != null && addresses.size() > 0) {
android.location.Address address = addresses.get(0);
addressText = String.format("%s, %s, %s", address.getMaxAddressLineIndex() > 0 ?
address.getAddressLine(0) : "", address.getLocality(), address.getCountryName());
}
return addressText;
}
@Override
protected void onPostExecute(String addressText) {
markerOptions.title(addressText);
mMap.addMarker(markerOptions);
}
}
}
答案 0 :(得分:0)
测试了您的代码,发现可能的错误原因。
//在触摸位置放置标记
mMap.addMarker(markerOptions);
在执行ReverseGeocodingTask()之前实现addMarker()
并在addMarker()
中再次调用onPostExecute()
。您可以尝试添加足够大的自定义标记图像,以使地址和覆盖标记可见。
Customize标记图片
您可以使用自定义标记图像(通常称为图标)替换默认标记图像。自定义图标始终设置为BitmapDescriptor,并使用BitmapDescriptorFactory类中的四种方法之一进行定义。
自定义图片代码:
private static final LatLng MELBOURNE = new LatLng(-37.813, 144.962);
private Marker melbourne = mMap.addMarker(new MarkerOptions()
.position(MELBOURNE)
.title("Melbourne")
.snippet("Population: 4,137,400")
.icon(BitmapDescriptorFactory.fromResource(R.drawable.arrow)));
您可以在mMap.addMarker(markerOptions);
之前移除第一个new ReverseGeocodingTask(getBaseContext()).execute(latLng[0]);
,然后您的应用将按原样运行。
答案 1 :(得分:0)
使用此代码可能有助于获取地址
if(addresses!= null&amp;&amp; addresses.size()&gt; 0){ 地址address = addresses.get(0); String addressLine = address.getAddressLine(0)+ address.getAddressLine(1)+ address.getAddressLine(2)+ address.getAddressLine(3); }