1

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);
TikTak
  • 713
  • 1
  • 11
  • 23
  • 1
    Duplicate of [Offline routing and navigation in OSMDroid, How can I get this feature?](https://stackoverflow.com/q/38116384/1340631) and [Graphhopper, osmdroid for osmdroid offline routing](https://stackoverflow.com/q/23021620/1340631) – scai Mar 22 '18 at 12:52
  • @scai, I tried graphhopper, but it doesn't work offline. – TikTak Mar 22 '18 at 13:42
  • 1
    GraphHopper is being used at various places offline. Please elaborate your problem. – scai Mar 22 '18 at 14:40
  • 1
    @TikTak, scai is right. It does work offline if you use it correctly. I used it successfuly in a project I worked on. – Josef Adamcik Mar 22 '18 at 15:03
  • @scai In the description, I specified my code as I used GraphHopper. – TikTak Mar 22 '18 at 15:04
  • @JosefAdamcik Can you throw away an example? – TikTak Mar 22 '18 at 15:06

1 Answers1

1

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

Josef Adamcik
  • 5,620
  • 3
  • 36
  • 42
  • Thank you for your answer. I wanted to ask, is it possible to do it all dynamically? When the Internet is downloaded map that we need and the preparation of the route for offline. – TikTak Apr 01 '18 at 15:19
  • @TikTak Becouse you asked the question in this way, than answer is, yes, it's possible. But there will be obviously a lot of work involved. You would have to build whole service around it. I suppose such task is out of your scope. AFAIK there isn't anybody providing such service for graphopper at the moment. – Josef Adamcik Apr 06 '18 at 14:35