2010-08-26 102 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