android - 如何从 android google map v2 中的给定地址获取经纬度?

标签 android google-maps

<分区>

我正在使用 google map v2 android, map 也显示在我的应用程序中。 我只想从给定地址获取纬度和经度。

示例:如果我在文本字段中键入“Indore”,那么我将获得该城市的纬度和经度。

最佳答案

你可以用这个方法

 public void getLatLongFromPlace(String place) {
            try {
                Geocoder selected_place_geocoder = new Geocoder(context);
                List<Address> address;

                address = selected_place_geocoder.getFromLocationName(place, 5);

                if (address == null) {
                    d.dismiss();
                } else {
                    Address location = address.get(0);
                               Latitude lat= location.getLatitude();
                Longitude lng = location.getLongitude();

                }

            } catch (Exception e) {
                e.printStackTrace();
fetchLatLongFromService fetch_latlng_from_service_abc = new fetchLatLongFromService(
                        place.replaceAll("\\s+", ""));
                fetch_latlng_from_service_abc.execute();

            }

        }


//Sometimes happens that device gives location = null

    public class fetchLatLongFromService extends
                AsyncTask<Void, Void, StringBuilder> {
            String place;


            public fetchLatLongFromService(String place) {
                super();
                this.place = place;

            }

            @Override
            protected void onCancelled() {
                // TODO Auto-generated method stub
                super.onCancelled();
                this.cancel(true);
            }

            @Override
            protected StringBuilder doInBackground(Void... params) {
                // TODO Auto-generated method stub
                try {
                    HttpURLConnection conn = null;
                    StringBuilder jsonResults = new StringBuilder();
                    String googleMapUrl = "http://maps.googleapis.com/maps/api/geocode/json?address="
                            + this.place + "&sensor=false";

                    URL url = new URL(googleMapUrl);
                    conn = (HttpURLConnection) url.openConnection();
                    InputStreamReader in = new InputStreamReader(
                            conn.getInputStream());
                    int read;
                    char[] buff = new char[1024];
                    while ((read = in.read(buff)) != -1) {
                        jsonResults.append(buff, 0, read);
                    }
                    String a = "";
                    return jsonResults;
                } catch (Exception e) {
                    e.printStackTrace();
                }
                return null;

            }

        @Override
        protected void onPostExecute(StringBuilder result) {
            // TODO Auto-generated method stub
            super.onPostExecute(result);
            try {
                JSONObject jsonObj = new JSONObject(result.toString());
                JSONArray resultJsonArray = jsonObj.getJSONArray("results");

                // Extract the Place descriptions from the results
                // resultList = new ArrayList<String>(resultJsonArray.length());

                JSONObject before_geometry_jsonObj = resultJsonArray
                        .getJSONObject(0);

                JSONObject geometry_jsonObj = before_geometry_jsonObj
                        .getJSONObject("geometry");

                JSONObject location_jsonObj = geometry_jsonObj
                        .getJSONObject("location");

                String lat_helper = location_jsonObj.getString("lat");
                double lat = Double.valueOf(lat_helper);


                String lng_helper = location_jsonObj.getString("lng");
                double lng = Double.valueOf(lng_helper);


                LatLng point = new LatLng(lat, lng);


            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();

            }
        }
    }

查找位置的常用方法是我之前提到的方法,但有时网络提供商或 GPS 提供商无法获取位置,因此给出空值,我也遇到过这种情况,但是当位置为空且抛出异常,使用谷歌网络服务获取纬度和经度。首先尝试从上述方法中获取,如果失败,有时会使用第二种方法从网络服务中获取它。

关于android - 如何从 android google map v2 中的给定地址获取经纬度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22909756/

相关文章:

android - 如何在 fragment 中使用 viewpager 和 tablayout?

java - 推特列表 twitter4j

android - 用于自定义类的 Dagger Android 可能吗?

android - 从 URL Google Map API 或 SDK 从搜索区域的特征 ID 获取位置?

java - 使用时钟更新 TextView

android - 如何在 kotlin parcelable 中使用 An object?

google-maps - 我应该为浏览器还是 PC 编写代码? (车队的管理)

javascript - 将折线添加到谷歌地图

java - 我们可以在java桌面应用程序中使用离线谷歌地图吗?

android - 如何在android中找到两条折线之间的交点?