我想知道如何使用 map View 在谷歌地图上获取我的当前位置。也就是说,当我步行并从一个位置移动到另一个位置时,我在 map 上的位置也会发生变化。它将通过 GPS 或网络提供商来实现,但想要获得我适当的当前位置。
最佳答案
您必须实现位置监听器来获取纬度和经度数据,使用这些值在谷歌地图上设置位置并使用标记来指示该位置
public class MyLocationListener implements LocationListener {
protected LocationManager lm;
public String message="Hello";
Location location;
private static final long MINIMUM_DISTANCE_CHANGE_FOR_UPDATES = 1;
private static final long MINIMUM_TIME_BETWEEN_UPDATES = 1000000;
Context mContext;
public MyLocationListener(Context mContext) {
// TODO Auto-generated constructor stub
this.mContext = mContext;
String context=Context.LOCATION_SERVICE;
lm = (LocationManager)mContext.getSystemService(context);
lm.requestLocationUpdates(LocationManager.PASSIVE_PROVIDER,MINIMUM_TIME_BETWEEN_UPDATES,MINIMUM_DISTANCE_CHANGE_FOR_UPDATES,this);
}
public void showCurrentLocation()
{
location=lm.getLastKnownLocation(LocationManager.PASSIVE_PROVIDER);
if (location != null) {
message = String.format(
"Longitude: %1$s \n Latitude: %2$s",
location.getLongitude(), location.getLatitude()
);
t1.setText(message);
}
}
public void onLocationChanged(Location location)
{
/* String message = String.format(
"New Location \n Longitude: %1$s \n Latitude: %2$s",
location.getLongitude(), location.getLatitude()
);
t1.setText(message);
Toast.makeText(WinUirep.this,
"Location Changed !",
Toast.LENGTH_LONG).show();*/
}
public void onStatusChanged(String s, int i, Bundle b)
{
}
public void onProviderDisabled(String s)
{
}
public void onProviderEnabled(String s)
{
}}
location.getlatitude() 从网络提供商处获取纬度,经度也是如此
关于android - 如何使用 map View 获取谷歌地图上的当前位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10045754/