Search code examples
androidgpsosmdroid

osmdroid plotting current location offline


I am trying to plot my current gps location and have it move with me. I have downloaded some tiles of a small area to use offline to use offline but I'm not sure how to put my gps point on top of the tiles and have it move with me. Here is my only class, MainActivity.java

import android.app.Activity;
import android.os.Bundle;

import org.osmdroid.api.IMapController;
import org.osmdroid.tileprovider.tilesource.TileSourceFactory;
import org.osmdroid.util.GeoPoint;
import org.osmdroid.views.MapView;

public class MainActivity extends Activity {

public static final GeoPoint SCHOOL = new GeoPoint(33.989820, -81.029123);

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    MapView mapView = (MapView) findViewById(R.id.mapview);
    mapView.setClickable(true);
    mapView.setBuiltInZoomControls(true);
    mapView.setMultiTouchControls(true);
    mapView.setUseDataConnection(false);
    mapView.setTileSource(TileSourceFactory.MAPQUESTOSM);

    IMapController mapViewController = mapView.getController();
    mapViewController.setZoom(16);
    mapViewController.setCenter(SCHOOL);
 }

}

Right now the app just shows the map of the tiles I made.


Solution

  • import java.lang.String;
    
    import android.app.Activity;
    import android.os.Bundle;
    import android.location.Location;
    import android.location.LocationManager;
    import android.location.LocationListener;
    
    import org.osmdroid.api.IMapController;
    import org.osmdroid.tileprovider.tilesource.TileSourceFactory;
    import org.osmdroid.util.GeoPoint;
    import org.osmdroid.views.MapView;
    import org.osmdroid.views.overlay.mylocation.MyLocationNewOverlay;
    import org.osmdroid.tileprovider.constants.OpenStreetMapTileProviderConstants;
    
    public class MainActivity extends Activity {
    
    protected static final String PROVIDER_NAME = LocationManager.GPS_PROVIDER;
    public static final GeoPoint SCHOOL = new GeoPoint(33.989820, -81.029123);
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
        MapView mapView = (MapView) findViewById(R.id.mapview);
        mapView.setClickable(true);
        mapView.setBuiltInZoomControls(true);
        mapView.setMultiTouchControls(true);
        mapView.setUseDataConnection(true);
        mapView.setTileSource(TileSourceFactory.MAPQUESTOSM);
    
        MyLocationNewOverlay myLocationOverlay = new MyLocationNewOverlay(getApplicationContext(), mapView);
        mapView.getOverlays().add(myLocationOverlay);
        myLocationOverlay.enableMyLocation();
        myLocationOverlay.enableFollowLocation();
    
        IMapController mapViewController = mapView.getController();
        mapViewController.setZoom(16);
        mapViewController.setCenter(SCHOOL);
    
    
      }
    
    }
    

    This shows my current gps position. Haven't gotten the chance to make sure that it actually follows me yet but hopefully it does!