Kan*_*dha 105 android google-maps google-geocoder
我想在Google地图中显示地址的位置.
如何使用Google Maps API获取地址的纬度和经度?
ud_*_*_an 134
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;
}
}
Run Code Online (Sandbox Code Playgroud)
strAddress是包含地址的字符串.该address变量保存转换后的地址.
Nay*_*pte 73
Ud_an的解决方案包含更新的API
注意:LatLng类是Google Play服务的一部分.
强制性的:
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.INTERNET"/>
Run Code Online (Sandbox Code Playgroud)
更新:如果您有目标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;
}
Run Code Online (Sandbox Code Playgroud)
Nir*_*ngi 51
如果您想将地址放在谷歌地图中,那么可以轻松使用以下内容
Intent searchAddress = new Intent(Intent.ACTION_VIEW,Uri.parse("geo:0,0?q="+address));
startActivity(searchAddress);
Run Code Online (Sandbox Code Playgroud)
要么
如果你需要从你的地址获取经纬度长然后使用谷歌将阿比以下
创建一个方法,返回带有HTTP调用响应的JSONObject,如下所示
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;
}
Run Code Online (Sandbox Code Playgroud)
现在将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;
}
Run Code Online (Sandbox Code Playgroud)
我希望这对你和其他人有帮助.. !! 谢谢..!!
小智 6
以下代码适用于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
Run Code Online (Sandbox Code Playgroud)
其中address是要转换为LatLng的String(123 Testing Rd City State zip).
| 归档时间: |
|
| 查看次数: |
150424 次 |
| 最近记录: |