4

在我的应用程序中,用户键入一个位置,我必须获取该位置的纬度和经度。所以在这里我没有Location对象,而是一个String.

我想到了一个使用它创建Location对象的选项,String但这是不可能的。

我知道使用 Google 的 Places API 是可能的,而且我知道如何使用它,但我更喜欢使用 SDK 中可用的任何方法。

有什么办法吗?

4

7 回答 7

4

您可以使用地理编码器。像这样 :

Geocoder geocoder = new Geocoder(App.getContext(), Locale.US);
    List<Address> listOfAddress;
    try {
        listOfAddress = geocoder.getFromLocation(theLatitude, theLongitude, 1);
        if(listOfAddress != null && !listOfAddress.isEmpty()){
        Address address = listOfAddress.get(0);

        String country = address.getCountryCode();
        String adminArea= address.getAdminArea();
        String locality= address.getLocality();

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

更新:从地址获取位置使用:

listOfAddress = geocoder.getFromLocationName("Address", 1);

并获得 lat, lon :

double latitude = address.getLatitude();
double longitude = address.getLongitude();

更新:当 Geocoder 返回 null 时,使用此代码段获取位置:

private static final String URL = "http://maps.googleapis.com/maps/api/geocode/xml";

public static RemoteCommand getLocation(String theAddress){
    RemoteCommand result = new RemoteCommand();

    result.setType(Type.GET);
    result.setUrl(URL);
    result.addGetParam("address", theAddress);
    result.addGetParam("sensor", "false");
    result.addGetParam("language", "en");

    // See description about GeocoderXMLHandler
    result.setXmlHandler(new GeocoderXMLHandler());

    return result;
}

最好的祝愿。

于 2013-06-24T13:04:01.997 回答
3
String UrlCity = "http://maps.googleapis.com/maps/api/geocode/json?address=" + NameString + "&sensor=false";

        JsonObjectRequest stateReq = new JsonObjectRequest(Request.Method.GET, UrlState, null, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {
                JSONObject location;
                try {
                    // Get JSON Array called "results" and then get the 0th
                    // complete object as JSON
                    location = response.getJSONArray("results").getJSONObject(0).getJSONObject("geometry").getJSONObject("location");
                    // Get the value of the attribute whose name is
                    // "formatted_string"
                    stateLocation = new LatLng(location.getDouble("lat"), location.getDouble("lng"));
                    // System.out.println(stateLocation.toString());
                } catch (JSONException e1) {
                    e1.printStackTrace();

                }
            }

        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Log.d("Error.Response", error.toString());
            }
        });
        // add it to the RequestQueue
        reqQueue.add(stateReq);

你从给定的名字得到坐标。这是通过使用Google Volley library完成的,或者您可以通过包装在 AsyncTask 或 runnable 中的简单 http 调用来完成。希望能帮助到你!

于 2013-11-14T17:21:24.327 回答
2

如 yakiv.mospan 所述,只有一种可能性(谷歌地理编码 API 除外)来获取纬度和经度。但Geocoder总是返回我null,许多人也是如此。我通过在 stackoverflow.com 上搜索返回 null 的问题了解到这一点。

毕竟,我决定使用Google Places API ,因为没有其他选项可用。

于 2013-06-24T17:38:01.703 回答
0

你可以看看:

http://developer.android.com/reference/android/location/Geocoder.html

getFromLocationName 方法可以帮助您。

于 2013-06-24T13:00:04.527 回答
0

使用 Google Geocoding API 获取地址的坐标。http://code.google.com/apis/maps/documentation/javascript/services.html#Geocoding

在此详细信息中给出了如何使用。下面也是示例 URL,因此请检查响应并查看这对您是否有用。

http://maps.googleapis.com/maps/api/geocode/json?address=ahmedab​​ad&sensor=false

于 2013-06-24T13:18:00.280 回答
0

在这里您可以使用此代码:

String add = address + "," + city + "," + country;

String addressUrl = "http://maps.googleapis.com/maps/api/geocode/json?address="
                + URLEncoder.encode(add.replaceAll(" ", "+"))
                + "&sensor=true";
try {
            HttpClient httpClient = new DefaultHttpClient();// Client

            HttpGet getRequest = new HttpGet();
            getRequest.setURI(new URI(addressUrl));

            // HttpPost postRequest = new HttpPost();
            // postRequest.setURI(new URI(url));

            HttpResponse response = httpClient.execute(getRequest);
            in = new BufferedReader(new InputStreamReader(response
                    .getEntity().getContent()));
            StringBuffer sb = new StringBuffer("");
            String line = "";
            String NL = System.getProperty("line.separator");
            while ((line = in.readLine()) != null) {
                sb.append(line + NL);
            }
            in.close();
            String page = sb.toString();
            JSONObject jsonObject = new JSONObject(page);
            if (jsonObject.has("results")) {
                JSONArray jsonArray = (JSONArray) jsonObject.get("results");

                if (jsonArray.length() > 0) {
                    jsonObject = (JSONObject) jsonArray.get(0);
                    if (jsonObject.has("geometry")) {
                        jsonObject = (JSONObject) jsonObject
                                .get("geometry");

                        if (jsonObject.has("location")) {
                            JSONObject location = (JSONObject) jsonObject
                                    .get("location");

                            latitude = (Double) location.get("lat");
                            longitude = (Double) location.get("lng");
                            Log.e(TAG, "lat : " + latitude + " long : "
                                    + longitude);
                        }
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

以下是可以帮助您的更多链接:

谷歌地图 V2

Google Places API 演示

于 2013-07-03T12:53:07.120 回答
-1

你可以用这个,

Geocoder geocoder = new Geocoder(this, Locale.getDefault());
geocoder.getFromLocation(LATITUDE, LONGITUDE, 3);
于 2013-06-24T13:00:41.463 回答