2

我想知道如何使用地图视图获得我在谷歌地图上的当前位置。那是当我步行并从一个位置移动到另一个位置时,我在地图上的位置也会改变。它将通过 GPS 或网络提供商实现,但希望获得我适当的当前位置。

4

3 回答 3

3

您必须实施位置侦听器以获取纬度和经度数据,使用这些值在谷歌地图上设置位置并使用标记来指示该位置

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() 从网络提供商处获取纬度,经度也一样

于 2012-04-06T16:19:19.507 回答
1

我建议以下教程,它们将帮助您构建所需的内容:

http://developer.android.com/resources/tutorials/views/hello-mapview.html http://www.vogella.de/articles/AndroidLocationAPI/article.html

于 2012-04-06T15:46:46.957 回答
0

使用 LocationListener 获取当前位置的纬度经度

public class MyLocationListener implements LocationListener{
    private Context context;

    public MyLocationListener (Context context){

            this.context = context;
            this.frequency = frequency;

            locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);

            criteria = new Criteria();
            criteria.setAccuracy(Criteria.ACCURACY_COARSE);
            criteria.setAltitudeRequired(false);
            criteria.setCostAllowed(true);
            criteria.setSpeedRequired(false);
            criteria.setBearingRequired(false);
            criteria.setPowerRequirement(Criteria.POWER_HIGH);

            provider = locationManager.getBestProvider(criteria, true);

            location = locationManager.getLastKnownLocation(provider);

    }
    public void onLocationChanged(Location location) {
        // TODO Auto-generated method stub
        // here you can get latitute and longitude and send to mapview to show the current location on map
            double latitute = location.getLatitude();
            double longitude = location.getLongitude();
        Toast.makeText(context, "lat,long:"+latitute+","+longitude, Toast.LENGTH_LONG);

    }

}

看教程点击这里阅读教程获取当前纬度和经度

单击此处阅读如何在特定纬度和经度的地图上放置图标

于 2012-04-06T16:18:01.853 回答