Determine user location on osm maps

后端 未结 1 1576
北荒
北荒 2021-02-02 03:57

I am working on an android application that determines user location in osm maps. I am being able to show user location on the map, but if the location change the whole map is r

相关标签:
1条回答
  • 2021-02-02 04:32

    You must use an location change listener Maybe this can help you

    Listener:

    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 class (how to use listener)

    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);
    
        }
    }
    
    0 讨论(0)
提交回复
热议问题