Android Location服务之LocationManager案例详解

  package com.scott.location;

  import android.content.Context;

  import android.location.Location;

  import android.location.LocationListener;

  import android.location.LocationManager;

  import android.os.Bundle;

  import android.view.View;

  import android.widget.Button;

  import android.widget.ImageView;

  import android.widget.Toast;

  import com.google.android.maps.GeoPoint;

  import com.google.android.maps.MapActivity;

  import com.google.android.maps.MapView;

  import com.google.android.maps.MapView.LayoutParams;

  public class MainActivity extends MapActivity {

  private MapView mapView;

  @Override

  public void onCreate(Bundle savedInstanceState) {

  super.onCreate(savedInstanceState);

  setContentView(R.layout.main);

  mapView = (MapView) findViewById(R.id.mapView);

  mapView.getController().setZoom(17);

  final LocationManager locMgr = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

  //获取缓存中的位置信息

  Location location = locMgr.getLastKnownLocation(LocationManager.GPS_PROVIDER);

  if (location != null) {

  markCurrLocation(location);

  }

  final MyLocationListener listener = new MyLocationListener();

  //注册位置更新监听(最小时间间隔为5秒,最小距离间隔为5米)

  locMgr.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5000, 5, listener);

  Button removeUpdates = (Button) findViewById(R.id.removeUpdates);

  removeUpdates.setOnClickListener(new View.OnClickListener() {

  @Override

  public void onClick(View v) {

  //停止监听

  locMgr.removeUpdates(listener);

  }

  });

  }

  /**

  * 标记当前位置

  * @param location

  */

  private void markCurrLocation(Location location) {

  mapView.removeAllViews(); //清除地图上所有标记视图

  GeoPoint point = new GeoPoint((int) (location.getLatitude() * 1E6), (int) (location.getLongitude() * 1E6));

  mapView.getController().animateTo(point);

  final MapView.LayoutParams params = new MapView.LayoutParams(LayoutParams.WRAP_CONTENT,

  LayoutParams.WRAP_CONTENT, point, LayoutParams.BOTTOM_CENTER);

  final ImageView marker = new ImageView(MainActivity.this);

  marker.setImageResource(R.drawable.marker);

  marker.setOnClickListener(new View.OnClickListener() {

  @Override

  public void onClick(View v) {

  Toast.makeText(getApplicationContext(), "hello, location manager!", Toast.LENGTH_SHORT).show();

  }

  });

  mapView.addView(marker, params);

  }

  @Override

  protected boolean isRouteDisplayed() {

  return false;

  }

  private final class MyLocationListener implements LocationListener {

  @Override

  public void onLocationChanged(Location location) {

  markCurrLocation(location);

  }

  @Override

  public void onStatusChanged(String provider, int status, Bundle extras) {

  //Provider状态在可用、暂不可用、无服务三个状态之间直接切换时触发此函数

  }

  @Override

  public void onProviderEnabled(String provider) {

  //Provider被enable时触发此函数,比如GPS被打开

  }

  @Override

  public void onProviderDisabled(String provider) {

  //Provider被disable时触发此函数,比如GPS被关闭

  }

  }

  }