2016-11-24 167 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