How to add GPS using import org.oscim.android.MapView?

454 views
Skip to first unread message

Mariel Basbas

unread,
Jan 26, 2017, 10:29:41 AM1/26/17
to mapsforge-dev
I've tried checking the documentation but it only supports `import org.oscim.map.Map`. How do i add gps in the mapView and get the geoPoint of it?

Mathieu De Brito

unread,
Jan 26, 2017, 11:01:59 AM1/26/17
to mapsforge-dev
Hi Mariel,

In the vtm-android-example, there is an Sample dedicated to this use case : LocationActivity

You can have a look here :

Emux

unread,
Jan 26, 2017, 12:14:42 PM1/26/17
to mapsfo...@googlegroups.com
Usually it's convenient to clone VTM and run the vtm-android-example, studying its code to understand how the library works.
Or even read the examples code directly and see how various aspects of the API are used.

--
Emux

Mariel Basbas

unread,
Feb 2, 2017, 9:17:15 AM2/2/17
to mapsforge-dev
Hello I've tried the LocationActivity.java. It throws a null exception so i changed it to if(location!=null) and if(locationManager!=null) so it wouldn't error but the GPS is not working. There's no location sign on the top of notif tab when the app is running. So it means, it is not using the GPS, how do i fix it?

Emux

unread,
Feb 2, 2017, 9:27:41 AM2/2/17
to mapsfo...@googlegroups.com
You need to provide more details for where and when the NullPointerException(s) happen.

e.g. LocationManager is created on start, shouldn't need any check.
And the location is received in one place in the listener when there is active provider.

--
Emux

Emux

unread,
Feb 2, 2017, 9:29:53 AM2/2/17
to mapsfo...@googlegroups.com
Of course there is need to enable the location provider(s) in Android settings (wifi, gps or both) and then start the app.

--
Emux

Mariel Basbas

unread,
Feb 2, 2017, 9:54:48 AM2/2/17
to mapsforge-dev
I've already set it on the settings but it seems it won't work. Here's the full code:
public class MainActivity extends Activity implements LocationListener{
   
...

   
private LocationLayer locationLayer;
   
private LocationManager locationManager;
   
private final MapPosition mapPosition = new MapPosition();

   
private double startLat;
   
private double startLong;

   
@Override
    protected void onCreate(Bundle savedInstanceState) {
        requestWindowFeature
(Window.FEATURE_NO_TITLE);
       
super.onCreate(savedInstanceState);
        setContentView
(R.layout.main);

       
Tile.SIZE = Tile.calculateTileSize(getResources().getDisplayMetrics().scaledDensity);
       
mapView = new MapView(this);

       
final EditText input = new EditText(this);
        input
.setText(currentArea);
       
boolean greaterOrEqKitkat = Build.VERSION.SDK_INT >= 19;
       
if (greaterOrEqKitkat) {
           
if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
                logUser
("PATH is not usable without an external storage!");
               
return;
           
}
           
mapsFolder = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS),
                   
"/maps/");
       
} else
            mapsFolder = new File(Environment.getExternalStorageDirectory(), "/maps/");

       
if (!mapsFolder.exists())
           
mapsFolder.mkdirs();

       
TextView welcome = (TextView) findViewById(R.id.welcome);
        welcome
.setText("Path Assistance to Hospitals(PATHS)");
        welcome
.setPadding(6, 3, 3, 3);
       
localSpinner = (Spinner) findViewById(R.id.locale_area_spinner);
       
localButton = (Button) findViewById(R.id.locale_button);
       
remoteSpinner = (Spinner) findViewById(R.id.remote_area_spinner);
       
remoteButton = (Button) findViewById(R.id.remote_button);
       
// TODO get user confirmation to download
        // if (AndroidHelper.isFastDownload(this))
        chooseAreaFromRemote();
        chooseAreaFromLocal
();
   
}

   
@Override
    protected void onDestroy() {
       
super.onDestroy();
       
if (hopper != null)
           
hopper.close();

       
hopper = null;
       
// necessary?
        System.gc();

       
// Cleanup VTM
        mapView.map().destroy();
   
}

   
boolean isReady() {
        if (hopper != null) {
            start = null;
           
return true;
       
}

       
if (prepareInProgress) {
            logUser
("Preparation still in progress");
           
return false;
       
}
        logUser
("Error loading files. Please redownload files on the main menu.");
       
return false;
   
}

   
private void initFiles(String area) {
       
...
   
}

   
private void chooseAreaFromLocal() {
       
...
   
}

   
private void chooseAreaFromRemote() {
       
...
   
}

   
private void chooseArea(Button button, final Spinner spinner,
                           
List<String> nameList, final MySpinnerListener myListener) {
       
...
   
}

   
void downloadingFiles() {
       
...
   
}

   
void loadMap(File areaFolder) {
        log
("loading map");
       
// Long press receiver
        mapView.map().layers().add(new LongPressLayer(mapView.map()));

       
...
        loadGraphStorage
();
   
}

   
@Override
    protected void onResume() {
       
super.onResume();

        enableAvailableProviders
();
   
}

   
@Override
    protected void onPause() {
       
super.onPause();

       
if (locationManager != null) {
           
if (ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED
                    || ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED){
               
locationManager.removeUpdates(this);
               
locationLayer.setEnabled(false);
           
}
       
}
   
}

   
@Override
    public void onLocationChanged(Location location) {
       
locationLayer.setEnabled(true);
       
if (location!=null) {
           
locationLayer.setPosition(location.getLatitude(), location.getLongitude(), location.getAccuracy());

           
// Follow location
            mapView.map().getMapPosition(mapPosition);
           
mapPosition.setPosition(location.getLatitude(), location.getLongitude());
           
mapView.map().setMapPosition(mapPosition);

            logUser
("Successful");
       
}
        logUser
("test");
   
}

   
@Override
    public void onProviderDisabled(String provider) {
   
}

   
@Override
    public void onProviderEnabled(String provider) {
   
}

   
@Override
    public void onStatusChanged(String provider, int status, Bundle extras) {
   
}

   
private void enableAvailableProviders() {
       
if (locationManager != null) {
           
if (ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED
                    || ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED){
               
locationManager.removeUpdates(this);
               
for (String provider : locationManager.getProviders(true)) {
                   
if (LocationManager.GPS_PROVIDER.equals(provider)
                           
|| LocationManager.NETWORK_PROVIDER.equals(provider)) {
                       
locationManager.requestLocationUpdates(provider, 0, 0, this);
                   
}
               
}
           
}
       
}
   
}

   
void loadGraphStorage() {
        log
("loading graph");
        getGeoPoints
();
        plotHosp
();

       
...

       
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

       
locationLayer = new LocationLayer(mapView.map());
       
locationLayer.setEnabled(true);
       
mapView.map().layers().add(locationLayer);

       
...
   
}

   
private void finishPrepare() {
       
prepareInProgress = false;
   
}

   
...
}

Emux

unread,
Feb 2, 2017, 10:00:20 AM2/2/17
to mapsfo...@googlegroups.com
See the LocationActivity in VTM samples and try following its implementation.

It's working and can help you debug your code, e.g. see where to initialize LocationManager

--
Emux

Mariel Basbas

unread,
Feb 2, 2017, 10:05:25 AM2/2/17
to mapsforge-dev
Oh, so that's why lol. I just have to declare it on the onCreate lol. Thanks!
Reply all
Reply to author
Forward
0 new messages