回答
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
變量保存轉換後的地址。
這是如何找到我們在地圖上點擊的緯度和經度。
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;
}
它運作良好。
要獲取位置的地址,我們可以使用地理編碼器類。
如果你想放置在谷歌地圖上你的地址,然後,如果你需要從你的地址獲取經緯度長然後使用谷歌將阿比以下簡單的方法來使用以下
Intent searchAddress = new Intent(Intent.ACTION_VIEW,Uri.parse("geo:0,0?q="+address));
startActivity(searchAddress);
OR
創建一個方法返回JSONObject與HTTP響應的響應 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別人.. !! 謝謝.. !!
如何傳遞這個? – 2013-03-14 07:59:59
不幸的是,這個解決方案不適用於某些移動運營商的移動連接:請求總是返回** OVER_QUERY_LIMIT **。那些移動運營商使用NAT過載,爲許多設備分配相同的IP ... – UmbySlipKnot 2013-07-04 13:47:38
好的解決方案!謝謝 – zozelfelfo 2015-01-11 16:29:33
下面的代碼會爲谷歌工作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測試路市國郵編)。
的回答以上問題Kandha:
它拋出了「java.io.IOException的服務不可用」 我已經給那些許可,包括圖書館......我可以得到地圖視圖.. 。它拋出IOException異常的地理編碼器...
我只是增加了一個捕捉IOException異常的嘗試後,它解決了這個問題
catch(IOException ioEx){
return null;
}
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;
}
謝謝,它爲我工作,以上解決方案無法正常工作。 – 2015-09-09 21:18:36
當實例化地理編碼器時,您應該傳遞上下文地理編碼器編碼器=新的地理編碼器(this);或新的Geocoder(getApplicationContext)而不是getActivity(),如答案中所述。 – 2015-09-22 23:00:13
@Quantumdroid上面的代碼寫在片段中。否則,你是絕對正確的。這是上下文。 – 2015-09-23 05:56:55
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();
}
空檢查沒有做任何事情。 – AjahnCharles 2017-09-01 16:08:06
- 1. 如何從asp.net中的地址找到經度和緯度
- 2. 如何從iOS中的經度和緯度查找地址?
- 3. 緯度和經度地址
- 4. 當從地址找到經緯度時,如何給出地址?
- 5. 如何從地址找到緯度經度?
- 6. 從地址返回經度和緯度
- 7. 如何使用緯度和經度在iPhone中查找地址?
- 8. 如何從緯度和經度android得到地址?
- 9. 如何從地理編碼地址找到經緯度?
- 10. 如何從C#中的地址獲取緯度和經度
- 11. 如何從Django GeoIP中的緯度和經度獲取地址?
- 12. 如何找到經度和緯度
- 13. 如何在Google地圖中找到緯度和經度爲一點的地址?
- 14. 如何查找多個地址的經度和緯度
- 15. 獲取緯度,經度從地址
- 16. 店經度和緯度IOS地址簿
- 17. 獲取地址的緯度和經度
- 18. C#轉換地址,經度和緯度
- 19. 如何從地址/位置(經度和緯度)查找地標的圖片
- 20. 如何從Android的緯度和經度獲取地址?
- 21. 如何從地址獲取經度和緯度?
- 22. 如何從r地址獲取緯度和經度?
- 23. 如何從經度和緯度獲取地址?
- 24. 如何從Android上的地址獲取緯度和經度?
- 25. 如何從Android上的地址獲取緯度和經度?
- 26. 如何從地址獲取經度和緯度
- 27. 如何從地址或街道獲得經度和緯度?
- 28. 如何將地址從Firebase轉換爲緯度和經度
- 29. 如何從經度和緯度獲得地址?
- 30. 如何找到任何地方的緯度經度和時區?
我有同樣的問題,看看我的解決此處 http://stackoverflow.com/a/19170557/2621050 – 2013-10-03 23:01:52