在我的Android應用程序中,我使用google maps v2通過獲取設備當前位置的經度和緯度來顯示地圖。我是顯示在那個位置的別針。如何獲取用戶在地圖上點擊的緯度和經度android
現在,當用戶點擊或點擊地圖上的任何其他位置時,我必須獲取經緯度點,並且必須在該位置設置引腳。
您能否告訴我如何獲取用戶點擊/點擊位置的經緯度。
在我的Android應用程序中,我使用google maps v2通過獲取設備當前位置的經度和緯度來顯示地圖。我是顯示在那個位置的別針。如何獲取用戶在地圖上點擊的緯度和經度android
現在,當用戶點擊或點擊地圖上的任何其他位置時,我必須獲取經緯度點,並且必須在該位置設置引腳。
您能否告訴我如何獲取用戶點擊/點擊位置的經緯度。
我使用的一個例子。根據您的需求相應地更改它。我用它長按。
map.setOnMapLongClickListener(new OnMapLongClickListener() {
@Override
public void onMapLongClick(LatLng point) {
map.addMarker(new MarkerOptions().position(point).title("Custom location").icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED)));enter code here
}
});
的經緯度點包含協調長按
嘗試使用谷歌,地圖V2內置的方法。
map.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
@Override
public void onMapClick(LatLng position) {
Toast.makeText(context,position.latitude+" : "+position.longitude,Toast.LENGTH_SHORT).show();
}
});
// Setting onclick event listener for the map
mMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
@Override
public void onMapClick(LatLng point) {
// Creating MarkerOptions
MarkerOptions options = new MarkerOptions();
// Setting the position of the marker
options.position(point);
//Get LatLng from touched point
double touchLat=point.latitude;
double touchLong=point.longitude;
///here is reverse GeoCoding which helps in getting address from latlng
try {
Geocoder geo = new Geocoder(MainActivity.this.getApplicationContext(), Locale.getDefault());
List<Address> addresses = geo.getFromLocation(touchLat,touchLong, 1);
if (addresses.isEmpty()) {
Toast.makeText(getApplicationContext(),"Waiting for Location",Toast.LENGTH_SHORT).show();
}
else {
if (addresses.size() > 0) {
address =addresses.get(0).getFeatureName()
+ ", " + addresses.get(0).getLocality()
+ ", " + addresses.get(0).getAdminArea()
+ ", " + addresses.get(0).getCountryName();
Toast.makeText(getApplicationContext(), "Address:- " +address, Toast.LENGTH_LONG).show();
}
// draws the marker at the currently touched location
drawMarker(point,"Your Touched Position",address+"");
}
}
catch (Exception e) {
e.printStackTrace(); // getFromLocation() may sometimes fail
}
請考慮爲您的答案添加解釋,以及文檔鏈接。 – nvisser
嗨安東尼斯,感謝您的回覆。什麼是eventLocationCoords這裏。 –
我更新了我的答案。我刪除它。這是一個散列圖,我在裏面保存了這個位置,如果我的散列表的長度大於或等於1,那麼我沒有刪除任何其他點。你不需要所有這些。我只留下你需要的行 –
「LatLng點包含longpress的協調」 你的意思是「... longpress的座標」? – Neo42