我想通过输入地址来获得的纬度和经度, 下面的代码,我从here但在我的情况下被复制,而我经过详细地址,它给我的错误:获取纬度,经度从地址
Caused by: java.lang.IllegalArgumentException: Illegal character in query at index 56: http://maps.google.com/maps/api/geocode/json?address=43 S BROADWAY, Pitman, New Jersey
。
public static void getLatLongFromAddress(String youraddress) {
String uri = "http://maps.google.com/maps/api/geocode/json?address=" +
youraddress + "&sensor=false";
HttpGet httpGet = new HttpGet(uri);
HttpClient client = new DefaultHttpClient();
HttpResponse response;
StringBuilder stringBuilder = new StringBuilder();
try {
response = client.execute(httpGet);
HttpEntity entity = response.getEntity();
InputStream stream = entity.getContent();
int b;
while ((b = stream.read()) != -1) {
stringBuilder.append((char) b);
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
JSONObject jsonObject = new JSONObject();
try {
jsonObject = new JSONObject(stringBuilder.toString());
double lng = ((JSONArray)jsonObject.get("results")).getJSONObject(0)
.getJSONObject("geometry").getJSONObject("location")
.getDouble("lng");
dobule lat = ((JSONArray)jsonObject.get("results")).getJSONObject(0)
.getJSONObject("geometry").getJSONObject("location")
.getDouble("lat");
Log.d("latitude", lat);
Log.d("longitude", lng);
} catch (JSONException e) {
e.printStackTrace();
}
}
你有什么改变? SO的要点是,与OP相同问题的其他人可以复制和修复它。使用固定的代码而没有解释这很难做到。 – Bobby
http://developer.android.com/reference/android/location/Geocoder.html请在这里指定 –
您需要指定当前城市或位置的地址引用任何字段名称的表单地址字符串,然后您将得到lat和lng Geocoder geocoder = new Geocoder(this,Locale.getDefault()); 列表
addresses = geocoder.getFromLocation(lat,lng,1); –