我想在Google Map 中显示地址的位置 .
如何使用Google Maps API获取地址的纬度和经度?
这是您可以找到我们点击 Map 的纬度和经度的方法 .
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; }
它运作良好 .
要获取位置的地址,我们可以使用地理编码器类 .
以上Kandha问题的答案:
它抛出“java.io.IOException服务不可用”我已经给了这些权限并包含了库...我可以获得 Map 视图...它会在地理编码器中抛出IOException ...
我在尝试后添加了一个catch IOException,它解决了问题
catch(IOException ioEx){ return null; }
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 变量保存转换后的地址 .
strAddress
address
Ud_an的解决方案包含更新的API
Note :LatLng class是Google Play服务的一部分 .
Mandatory :
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/> <uses-permission android:name="android.permission.INTERNET"/>
Update: 如果您有目标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); p1 = new LatLng(location.getLatitude(), location.getLongitude() ); } catch (IOException ex) { ex.printStackTrace(); } return p1; }
如果您想将地址放在谷歌 Map 中,那么可以轻松使用以下内容
Intent searchAddress = new Intent(Intent.ACTION_VIEW,Uri.parse("geo:0,0?q="+address)); startActivity(searchAddress);
要么
如果你需要从你的地址获得lat,那么请使用 Google Place Api
创建一个方法,返回 JSONObject ,其响应为 HTTP Call ,如下所示
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; }
我希望这对你和其他人有帮助.. !!谢谢..!!
以下代码适用于google 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是要转换为LatLng的String(123 Testing Rd City State zip) .
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(); }
7 回答
这是您可以找到我们点击 Map 的纬度和经度的方法 .
它运作良好 .
要获取位置的地址,我们可以使用地理编码器类 .
以上Kandha问题的答案:
我在尝试后添加了一个catch IOException,它解决了问题
strAddress
是包含地址的字符串 .address
变量保存转换后的地址 .Ud_an的解决方案包含更新的API
Note :LatLng class是Google Play服务的一部分 .
Mandatory :
Update: 如果您有目标SDK 23及更高版本,请确保您负责位置的运行时权限 .
如果您想将地址放在谷歌 Map 中,那么可以轻松使用以下内容
要么
如果你需要从你的地址获得lat,那么请使用 Google Place Api
创建一个方法,返回 JSONObject ,其响应为 HTTP Call ,如下所示
现在将 JSONObject 传递给getLatLong()方法,如下所示
我希望这对你和其他人有帮助.. !!谢谢..!!
以下代码适用于google apiv2:
其中address是要转换为LatLng的String(123 Testing Rd City State zip) .