简体   繁体   English

如何使用 Android mapview 在模拟器中获取当前位置(纬度和经度)?

[英]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 APIAndroid 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.

相关问题 如何在Android中获取当前位置的经纬度? - How to get latitude and longitude of current location in Android? 如何在android中获取当前位置的经纬度 - How to get the current location latitude and longitude in android Android - 如何获取当前位置(经度和纬度)? - Android - How to get current location (latitude and longitude)? Android : 如何在经度和纬度中获取当前位置? - Android :How to get Current Location in Longitude and latitude? 如何在Android中获取当前位置的经度和纬度 - How to get latitude and longitude of current location in Android 如何在没有模拟器控制的情况下获取当前位置的经纬度? - How to get Current Location Latitude and Longitude without Emulator Control? 使用Android中的后台服务获取当前位置的经度和纬度 - Get current location Latitude and Longitude using background service in android 无法在Android中获取当前位置的经度和纬度 - Not able to get current location latitude and longitude in android 如何在Android中将当前位置作为经度和纬度? - How can I get the current location as a latitude and longitude in Android? 如何获取android移动设备的当前位置经纬度? - how to get the current location latitude and longitude of android mobile device?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM