简体   繁体   English

如何在Android中使用openstreetmap按经度和纬度获取地址

[英]How to get the address by latitude and longitude using openstreetmap in android

In my app I am using osm map. 在我的应用中,我正在使用osm map。 I have latitude and longitude. 我有经度和纬度。 using this method 使用这种方法

proj = mapView.getProjection();
        loc = (GeoPoint) proj.fromPixels((int) e.getX(), (int) e.getY());
        String longitude = Double
                .toString(((double) loc.getLongitudeE6()) / 1000000);
        String latitude = Double
                .toString(((double) loc.getLatitudeE6()) / 1000000);
        Toast toast = Toast.makeText(getApplicationContext(), "Longitude: "
                + longitude + " Latitude: " + latitude, Toast.LENGTH_SHORT);
        toast.show();

So from here how I will query to get the city name from osm database. 因此,从这里我将如何查询以从osm数据库中获取城市名称。 Please help me. 请帮我。 How can I convert this into human understandable form. 我如何将其转换为人类可以理解的形式。 Here is my code which I am using. 这是我正在使用的代码。 link 链接

Try this code for getting address. 尝试使用此代码获取地址。

Geocoder geocoder;
List<Address> addresses;
geocoder = new Geocoder(this, Locale.getDefault());
addresses = geocoder.getFromLocation(latitude, longitude, 1);

String address = addresses.get(0).getAddressLine(0);
String city = addresses.get(0).getAddressLine(1);
String country = addresses.get(0).getAddressLine(2);

for openstreammap 对于openstreammap

    final String requestString = "http://nominatim.openstreetmap.org/reverse?format=json&lat=" +
            Double.toString(lat) + "&lon=" + Double.toString(lon) + "&zoom=18&addressdetails=1";        

RequestBuilder builder = new RequestBuilder(RequestBuilder.GET, URL.encode(requestString));
try {
    @SuppressWarnings("unused")
    Request request = builder.sendRequest(null, new RequestCallback() {
        @Override
        public void onResponseReceived(Request request, Response response) {
            if (response.getStatusCode() == 200) {
                String city = "";
                try {
                    JSONValue json = JSONParser.parseStrict(response);
                    JSONObject address = json.isObject().get("address").isObject();
                    final String quotes = "^\"|\"$";

                    if (address.get("city") != null) {
                        city = address.get("city").toString().replaceAll(quotes, "");
                    } else if (address.get("village") != null) {
                        city = address.get("village").toString().replaceAll(quotes, "");
                    }
                } catch (Exception e) {
                }            
            }
        }
    });
} catch (Exception e) {
}

here is my solution. 这是我的解决方案。 i think it works for you also. 我认为它也对您有效。

 public String ConvertPointToLocation(GeoPoint point) {   
     String address = "";
     Geocoder geoCoder = new Geocoder( getBaseContext(), Locale.getDefault());
     try {
         List<Address> addresses = geoCoder.getFromLocation(
             point.getLatitudeE6()  / 1E6, 
             point.getLongitudeE6() / 1E6, 1);

         if (addresses.size() > 0) {
             for (int index = 0; index < addresses.get(0).getMaxAddressLineIndex(); index++)
                 address += addresses.get(0).getAddressLine(index) + " ";
         }


         Toast.makeText(getBaseContext(), address, Toast.LENGTH_SHORT).show();

     }
     catch (IOException e) {                
         e.printStackTrace();
     }   

     return address;
 } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM