在我的应用用户中输入一个位置,我必须获得该位置的纬度和经度。所以这里我没有Location
个对象而是String
。
我想到了使用此Location
创建String
对象的选项,但这是不可能的。
我知道使用Google的place API是可能的,我知道如何使用它,但我更喜欢使用SDK中提供的任何方法。
有没有办法这样做?
答案 0 :(得分:3)
您可以使用Geocoder。像这样:
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;
}
祝福。
答案 1 :(得分: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调用来完成。 希望能帮助到你!
答案 2 :(得分:2)
只有一种可能性(谷歌地理编码API除外)可以获得yakiv.mospan所述的纬度和经度。但是Geocoder
总是会让我回复null
,对很多人来说也是如此。我通过在stackoverflow.com上搜索返回null的问题来了解这一点。
毕竟,我决定使用Google places API因为没有其他选项可用。
答案 3 :(得分:0)
你可以看一下:
http://developer.android.com/reference/android/location/Geocoder.html
getFromLocationName方法可以帮助您。
答案 4 :(得分:0)
使用Google地理编码API获取地址的坐标。 http://code.google.com/apis/maps/documentation/javascript/services.html#Geocoding
详细介绍了如何使用的信息。 以下是示例网址,因此请检查响应并查看这是否对您有用。
http://maps.googleapis.com/maps/api/geocode/json?address=ahmedabad&sensor=false
答案 5 :(得分: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();
}
}
}
以下是更多可以帮助您的链接:
答案 6 :(得分:-1)
你可以使用它,
Geocoder geocoder = new Geocoder(this, Locale.getDefault());
geocoder.getFromLocation(LATITUDE, LONGITUDE, 3);