我想通过输入地址获得经纬度, 我从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();
}
}
答案 0 :(得分:2)
答案 1 :(得分:2)
使用:
youraddress = youraddress.replaceAll("\\s", "+");
String uri = "http://maps.google.com/maps/api/geocode/json?address=" +
youraddress + "&sensor=false";
将所有空格替换为+,类似于谷歌的方式。这就是我们在应用中使用的方式。
答案 2 :(得分:1)
您需要从地址指定任何字段名称表单地址字符串的当前城市或位置引用,然后您获得lat和lng之类的
`Geocoder geocoder = new Geocoder(this, Locale.getDefault()); List<Address> addresses = geocoder.getFromLocation(lat, lng, 1);`
请试试这个:
private static JSONObject getLocationInfo(String address)
{
StringBuilder stringBuilder = new StringBuilder();
try {
address = address.replaceAll(" ","%20");
HttpPost httppost = new HttpPost("http://maps.google.com/maps/api/geocode/json?address=" + address + "&sensor=false");
HttpClient client = new DefaultHttpClient();
HttpResponse response;
stringBuilder = new StringBuilder();
response = client.execute(httppost);
HttpEntity entity = response.getEntity();
InputStream stream = entity.getContent();
int b;
while ((b = stream.read()) != -1) {
stringBuilder.append((char) b);
}
} catch (ClientProtocolException e) {
Log.i("getLocationInfo ClientProtocolException", e.toString());
} catch (IOException e) {
Log.i("getLocationInfo IOException", e.toString());
}
JSONObject jsonObject = new JSONObject();
try {
jsonObject = new JSONObject(stringBuilder.toString());
} catch (JSONException e) {
// TODO Auto-generated catch block
Log.i("getLocationInfo JSONException", e.toString());
}
return jsonObject;
}
private static boolean getLatLong(JSONObject jsonObject)
{
try {
longitute = ((JSONArray)jsonObject.get("results")).getJSONObject(0).getJSONObject("geometry").getJSONObject("location").getDouble("lng");
Log.i("Log1", longitute1+"");
latitude = ((JSONArray)jsonObject.get("results")).getJSONObject(0)
.getJSONObject("geometry").getJSONObject("location")
.getDouble("lat");
Log.i("lat1", latitude1+"");
} catch (JSONException e) {
longitute=0;
latitude = 0;
Log.i("getLatLong", e.toString());
return false;
}
return true;
}
或进一步的地理编码需要你:
Geocoder gcd = new Geocoder(context, Locale.getDefault());
List<Address> addresses = gcd.getFromLocation(lat, lng, 1);
if (addresses.size() > 0)
System.out.println(addresses.get(0).getLocality());
希望这会对你有所帮助。
答案 3 :(得分:0)
应编码&#34; youraddress&#34;喜欢
String encodedYourAddress = URLEncoder.encode(youraddress, "UTF-8");
并将被调用的URI构建为
String uri = "http://maps.google.com/maps/api/geocode/json?address="+encodedYourAddress+"&sensor=false";
答案 4 :(得分:0)
您可以将此功能用于
public LatLng getLatLng(Context _context,String locn){
Geocoder geocoder = new Geocoder(_context,Locale.getDefault());
try{
List<Address> addresses = geocoder.getFromLocationName(locn, 5);
if(addresses.size()>0)
{
GeoPoint p=new GeoPoint((int)(addresses.get(0).getLatitude()*1E6),(int)(addresses.get(0).getLongitude()*1E6));
Double plat= (double) (p.getLatitudeE6())/1E6;
Double plon =(double) (p.getLongitudeE6())/1E6;
LatLng lonlat=new LatLng(plat,plon);
}
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return lonlat;
}
只需将当前context
和地址String
传递给LatLng
。上述函数将返回地址的LatLng
值。< / p>
并且,您可以获得相应的纬度和经度值,如下所示
double lat = lonlat.latitude;
double lng = lonlat.longitude;
答案 5 :(得分:0)
我曾经遇到过同样的问题。我用URLEncoder类来计算它。
我认为您应该对搜索字词进行编码并将其放在网址中,
String query = URLEncoder.encode(address, "utf-8");
HttpGet httpGet = new HttpGet(
"http://maps.google.com/maps/api/geocode/json?address="
+ query + "&ka&sensor=false");
根据我的理解,这是最好的解决方案。