TikTak
TikTak

Reputation: 803

Can I implement offline navigation in osmdroid?

I'm developing an app that uses offline maps. Now I need to implement offline navigation in the app. Can I implement offline navigation?

UPD: I tried grapphopper, but it doesn't work offline.

    GeoPoint startPoint = new GeoPoint(41.4670689,69.5824818);
    RoadManager roadManager = new GraphHopperRoadManager("API KEY", true);
    ArrayList<GeoPoint> waypoints = new ArrayList<>();
    waypoints.add(startPoint);
    waypoints.add(new GeoPoint(41.3868364,69.4419728));
    Road road = roadManager.getRoad(waypoints);
    Polyline roadOverlay = RoadManager.buildRoadOverlay(road);
    map.getOverlays().add(roadOverlay);
    map.getController().setCenter(startPoint);

Upvotes: 1

Views: 1171

Answers (1)

Josef Adamcik
Josef Adamcik

Reputation: 5780

You are using support for graphopper from osmbonuspack (GraphHopperRoadManager) which apparently does support only online functionality (it just calls the official Graphopper api).

You'll need to integrate the Graphopper library directly into you android project. And that's not as simple as you may think.

You'll need to:

1) Obtain openstreetmap data for your locality (in pbf format).

You can find sliced parts of the globe on internet. There are tools that will help you to cut out a region you need, if you want to work with some specific area.

2) Prepare data for graphopper

Clone the Graphopper project and import your OSM data

git clone git://github.com/graphhopper/graphhopper.git graphhopper
cd graphhopper
git checkout 0.10.0
./graphhopper.sh import your-area.pbf

This will creat a directory (probably next to your pbf named as {your-area}-gh) with preprocessed graphopper data. You have to include this directory in your app. You can add it to the assets directory or download it at runtime.

Note: There may be additional setup steps for the graphopper project, unfortunatelly I don't remember them (I did this integration 2 years ago). Navigating graphopper documentation can be a bit challenging but they have some community support and there are resources online. It can be done.

3) Include graphopper library into your project

They have an android demo app. So check how the library is integrated and used.

At the time of writing, the dependencies are specified in this way:

    implementation(group: 'com.graphhopper', name: 'graphhopper-core', version: '0.10-SNAPSHOT') {
       exclude group: 'com.google.protobuf', module: 'protobuf-java'
       exclude group: 'org.openstreetmap.osmosis', module: 'osmosis-osm-binary'
       exclude group: 'org.apache.xmlgraphics', module: 'xmlgraphics-commons'
    }
   implementation 'org.slf4j:slf4j-api:1.7.25'
   implementation 'org.slf4j:slf4j-android:1.7.25'

But change '0.10.0-SHAPSHOT' to '0.10'. Also, you should have `jcenter()`` repository added in your project's root gradle file.

Note: versions do matter. Your library in app should have exactly the same version as core project you cloned to generate data. There may be incompatibilities in data format between versions. That's why I included git checkout 0.10.0 in the second step.

4) Use the library

Again, check the example: https://github.com/graphhopper/graphhopper/blob/master/android/app/src/main/java/com/graphhopper/android/MainActivity.java

As It's stated above, your application will need the xyx-gh directory with data.

Graphpoper initialization, you should do it on background GraphHopper hopper = new GraphHopper().forMobile(); hopper.load("yourpathtodirectory-gh");

Usage, again preferably on background: GHRequest ghRequest = new GHRequest(from, to); GHResponse response = hopper.route(ghRequest);

For more information and more troubleshooting check the official documentation: https://github.com/graphhopper/graphhopper/blob/0.10/docs/index.md

Upvotes: 1

Related Questions