【问题标题】:How to get the address by latitude and longitude using openstreetmap in android如何在android中使用openstreetmap按经纬度获取地址
【发布时间】:2016-10-06 18:23:30
【问题描述】:

在我的应用程序中,我使用的是 osm map。我有纬度和经度。 使用这种方法

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

所以从这里我将如何查询以从 osm 数据库中获取城市名称。请帮我。 我怎样才能将其转换为人类可以理解的形式。这是我正在使用的代码。link

【问题讨论】:

    标签: android openstreetmap


    【解决方案1】:

    试试这个代码来获取地址。

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

    对于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) {
    }
    

    【讨论】:

    【解决方案2】:

    这是我的解决方案。我认为它也适合你。

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

    【讨论】:

      猜你喜欢
      • 2014-06-14
      • 1970-01-01
      • 2023-03-09
      • 2013-07-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多