2010-08-26 113 views
88

我想在Google地图中显示地址的位置。如何从地址中找到纬度和经度?

如何使用Google Maps API获取地址的经度和纬度?

+0

我有同样的问题,看看我的解决此处 http://stackoverflow.com/a/19170557/2621050 – 2013-10-03 23:01:52

回答

114
public GeoPoint getLocationFromAddress(String strAddress){ 

Geocoder coder = new Geocoder(this); 
List<Address> address; 
GeoPoint p1 = null; 

try { 
    address = coder.getFromLocationName(strAddress,5); 
    if (address==null) { 
     return null; 
    } 
    Address location=address.get(0); 
    location.getLatitude(); 
    location.getLongitude(); 

    p1 = new GeoPoint((double) (location.getLatitude() * 1E6), 
         (double) (location.getLongitude() * 1E6)); 

    return p1; 
    } 
} 

strAddress是一个包含地址的字符串。 address变量保存转换后的地址。

+1

它抛出 – Kandha 2010-08-26 13:02:57

+3

你的“不可java.io.IOException的服务”需要正确的权限才能访问该服务。 # <使用权限android:name =“android.permission.INTERNET”/> – Flo 2010-08-26 14:09:06

+1

您需要连接到互联网才能使用Geocoder。 – Flo 2010-08-26 15:20:17

3

这是如何找到我们在地图上点击的纬度和经度。

public boolean onTouchEvent(MotionEvent event, MapView mapView) 
{ 
    //---when user lifts his finger--- 
    if (event.getAction() == 1) 
    {     
     GeoPoint p = mapView.getProjection().fromPixels(
      (int) event.getX(), 
      (int) event.getY()); 

     Toast.makeText(getBaseContext(), 
      p.getLatitudeE6()/1E6 + "," + 
      p.getLongitudeE6() /1E6 , 
      Toast.LENGTH_SHORT).show(); 
    }        
    return false; 
} 

它运作良好。

要获取位置的地址,我们可以使用地理编码器类。

51

如果你想放置在谷歌地图上你的地址,然后,如果你需要从你的地址获取经纬度长然后使用谷歌将阿比以下简单的方法来使用以下

Intent searchAddress = new Intent(Intent.ACTION_VIEW,Uri.parse("geo:0,0?q="+address)); 
startActivity(searchAddress); 

OR

创建一个方法返回JSONObjectHTTP响应的响应 li柯以下

public 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) { 
     } catch (IOException e) { 
     } 

     JSONObject jsonObject = new JSONObject(); 
     try { 
      jsonObject = new JSONObject(stringBuilder.toString()); 
     } catch (JSONException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 

     return jsonObject; 
    } 

现在传递的JSONObject到getLatLong()方法像下面

public static boolean getLatLong(JSONObject jsonObject) { 

     try { 

      longitute = ((JSONArray)jsonObject.get("results")).getJSONObject(0) 
       .getJSONObject("geometry").getJSONObject("location") 
       .getDouble("lng"); 

      latitude = ((JSONArray)jsonObject.get("results")).getJSONObject(0) 
       .getJSONObject("geometry").getJSONObject("location") 
       .getDouble("lat"); 

     } catch (JSONException e) { 
      return false; 

     } 

     return true; 
    } 

我希望这可以帮助你ND别人.. !! 谢谢.. !!

+0

如何传递这个? – 2013-03-14 07:59:59

+1

不幸的是,这个解决方案不适用于某些移动运营商的移动连接:请求总是返回** OVER_QUERY_LIMIT **。那些移动运营商使用NAT过载,为许多设备分配相同的IP ... – UmbySlipKnot 2013-07-04 13:47:38

+4

好的解决方案!谢谢 – zozelfelfo 2015-01-11 16:29:33

6

下面的代码会为谷歌工作apiv2:

public void convertAddress() { 
    if (address != null && !address.isEmpty()) { 
     try { 
      List<Address> addressList = geoCoder.getFromLocationName(address, 1); 
      if (addressList != null && addressList.size() > 0) { 
       double lat = addressList.get(0).getLatitude(); 
       double lng = addressList.get(0).getLongitude(); 
      } 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } // end catch 
    } // end if 
} // end convertAddress 

其中Address是要转换到经纬度的字符串(123测试路市国邮编)。

1

的回答以上问题Kandha:

它抛出了“java.io.IOException的服务不可用” 我已经给那些许可,包括图书馆......我可以得到地图视图.. 。它抛出IOException异常的地理编码器...

我只是增加了一个捕捉IOException异常的尝试后,它解决了这个问题

catch(IOException ioEx){ 
     return null; 
    } 
54

Ud_an的更新API解决方案

注意LatLng类是Google Play服务的一部分。

强制性

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/> 

<uses-permission android:name="android.permission.INTERNET"/> 

更新:如果你有目标SDK 23及以上的,一定要照顾好运行许可的位置。

public LatLng getLocationFromAddress(Context context,String strAddress) { 

    Geocoder coder = new Geocoder(context); 
    List<Address> address; 
    LatLng p1 = null; 

    try { 
     // May throw an IOException 
     address = coder.getFromLocationName(strAddress, 5); 
     if (address == null) { 
      return null; 
     } 
     Address location = address.get(0); 
     location.getLatitude(); 
     location.getLongitude(); 

     p1 = new LatLng(location.getLatitude(), location.getLongitude()); 

    } catch (IOException ex) { 

     ex.printStackTrace(); 
    } 

    return p1; 
} 
+1

谢谢,它为我工作,以上解决方案无法正常工作。 – 2015-09-09 21:18:36

+0

当实例化地理编码器时,您应该传递上下文地理编码器编码器=新的地理编码器(this);或新的Geocoder(getApplicationContext)而不是getActivity(),如答案中所述。 – 2015-09-22 23:00:13

+1

@Quantumdroid上面的代码写在片段中。否则,你是绝对正确的。这是上下文。 – 2015-09-23 05:56:55

0
Geocoder coder = new Geocoder(this); 
     List<Address> addresses; 
     try { 
      addresses = coder.getFromLocationName(address, 5); 
      if (addresses == null) { 
      } 
      Address location = addresses.get(0); 
      double lat = location.getLatitude(); 
      double lng = location.getLongitude(); 
      Log.i("Lat",""+lat); 
      Log.i("Lng",""+lng); 
      LatLng latLng = new LatLng(lat,lng); 
      MarkerOptions markerOptions = new MarkerOptions(); 
      markerOptions.position(latLng); 
      googleMap.addMarker(markerOptions); 
      googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(latLng,12)); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
+0

空检查没有做任何事情。 – AjahnCharles 2017-09-01 16:08:06

相关问题