【问题标题】:Get Lat, Long from address从地址获取经纬度
【发布时间】:2014-06-12 10:06:32
【问题描述】:

我想通过输入地址得到经纬度, 我从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();
    }
}

【问题讨论】:

    标签: android google-maps latitude-longitude


    【解决方案1】:

    您不能将空格传递给 URL。 尝试用%20 替换每个空格,应该没问题。

    String replaced = yourString.replace(" ", "%20");
    

    更多信息:W3Schools

    Characters allowed in a URL

    How do I replace a character in a string in Java?

    【讨论】:

      【解决方案2】:

      使用:

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

      将所有空格替换为 +,类似于 google 的做法。这就是我们在应用中使用的方式。

      【讨论】:

        【解决方案3】:

        应该像“你的地址”一样编码

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

        并将调用的 URI 构建为

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

        【讨论】:

          【解决方案4】:

          您需要从地址任何字段名称表单地址字符串中指定当前城市或位置参考,然后您会得到 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());
          

          希望这会对你有所帮助。

          【讨论】:

          • 你改变了什么? SO的重点是与OP有相同问题的其他人可以重现和修复它。没有解释的固定代码真的很难做到。
          • 您需要从地址任何字段名称表单地址字符串中指定当前城市或位置参考,然后您会得到纬度和经度,例如 Geocoder geocoder = new Geocoder(this, Locale.getDefault()); List
            地址 = geocoder.getFromLocation(lat, lng, 1);
          【解决方案5】:

          您可以使用此功能

          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 值。

          并且,你可以得到对应的纬度和经度值如下

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

          【讨论】:

            【解决方案6】:

            我曾经遇到过同样的问题。我使用 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");
            

            根据我的理解,这是为我工作的最佳解决方案。

            【讨论】:

              猜你喜欢
              • 2013-07-04
              • 2010-12-02
              • 1970-01-01
              • 2012-02-15
              • 1970-01-01
              • 2017-10-19
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多