[英]How to get current location (latitude and longitude) in emulator using Android mapview?
I want to create a MapView application which shows my current location's latitude and longitude.我想创建一个 MapView 应用程序,它显示我当前位置的纬度和经度。 After getting the current location, get the name of the location.获取当前位置后,获取位置名称。 Any help please?请问有什么帮助吗?
With android it is actually pretty easy in order to get the location from the GPS Service.使用 android,从 GPS 服务获取位置实际上非常容易。 Use the LocationManager the easiest way to do it使用LocationManager最简单的方法
locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
customLocationListener = new CustomLocationListener();
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
0,
0,
ll);
.....A Spagehtti CODE GOES HERE..... .....这里有一份意大利面条代码......
class CustomLocationListener implements LocationListener{ ............
public void onLocationChanged(Location argLocation) {
if(location != null) {
int latitude=(int)(argLocation.getLatitude()*1E6);
int longitude=(int)(argLocation.getLongitude()*1E6);
}
} ........ }
might also check Location Android API and Android Development也可以检查Location Android API和Android Development
Geocoder geo = new Geocoder(getApplicationContext(), Locale.getDefault());
List<Address> add;
try
{
add = geo.getFromLocation(
location.getLatitude(),
location.getLongitude(),
1
);
if (add.size() > 0)
{
addl1=add.get(0).getAddressLine(0);
addl2=add.get(0).getAddressLine(1);
addl3=add.get(0).getAddressLine(2);
}
}
you can try this, for getting location name by put it in to the onLocationChanged.你可以试试这个,通过把它放在onLocationChanged中来获取位置名称。
Please refer the link: http://developer.android.com/guide/topics/location/obtaining-user-location.html请参考链接: http : //developer.android.com/guide/topics/location/obtaining-user-location.html
For getting location name, you should use GeoCoding.要获取位置名称,您应该使用 GeoCoding。
In addition to test this using an emulator only you might need to set the location from outside, see: How to emulate GPS location in the Android Emulator?除了仅使用模拟器进行测试外,您可能需要从外部设置位置,请参阅: 如何在 Android 模拟器中模拟 GPS 位置?
Or even use a MockLocationProvider / LocationManager.setTestProvider.. for Unit testing.或者甚至使用 MockLocationProvider / LocationManager.setTestProvider.. 进行单元测试。
Oh and by the way: If you already use a MapView, you might also be interest in using a MyLocationOverlay.顺便说一句:如果您已经使用 MapView,您可能也对使用 MyLocationOverlay 感兴趣。 It will display your current location on the Map.它将在地图上显示您的当前位置。 And by subclassing it, you can also hook the onLocationChanged method to plug in your custom location code that should run once the location changes.通过继承它,您还可以挂钩 onLocationChanged 方法以插入您的自定义位置代码,该代码应在位置更改后运行。
use following code it may help you to find current city使用以下代码可以帮助您找到当前城市
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();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.