如何获取用户在android中输入的位置/地址的gps坐标?
答案 0 :(得分:74)
Geocoder geocoder = new Geocoder(<your context>);
List<Address> addresses;
addresses = geocoder.getFromLocationName(<String address>, 1);
if(addresses.size() > 0) {
double latitude= addresses.get(0).getLatitude();
double longitude= addresses.get(0).getLongitude();
}
答案 1 :(得分:11)
您可以使用Android的Geocoder
进行反向地理编码:
Geocoder geocoder = new Geocoder(this, Locale.getDefault());
List<Address> addresses = geocoder.getFromLocationName(myLocation, 1);
Address address = addresses.get(0);
double longitude = address.getLongitude();
double latitude = address.getLatitude();
还在AndroidManifest.xml
中包含以下内容:
<uses-permission android:name="android.permission.INTERNET"/>
另请注意,您需要使用包含Geocoder
实施的API。包含此功能的API就是Android Google API。您可以使用Geocoder.isPresent()
检查目标API是否存在实施。
查看Geocoder
documentation了解详情。
答案 2 :(得分:3)
List<Address> addresses;
addresses = geocoder.getFromLocationName(<String address>, 1);
if(addresses.size() > 0){
double latitude= addresses.get(0).getLatitude();
double longitude= addresses.get(0).getLongitude();
}
清单权限: -
android.permission.INTERNET
android.permission.ACCESS_COARSE_LOCATION
android.permission.ACCESS_FINE_LOCATION
android.permission.ACCESS_MOCK_LOCATION
答案 3 :(得分:2)
使用清单权限,例如
android.permission.INTERNET
android.permission.ACCESS_COARSE_LOCATION
android.permission.ACCESS_FINE_LOCATION
android.permission.ACCESS_MOCK_LOCATION
然后继续这个
Geocoder geocoder = new Geocoder(this, Locale.getDefault());
List<Address> addresses = geocoder.getFromLocationName(myLocation, 1);
Address address = addresses.get(0);
if(addresses.size() > 0) {
double latitude = addresses.get(0).getLatitude();
double longitude = addresses.get(0).getLongitude();
}
答案 4 :(得分:0)
这是一个完整的样本
首先定义一个缩放
final CameraUpdate zoom = CameraUpdateFactory.zoomTo(5);
清除所有地图标记
mMap.clear();
//Declare a new marker
final MarkerOptions mp = new MarkerOptions();
//declare a EditText to get the user informed address
EditText etEndereco = findViewById(R.id.map_prof_etEnd);
Geocoder geocoder = new Geocoder(map_prof.this);
List<Address> addresses = null;
try {
addresses = geocoder.getFromLocationName(etEndereco.getText().toString(), 1);
} catch (IOException e) {
e.printStackTrace();
}
if (addresses.size() > 0) {
double latitude = addresses.get(0).getLatitude();
double longitude = addresses.get(0).getLongitude();
mp.position(new LatLng(latitude, longitude));
Log.e("teste map2", "inserted latitude " + Latitude + ", inserted Longitude " + Longitude);
mp.title("Selected location");
CameraUpdate center = CameraUpdateFactory.newLatLng(new LatLng(latitude, longitude));
mMap.clear();
mMap.addMarker(mp);
mMap.moveCamera(center);
mMap.animateCamera(zoom);
}