地圖會動的重點,GPS開關重點

張貼日期:2011/5/13 上午 07:35:52

本頁面有MapView時要將Activity改MapActivity

監聽GPS要加入 implements LocationListener

public class fgs2 extends MapActivity implements LocationListener {

int lv = 7;

LocationManager locm = null;

@Override protected void onCreate( Bundle savedInstanceState ) {

super.onCreate( savedInstanceState );

this.locm = (LocationManager)this.getSystemService( Context.LOCATION_SERVICE );

this.locm.requestLocationUpdates( LocationManager.GPS_PROVIDER , 0 , 0 , this );

this.setContentView( R.layout.main );

MapView mv = (MapView)this.findViewById( R.id.mapv );

mv.setBuiltInZoomControls( true );

mv.setSatellite( true );// 使用衛星圖

MapController mc = mv.getController();

//設定Zoom大小和地圖的中心點

mc.setZoom( this.lv );

mc.setCenter( new GeoPoint( 23828194 , 120800899 ) );

}

@Override protected void onPause() { //暫停

if ( this.locm != null ) {

this.locm.removeUpdates( this );

}

super.onPause();

}

@Override protected void onResume() { //恢復暫停

if ( this.locm != null ) {

this.locm.requestLocationUpdates( LocationManager.GPS_PROVIDER , 0 , 0 , this );

}

super.onResume();

}

@Override protected void onDestroy() { //終止前記得關閉GPS監聽

if ( this.locm != null ) {

this.locm.removeUpdates( this );

}

super.onDestroy();

}

@Override public void onLocationChanged( Location loc ) {

MapController mc = ( (MapView)this.findViewById( R.id.mapv ) ).getController();

mc.animateTo( new GeoPoint( (int)( loc.getLatitude() * 1E6 ) , (int)( loc.getLongitude() * 1E6 ) ) );

// mc.setZoom( 17 );

}

@Override public void onProviderDisabled( String provider ) {

}

@Override public void onProviderEnabled( String provider ) {

}

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

}

@Override protected boolean isRouteDisplayed() {

return false;

}

}