【问题标题】:how to get the address of center point of GoogleMap v2 in android?如何在android中获取GoogleMap v2的中心点地址?
【发布时间】:2015-06-01 08:47:06
【问题描述】:

我关注this tutorial 以获取 v2 GoogleMap 中心点的位置。现在我想使用反向地理编码获取该点的地址 ....

单击按钮时,我正在使用以下代码获取中心点的 LatLang:

   @Override
public void onClick(View view) {
    if(view.getId()==R.id.btn_set_location){

        VisibleRegion visibleRegion = map.getProjection()
                .getVisibleRegion();

        Point x = map.getProjection().toScreenLocation(
                visibleRegion.farRight);

        Point y = map.getProjection().toScreenLocation(
                visibleRegion.nearLeft);

        Point centerPoint = new Point(x.x / 2, y.y / 2);

        LatLng centerFromPoint = map.getProjection().fromScreenLocation(
                centerPoint);



    }
}

我已经看到其他教程分别使用纬度和经度获取地址,在我的情况下,我有 LatLang,如何使用 LatLang 获取地址..这里需要帮助....如果还有其他方法可以获得中心点地图和同一点的地址...任何帮助将不胜感激...谢谢

【问题讨论】:

    标签: android google-maps-android-api-2 reverse-geocoding android-googleapiclient


    【解决方案1】:

    我想,你想将 LatLang 作为参数传递。我用以下代码解决了这个问题:

    private class ReverseGeoCoding extends AsyncTask<LatLng, Void, String> {
        Context mContext;
    
    
        public ReverseGeoCoding(Context context){
            super();
            mContext = context;
        }
    
        // Finding address using reverse geocoding
        @Override
        protected String doInBackground(LatLng... params) {
            Geocoder geocoder = new Geocoder(mContext);
            double latitude = params[0].latitude;
            double longitude = params[0].longitude;
    
            List<Address> addresses = null;
            String addressText="";
    
            try {
                addresses = geocoder.getFromLocation(latitude, longitude,1);
            } catch (IOException e) {
                e.printStackTrace();
            }
    
            if(addresses != null && addresses.size() > 0 ){
                Address address = addresses.get(0);
    
                addressText = String.format("%s, %s, %s",
                        address.getMaxAddressLineIndex() > 0 ? address.getAddressLine(0) : "",
                        address.getLocality(),
                        address.getCountryName());
            }
    
            return addressText;
        }
    

    希望这对你有用>>>

    编辑后:

    您可以关注this,如果您从事活动类工作,如果您从事片段类工作,那么以下代码将适合您:

    new ReverseGeoCoding(getActivity()).execute(centerFromPoint);
    

    【讨论】:

    • 那么如何从我的按钮事件处理部分调用 AsyncTask 我在使用适当参数调用类时遇到问题...我遵循上述答案,但在调用类的情况下仍然无法工作.. ..
    • 感谢您的回答,我浏览了链接...因为我在 Activity 类工作,它对我有用...。感谢您的帮助
    【解决方案2】:
    public class AsyncRouteGetter extends AsyncTask<Double, String, String> {
    protected String doInBackground(Double... coords) {
        String xml = "";
        String url = "http://maps.google.com/maps/api/geocode/xml?address=" + coords[0] + "," + coords[1] + "&sensor=false";
        //String url = "http://maps.google.com/maps/api/geocode/json?address=51.0031761,17.0499418&sensor=false";
        try {
            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost(url);
            HttpResponse httpResponse = httpClient.execute(httpPost);
            HttpEntity httpEntity = httpResponse.getEntity();
            xml = EntityUtils.toString(httpEntity);
        } catch (Exception e) { e.printStackTrace(); }
        //Log.d("LOK", xml);
    
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = null;
        Document doc = null;
        try {
            builder = factory.newDocumentBuilder();
            InputSource is = new InputSource(new StringReader(xml));
            doc = builder.parse(is);
        } catch (Exception e) { Log.d("DOC", "BLAD2"); e.printStackTrace(); return null; }
        String result = doc.getElementsByTagName("formatted_address").item(0).getTextContent();
        Log.d("LOKALIZACJA", result);
    
        return result;
        }
    }
    

    这对我来说非常有效。随意将其标记为答案:)

    更新

     AsyncRouteGetter arg = new AsyncRouteGetter();
            arg.execute(location.latitude, location.longitude);
            String route = null;
            try {
                route = arg.get();
            } catch (Exception e) { Log.d("LOK", "Error1"); }
            if (route != null) {
                Log.d("LOK", route);
                currentAddress = route;
            } else {
                Log.d("LOK", "Error2");
                currentAddress = "anything, so you wont work with null";
            }
    

    【讨论】:

    • 我仔细研究了你的代码@xenteros,那么如何从onclick方法调用AsyncRouteGetter() ....我在传递参数时遇到问题。
    • 我已经在“centerFromPoint”中获得了纬度和经度,我唯一需要做的就是将其作为参数传递@xenteros
    • 在第二个代码中,将 location.latitude 替换为第一个坐标,将 location.longitude 替换为第二个。然后在 String currentAdress 中,您将拥有街道或建筑物名称等地址。是你想要的吗?
    猜你喜欢
    • 2012-12-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-25
    • 2013-05-30
    • 2015-08-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多