2014-06-12 74 views
0

我想通过输入地址来获得的纬度和经度, 下面的代码,我从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(); 
    } 
} 

回答

1

您需要指定地址的当前城市或位置引用任何字段的名称地址,格式字符串,然后你得到的纬度和像

`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()); 

希望这会帮助你。

+0

你有什么改变? SO的要点是,与OP相同问题的其他人可以复制和修复它。使用固定的代码而没有解释这很难做到。 – Bobby

+0

http://developer.android.com/reference/android/location/Geocoder.html请在这里指定 –

+1

您需要指定当前城市或位置的地址引用任何字段名称的表单地址字符串,然后您将得到lat和lng Geocoder geocoder = new Geocoder(this,Locale.getDefault()); 列表

addresses = geocoder.getFromLocation(lat,lng,1); –

0

应该编码 “youraddress” 像

String encodedYourAddress = URLEncoder.encode(youraddress, "UTF-8"); 

,并建立了所谓的URI作为

String uri = "http://maps.google.com/maps/api/geocode/json?address="+encodedYourAddress+"&sensor=false"; 
2

用途:

youraddress = youraddress.replaceAll("\\s", "+"); 
String uri = "http://maps.google.com/maps/api/geocode/json?address=" + 
       youraddress + "&sensor=false"; 

更换所有空格+,类似的方式谷歌确实。这就是我们在应用程序中使用的方式。

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并具有转换为LatLng。以上函数返回地址的LatLng值的地址作为String

而且,你可以得到相应的纬度和经度值,因为一旦如下

double lat = lonlat.latitude; 
double lng = lonlat.longitude; 
0

我有同样的问题。我使用URLEncoder类来解决这个问题。

我想你应该编码您的搜索词,并把它放在URL一样,

String query = URLEncoder.encode(address, "utf-8"); 
HttpGet httpGet = new HttpGet(
       "http://maps.google.com/maps/api/geocode/json?address=" 
         + query + "&ka&sensor=false"); 

这是按照我的理解为我工作的最佳解决方案。