gpt4 book ai didi

android - 确定用户在 osm map 上的位置

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:12:46 24 4
gpt4 key购买 nike

我正在开发一个确定用户在 osm map 中的位置的 android 应用程序。我能够在 map 上显示用户位置,但如果位置发生变化,整个 map 都会重新加载,这有什么问题?另外如何提高用户位置的准确性?以及如何制作一个根据精度增加和减少的圆圈(如谷歌所示)?

代码:

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) {}

}

最佳答案

您必须使用位置更改监听器也许这可以帮助你

听众:

package dispatch.driver.osmMaps;

import org.osmdroid.util.GeoPoint;

import android.location.Location;
import android.location.LocationListener;
import android.os.Bundle;
import android.widget.Toast;

public class OsmGeoUpdateHandler implements LocationListener
{

private OsmMapsActivity mMapActivity;

public OsmGeoUpdateHandler(OsmMapsActivity aMapActivity)
{
this.mMapActivity = aMapActivity;

}

@Override
public void onLocationChanged(Location location)
{
Toast.makeText(mMapActivity,
"latitude = " + location.getLatitude() * 1e6 + " longitude = " + location.getLongitude() * 1e6,
Toast.LENGTH_SHORT).show();

int latitude = (int) (location.getLatitude() * 1E6);
int longitude = (int) (location.getLongitude() * 1E6);
GeoPoint point = new GeoPoint(latitude, longitude);
mMapActivity.updateCarPosition(point);
}

@Override
public void onProviderDisabled(String provider)
{
// TODO Auto-generated method stub

}

@Override
public void onProviderEnabled(String provider)
{
// TODO Auto-generated method stub

}

@Override
public void onStatusChanged(String provider, int status, Bundle extras)
{
// TODO Auto-generated method stub

}

}

OsmMapActivity类(监听器的使用方法)

public class OsmMapsActivity extends Activity
{
// final private int MAP_DEFAULT_ZOOM = 14;
final private double MAP_DEFAULT_LATITUDE = 44.445883;
final private double MAP_DEFAULT_LONGITUDE = 26.040963;

private MapView mMapView;
private ResourceProxy mResourceProxy;
private OsmMapsItemizedOverlay mItemizedOverlay;
private MyLocationOverlay mMyLocationOverlay;
private LocationManager locationManager;
private OverlayItem lastPosition = null;
private Order mOrder;
OsmGeoUpdateHandler mUpdateHandler;
private ArrayList<OverlayItem> mItems = new ArrayList<OverlayItem>();

@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);

// Specify the XML layout to use:
setContentView(R.layout.osmmap);

mResourceProxy = new DefaultResourceProxyImpl(getApplicationContext());
mMapView = (MapView) findViewById(R.id.mapview);

// Setup the mapView controller:
mMapView.setBuiltInZoomControls(true);
mMapView.setMultiTouchControls(true);
mMapView.setClickable(true);
mMapView.setUseDataConnection(false);
mMapView.setTileSource(TileSourceFactory.MAPNIK);
mMapView.getController().setZoom(12);


/* location manager */
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
mUpdateHandler = new OsmGeoUpdateHandler(this);
Location location = null;

for (String provider : locationManager.getProviders(true))
{
location = locationManager.getLastKnownLocation(provider);
if (location != null)
{
//location.setLatitude(MAP_DEFAULT_LATITUDE);
//location.setLongitude(MAP_DEFAULT_LONGITUDE);
locationManager.requestLocationUpdates(provider, 0, 0, mUpdateHandler);
break;
}
}

//add car position
if (location == null)
{
location = new Location(LocationManager.GPS_PROVIDER);
location.setLatitude(MAP_DEFAULT_LATITUDE);
location.setLongitude(MAP_DEFAULT_LONGITUDE);
updateCarPosition(new GeoPoint(location));
}

} // end onCreate()
public void updateCarPosition(GeoPoint aPoint)
{
if (mItemizedOverlay == null)
{
return;
}
OverlayItem overlayItem;

/* remove last position marker */
removeLastPosition(lastPosition);

overlayItem = new OverlayItem("Center", "Center", (GeoPoint) aPoint);

lastPosition = overlayItem;

mItemizedOverlay.addOverlay(overlayItem);


mMapView.getOverlays().add(mItemizedOverlay);
mMapView.getController().animateTo(aPoint);

}
}

关于android - 确定用户在 osm map 上的位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10575285/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com