我想知道如何使用地图视图在谷歌地图上获得我的当前位置。当我从一个地方走到另一个地方时,我在地图上的位置也会改变。它将实现由全球定位系统或网络供应商,但希望得到我的适当的当前位置。

最佳答案

必须实现location listener以获取纬度和经度数据,使用这些值设置google地图上的位置并使用标记来指示该位置

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()从网络提供程序获取纬度,sames表示经度

10-07 19:36
查看更多