Java 如何在Android中从字符串地址获取位置坐标?
我有一个带有EditText的活动来输入地址。当用户输入地址并按下保存按钮时,我想从输入的地址获取位置坐标。 使用谷歌地图或地理编码API可以做到这一点吗?我已经检查过了,但没有成功 任何帮助都将不胜感激。试试以下方法:Java 如何在Android中从字符串地址获取位置坐标?,java,android,google-maps,geocoding,google-geocoder,Java,Android,Google Maps,Geocoding,Google Geocoder,我有一个带有EditText的活动来输入地址。当用户输入地址并按下保存按钮时,我想从输入的地址获取位置坐标。 使用谷歌地图或地理编码API可以做到这一点吗?我已经检查过了,但没有成功 任何帮助都将不胜感激。试试以下方法: <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/> <uses-permission android:name="android.permission
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.INTERNET"/>
public LatLng getLocationFromAddress(Context context,String strAddress) {
Geocoder coder = new Geocoder(context);
List<Address> address;
LatLng LatLan= null;
try {
// May throw an IOException
address = coder.getFromLocationName(strAddress, 5);
if (address == null) {
return null;
}
Address location = address.get(0);
LatLan= new LatLng(location.getLatitude(), location.getLongitude() );
} catch (IOException ex) {
ex.printStackTrace();
}
return LatLan;
}
公共LatLng getLocationFromAddress(上下文,字符串){
地理编码器=新地理编码器(上下文);
名单地址;
LatLng LatLan=null;
试一试{
//可能会抛出一个异常
地址=编码者。getFromLocationName(StradAddress,5);
如果(地址==null){
返回null;
}
地址位置=地址。获取(0);
LatLan=新LatLng(location.getLatitude(),location.getLongitude());
}捕获(IOEX异常){
例如printStackTrace();
}
返回拉特兰;
}
以下是所有信息