我工作的一个Android应用程序,在OSM的地图来确定用户的位置。 我能够在地图上显示用户的位置,但如果该位置改变整个地图被重新加载,这有什么错呢? 还我怎么能增加用户位置的准确性? 我怎样才能使一个循环,增加和减少根据精度(如在谷歌显示出一个)?
代码:
public class OsmDemoActivity extends Activity implements LocationListener,
MapViewConstants
{
private MapView mMapView;
private MapController mapController;
private LocationManager mLocMgr;
private ItemizedOverlay<OverlayItem> mMyLocationOverlay;
private ResourceProxy mResourceProxy;
ArrayList<OverlayItem> items;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
mResourceProxy = new DefaultResourceProxyImpl(getApplicationContext());
setContentView(R.layout.main);
//mMapView.setUseDataConnection(false);
initilaizeMap();
//addOverlay();
mLocMgr = (LocationManager) getSystemService(LOCATION_SERVICE);
mLocMgr.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
}
public void initilaizeMap()
{
mMapView = (MapView) this.findViewById(R.id.mapView);
mMapView.setTileSource(TileSourceFactory.MAPNIK);
//mMapView.setUseDataConnection(false);
mMapView.setBuiltInZoomControls(true);
mMapView.setMultiTouchControls(true);
mapController = this.mMapView.getController();
mapController.setZoom(15);
mapController.setCenter(new GeoPoint(15.610762,32.540345));
/*
final MyLocationOverlay myLocationOverlay = new MyLocationOverlay(this, mMapView,
mResourceProxy);
myLocationOverlay.enableMyLocation();
//myLocationOverlay.disableMyLocation(); // not on by default
myLocationOverlay.disableCompass();
myLocationOverlay.disableFollowLocation();
myLocationOverlay.setDrawAccuracyEnabled(true);
myLocationOverlay.runOnFirstFix(new Runnable() {
public void run() {
mapController.animateTo(myLocationOverlay
.getMyLocation());
}
});
//ArrayList<OverlayItem> mOsmOverlays;
//mOsmOverlays.add(myLocationOverlay);
*/
}
public void addOverlay()
{
GeoPoint point2 = new GeoPoint(53554070, -2959520); // centre map here
GeoPoint point3 = new GeoPoint(53554070 + 1000, -2959520 + 1000); // icon goes here
GeoPoint point4 = new GeoPoint(15.610844, 32.540045);
GeoPoint point5 = new GeoPoint(15610844 + 40, 32540045 + 40);
GeoPoint point6 = new GeoPoint(15610844 + 50, 32540045 + 50);
GeoPoint point7 = new GeoPoint(15610844 + 10, 32540045 +10);
mapController.setCenter(point4);
items = new ArrayList<OverlayItem>();
// Put overlay icon a little way from map center
items.add(new OverlayItem("Here5", "SampleDescription", point5));
items.add(new OverlayItem("Here6", "SampleDescription", point6));
items.add(new OverlayItem("Here7", "SampleDescription", point7));
/* OnTapListener for the Markers, shows a simple Toast. */
this.mMyLocationOverlay = new ItemizedIconOverlay<OverlayItem>(items,
new ItemizedIconOverlay.OnItemGestureListener<OverlayItem>() {
@Override
public boolean onItemSingleTapUp(final int index,
final OverlayItem item) {
Toast.makeText(
OsmDemoActivity.this,
"Item onItemSingleTapUp '" + item.mTitle, Toast.LENGTH_LONG).show();
return true; // We 'handled' this event.
}
@Override
public boolean onItemLongPress(final int index,
final OverlayItem item) {
Toast.makeText(
OsmDemoActivity.this,
"Item onItemLongPress '" + item.mTitle ,Toast.LENGTH_LONG).show();
return false;
}
}, mResourceProxy);
this.mMapView.getOverlays().add(this.mMyLocationOverlay);
mMapView.invalidate();
}
public void displayLocation(GeoPoint loc)
{
mapController.setCenter(loc);
items = new ArrayList<OverlayItem>();
// Put overlay icon a little way from map center
items.add(new OverlayItem("Here u r", "SampleDescription", loc));
/* OnTapListener for the Markers, shows a simple Toast. */
this.mMyLocationOverlay = new ItemizedIconOverlay<OverlayItem>(items,
new ItemizedIconOverlay.OnItemGestureListener<OverlayItem>() {
@Override
public boolean onItemSingleTapUp(final int index,
final OverlayItem item) {
Toast.makeText(
OsmDemoActivity.this,
"Item onItemSingleTapUp '" + item.mTitle, Toast.LENGTH_LONG).show();
return true; // We 'handled' this event.
}
@Override
public boolean onItemLongPress(final int index,
final OverlayItem item) {
Toast.makeText(
OsmDemoActivity.this,
"Item onItemLongPress '" + item.mTitle ,Toast.LENGTH_LONG).show();
return false;
}
}, mResourceProxy);
mMapView.getOverlays().clear();
this.mMapView.getOverlays().add(this.mMyLocationOverlay);
//mMapView.invalidate();
}
public void onLocationChanged(Location location)
{
int lat = (int) (location.getLatitude() * 1E6);
int lng = (int) (location.getLongitude() * 1E6);
GeoPoint gpt = new GeoPoint(lat, lng);
//mapController.setCenter(gpt);
//mMapView.invalidate();
displayLocation(gpt);
}
@Override
public void onProviderDisabled(String arg0) {}
@Override
public void onProviderEnabled(String provider) {}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {}
}