2014-06-19 33 views
1

在我的Android應用程序中,我使用google maps v2通過獲取設備當前位置的經度和緯度來顯示地圖。我是顯示在那個位置的別針。如何獲取用戶在地圖上點擊的緯度和經度android

現在,當用戶點擊或點擊地圖上的任何其他位置時,我必須獲取經緯度點,並且必須在該位置設置引腳。

您能否告訴我如何獲取用戶點擊/點擊位置的經緯度。

回答

3

我使用的一個例子。根據您的需求相應地更改它。我用它長按。

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 
      } 
     }); 

的經緯度點包含協調長按

+0

嗨安東尼斯,感謝您的回覆。什麼是eventLocationCoords這裏。 –

+0

我更新了我的答案。我刪除它。這是一個散列圖,我在裏面保存了這個位置,如果我的散列表的長度大於或等於1,那麼我沒有刪除任何其他點。你不需要所有這些。我只留下你需要的行 –

+0

「LatLng點包含longpress的協調」 你的意思是「... longpress的座標」? – Neo42

1

嘗試使用谷歌,地圖V2內置的方法。

map.setOnMapClickListener(new GoogleMap.OnMapClickListener() { 
     @Override 
     public void onMapClick(LatLng position) { 
     Toast.makeText(context,position.latitude+" : "+position.longitude,Toast.LENGTH_SHORT).show(); 
     } 
}); 
0

請嘗試以下操作。

編寫一個派生自Overlay類的類並覆蓋onTap()方法。然後,您可以將您的覆蓋圖添加到您的。當您在地圖上的某處選擇標籤時,表示您點按位置的GeoPoint對象將傳遞給onTap()方法。

OR

現代答案here,使用Google地圖Android版V2,是使用OnMapClickListener,它給你的地圖上的敲擊的經緯度。

0
// 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 
     } 
+1

請考慮爲您的答案添加解釋,以及文檔鏈接。 – nvisser

相關問題