Determine user location on osm maps

不想你离开。 提交于 2019-12-02 19:47:22

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);

    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!