回答
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