android - 如何使用 map View 获取谷歌地图上的当前位置

标签 android

我想知道如何使用 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/

相关文章:

java - 如何在主类之外的新 URL 中使用资源

java - Android - 抽屉导航实现 Activity 更改

android - 从网络链接启动现有的 Android 应用程序

android - 构造函数 TimePickerDialog 未定义

java - FitViewport 无法正确缩放 Libgdx

php - Android App 与 Laravel Action 方法的交互

android - 如何更改 ListView 中的文本颜色

android - ViewStub 在有条件地扩充多个布局时引发错误

android - 如何在android中关闭光标

Android ViewPager 无法加载第三个 fragment