我正在尝试制作一个应用,以获取标记在android中放置在地图上的位置名称。当我使用marker.position()
函数时,它给了我一个奇怪的字符串。以com.google.android.gms.maps.model.MarkerOptions@28f817d
我尝试使用地理编码,但对我不起作用。我可能以不正确的方式使用它。
mMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
@Override
public void onMapClick(LatLng point) {
mMap.clear();
MarkerOptions marker = new MarkerOptions();
mMap.addMarker(marker.position(point)
.draggable(true)
.title(String.valueOf(marker.position(point)))
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE))
.snippet(point.latitude + "," + point.longitude));
}
});
}
I wish that I can get the location name on the title.
答案 0 :(得分:0)
做这样的事情
mMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
@Override
public void onMapClick(LatLng point) {
mMap.clear();
MarkerOptions marker = new MarkerOptions();
mMap.addMarker(marker.position(point)
.draggable(true)
.title(getAddress(point.getLatitude(),point.getLongitude()))
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE))
.snippet(point.latitude + "," + point.longitude));
}
});
}
像这样编写一个名为getAddress的函数
public string getAddress(double lat, double lng) {
Geocoder geocoder = new Geocoder(HomeActivity.mContext, Locale.getDefault());
try {
List<Address> addresses = geocoder.getFromLocation(lat, lng, 1);
Address obj = addresses.get(0);
String add = obj.getAddressLine(0);
add = add + "\n" + obj.getCountryName();
add = add + "\n" + obj.getCountryCode();
add = add + "\n" + obj.getAdminArea();
add = add + "\n" + obj.getPostalCode();
add = add + "\n" + obj.getSubAdminArea();
add = add + "\n" + obj.getLocality();
add = add + "\n" + obj.getSubThoroughfare();
Log.v("IGA", "Address" + add);
// Toast.makeText(this, "Address=>" + add,
// Toast.LENGTH_SHORT).show();
// TennisAppActivity.showDialog(add);
return add;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show();
return e;
}
}
答案 1 :(得分:0)
那是因为marker.position(point))返回类LatLng的对象,而不是String本身。您可以使用其任何属性(纬度和经度):
String.valueOf(marker.position(point))
到
"" + marker.position(point).latitude + marker.position(point).longitude