我已经创建了一张地图,但是当在手机上时,它并没有移动到我的位置(取自 gps)。为什么?因为我需要检索用户位置,如果我找到它,将地图动画到该点并刷新它(从数据库中获取图钉)。在消极的情况下,我把中心放在米兰。
public class Mappa extends MapActivity {
private MapView mapView;
double latitudine=0;
double longitudine=0;
int kilometri=20;
@Override
protected boolean isRouteDisplayed() {
return false;
}
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mappa);
mapView = (MapView) findViewById(R.id.mapview);
MapController mapController = mapView.getController();
final MyLocationOverlay myLocationOverlay;
mapView.setBuiltInZoomControls(true);
List<Overlay> mapOverlays = mapView.getOverlays();
Drawable drawable = this.getResources().getDrawable(R.drawable.pin_bioscalin);
MyOverlays itemizedoverlay = new MyOverlays(drawable, this);
// milano
latitudine = 45.464164;
longitudine = 9.190321;
GeoPoint mio_point = new GeoPoint((int)(latitudine*1E6), (int)(longitudine*1E6));
mapController.setCenter(mio_point);
mapController.animateTo(mio_point);
mapController.setZoom(11);
// GPS
LocationManager mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
LocationListener mlocListener = new MyLocationListener();
mlocManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 0, 0, mlocListener);
// farmacie
Database_mio db=new Database_mio(this);
db.openDataBase();
Cursor data=db.elenco_negozi_mappa(latitudine,longitudine,kilometri);
data.moveToFirst();
do {
String id_farmacia = data.getString(data
.getColumnIndex("_id"));
String ragione_farmacia = data.getString(data
.getColumnIndex("ragione"));
String indirizzo_farmacia = data.getString(data
.getColumnIndex("ragione"));
int latitude_farmacia = (int) (data.getDouble(data
.getColumnIndex("lat")) * 1E6);
int longitude_farmacia = (int) (data.getDouble(data
.getColumnIndex("lng")) * 1E6);
GeoPoint point = new GeoPoint(latitude_farmacia,longitude_farmacia);
OverlayItem overlayitem = new OverlayItem(point,ragione_farmacia,indirizzo_farmacia);
itemizedoverlay.addOverlay(overlayitem);
} while (data.moveToNext());
mapOverlays.add(itemizedoverlay);
// disclamer
ImageView img = (ImageView) findViewById(R.id.image_mia_map);
img.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent disclamer = new Intent(getParent(), Disclamer.class);
disclamer.putExtra("da_dove", "Mappa");
TabGroupActivity parentActivity = (TabGroupActivity)getParent();
parentActivity.startChildActivity("Disclamer", disclamer);
}
});
}
/* Class My Location Listener */
public class MyLocationListener implements LocationListener{
@Override
public void onLocationChanged(Location loc){
loc.getLatitude();
loc.getLongitude();
String Text = "La mia posizione: "+"Latitudine = "+loc.getLatitude()+"Longitudine = "+loc.getLongitude();
GeoPoint mio_point = new GeoPoint((int)(loc.getLatitude()*1E6), (int)(loc.getLongitude()*1E6));
mapView.getController().setCenter(mio_point);
mapView.getController().animateTo(mio_point);
Toast.makeText( getApplicationContext(),Text,Toast.LENGTH_SHORT).show();
}
@Override
public void onProviderDisabled(String provider){
Toast.makeText( getApplicationContext(),"Gps disabilitato",Toast.LENGTH_SHORT ).show();
}
@Override
public void onProviderEnabled(String provider){
Toast.makeText( getApplicationContext(),"Gps abilitato",Toast.LENGTH_SHORT).show();
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras){
}
}
}
谢谢大家。