2016-11-24 163 views
-1

我想獲取點擊地圖的地址。我嘗試使用地理編碼器。但它不工作。這是否需要任何API。請幫助我。我將在下面給我使用的代碼發短信。從經緯度獲取地址

mMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() { 

    @Override 
    public void onMapClick(LatLng point) { 
     //save current location 
     latLng = point; 

     List<Address> addresses = new ArrayList<>(); 
     try { 
      addresses = geocoder.getFromLocation(point.latitude, point.longitude,1); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 

     android.location.Address address = addresses.get(0); 

     if (address != null) { 
      StringBuilder sb = new StringBuilder(); 
      for (int i = 0; i < address.getMaxAddressLineIndex(); i++){ 
       sb.append(address.getAddressLine(i) + "\n"); 
      } 
      Toast.makeText(MapsActivity.this, sb.toString(), Toast.LENGTH_LONG).show(); 
     } 



     //place marker where user just clicked 
     marker = mMap.addMarker(new MarkerOptions().position(point).title("Marker"))); 


    } 
}); 
+0

查看我的答案:http://stackoverflow.com/a/32647204/3626214 – Aspicas

+0

openstreetmap.org提供了一種類似的功能來反轉地理編碼。 –

+2

define:*但它不工作。* ...這個代碼極其愚蠢......'addresses = new ArrayList <>();'???對於下一行是'addresses = geocoder ....'...'addresses.get(0);'?如果地址是空的呢? ...爲什麼它不在'地址= geocoder ....後面'第一次嘗試? – Selvin

回答

0

這很簡單,只需要在HTTP請求來調用和它將返回可輕鬆解析的JSON響應來獲取地址。

http://maps.googleapis.com/maps/api/geocode/json?latlng=23,72&sensor=true

{ 
    "results" : [ 
     { 
     "address_components" : [ 
      { 
       "long_name" : "Gujarat State Highway 17", 
       "short_name" : "GJ SH 17", 
       "types" : [ "route" ] 
      }, 
      { 
       "long_name" : "Babajipara", 
       "short_name" : "Babajipara", 
       "types" : [ "locality", "political" ] 
      }, 
      { 
       "long_name" : "Ahmedabad", 
       "short_name" : "Ahmedabad", 
       "types" : [ "administrative_area_level_2", "political" ] 
      }, 
      { 
       "long_name" : "Gujarat", 
       "short_name" : "GJ", 
       "types" : [ "administrative_area_level_1", "political" ] 
      }, 
      { 
       "long_name" : "India", 
       "short_name" : "IN", 
       "types" : [ "country", "political" ] 
      }, 
      { 
       "long_name" : "363115", 
       "short_name" : "363115", 
       "types" : [ "postal_code" ] 
      } 
     ] 
], 
    "status" : "OK" 
} 
0

```

    String mAddress = ""; 
        try { 

         //mAddress = new Geocoder(this).getFromLocation(Lat,Lng,1).get(0).getAddressLine(0).toString(); 
         Geocoder geocoder = new Geocoder(this); 
         // mAddress = new Geocoder(this).getFromLocation(Lat,Lng,1).get(0).getAddressLine().toString(); 
         for (int ai = 0; ai < 5; ai++) { 

          try { 
           mAddress = mAddress + "\n" + geocoder.getFromLocation(Lat, Lng, 1).get(0).getAddressLine(ai).toString(); 
          } catch (Exception e) { 
           break; 
          } 

         } 
        } catch (Exception e) { 
         mAddress = ""; 
        } 

```

那裏緯度,經度是你的位置的緯度和經度

0

你可以試試這個,

public String getCompleteAddress(double latitude, double longitude) { 
    String location = ""; 
    try { 
     Geocoder geocoder = new Geocoder(mContext, Locale.getDefault()); 
     List<Address> addresses = geocoder.getFromLocation(latitude, longitude, 1); 
     if (addresses.size() > 0) { 
      Address address = addresses.get(0); 
      String state, city, zip, street; 
      if (address.getAdminArea() != null) { 
       state = address.getAdminArea(); 
      } else { 
       state = ""; 
      } 
      if (address.getLocality() != null) { 
       city = address.getLocality(); 
      } else { 
       city = ""; 
      } 
      if (address.getPostalCode() != null) { 
       zip = address.getPostalCode(); 
      } else { 
       zip = ""; 
      } 

      if (address.getThoroughfare() != null) { 
       street = address.getSubLocality() + "," + address.getThoroughfare(); 
      } else { 
       street = address.getSubLocality() + "," + address.getFeatureName(); 
      } 
      location = street + "," + city + "," + zip + "," + state; 
     } 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 

    return location; 
} 

等一個,

public Address getAddressFromLocation(double latitude, double longitude){ 
    String zipcode=""; 
    Address address=null; 
    try { 
     Geocoder geocoder = new Geocoder(mContext, Locale.getDefault()); 
     List<Address> addresses = geocoder.getFromLocation(latitude, longitude, 1); 

     if (addresses.size() > 0) { 
      address= addresses.get(0); 
      String admin = address.getAdminArea(); 
      String subLocality = address.getSubLocality(); 
      String city = address.getLocality(); 
      zipcode = address.getPostalCode(); 

     } 

    } catch (IOException e) { 
     e.printStackTrace(); 
     mAppUtil.showToast("Please try again"); 


    } 
return address; 
} 

和第三個選項

http://maps.googleapis.com/maps/api/geocode/json?latlng=30.7333,76.7794&sensor=true

+0

thanku Saveen爲我的迴應..我應用你的第一個代碼,併爲我的位置是返回的長度爲0。之後,我發現地理編碼器後端不存在..我用谷歌的地方api。我該如何糾正這一點? – Aparna

+0

請重新檢查你的經度和緯度,它不應該是零或0. – Saveen

+0

你可以做以上api以及傳遞經緯度 – Saveen