如何使用Android mapview在emulator中获取当前位置(纬度和经度)?
我想创建一个MapView应用程序,显示我当前位置的纬度和经度。获取当前位置后,获取该位置的名称。有什么帮助吗?请参考以下链接:如何使用Android mapview在emulator中获取当前位置(纬度和经度)?,android,Android,我想创建一个MapView应用程序,显示我当前位置的纬度和经度。获取当前位置后,获取该位置的名称。有什么帮助吗?请参考以下链接: 要获取位置名称,您应该使用地理编码。使用android,从GPS服务获取位置实际上相当容易。用最简单的方法来做 locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE); customLocationListener = new CustomLocationListen
要获取位置名称,您应该使用地理编码。使用android,从GPS服务获取位置实际上相当容易。用最简单的方法来做
locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
customLocationListener = new CustomLocationListener();
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
0,
0,
ll);
…这里有一个Spagehtti代码
class CustomLocationListener implements LocationListener{ ............
public void onLocationChanged(Location argLocation) {
if(location != null) {
int latitude=(int)(argLocation.getLatitude()*1E6);
int longitude=(int)(argLocation.getLongitude()*1E6);
}
} ........ }
还可以检查和Geocoder geo=new Geocoder(getApplicationContext(),Locale.getDefault());
列表添加;
尝试
{
add=geo.getFromLocation(
location.getLatitude(),
location.getLongitude(),
1.
);
如果(添加.size()>0)
{
addl1=add.get(0).getAddressLine(0);
addl2=add.get(0).getAddressLine(1);
addl3=add.get(0).getAddressLine(2);
}
}
你可以试试这个,
要通过将位置名称放入onLocationChanged来获取位置名称。除了使用模拟器进行测试外,您可能需要从外部设置位置,请参阅: 甚至可以使用MockLocationProvider/LocationManager.setTestProvider。。用于单元测试
哦,顺便说一下:如果您已经使用了MapView,那么您可能还对使用MyLocationOverlay感兴趣。它将在地图上显示您当前的位置。通过对其子类化,您还可以钩住onLocationChanged方法,插入您的自定义位置代码,该代码在位置更改后应立即运行。使用以下代码,它可以帮助您找到当前城市
Geocoder geocoder=new Geocoder(getBaseContext(),Locale.getDefault());
try
{
String city="";
List<Address> addresses= geocoder.getFromLocation(
geoPoint.getLatitudeE6() / 1E6,
geoPoint.getLongitudeE6() / 1E6,
1
);
if(addresses.size()>0)
{
city+=addresses.get(0).getSubAdminArea();
}
}
catch (IOException e)
{
e.printStackTrace();
}
Geocoder-Geocoder=new-Geocoder(getBaseContext(),Locale.getDefault());
尝试
{
字符串城市=”;
列表地址=geocoder.getFromLocation(
geoPoint.getLatitudeE6()/1E6,
geoPoint.getLongitudeE6()/1E6,
1.
);
如果(地址.size()>0)
{
city+=地址。get(0.getSubAdminArea();
}
}
捕获(IOE异常)
{
e、 printStackTrace();
}
Geocoder geocoder=new Geocoder(getBaseContext(),Locale.getDefault());
try
{
String city="";
List<Address> addresses= geocoder.getFromLocation(
geoPoint.getLatitudeE6() / 1E6,
geoPoint.getLongitudeE6() / 1E6,
1
);
if(addresses.size()>0)
{
city+=addresses.get(0).getSubAdminArea();
}
}
catch (IOException e)
{
e.printStackTrace();
}