Reputation: 7581
My App uses openstreetmap files to show waypoint (or coordinates) on a map.
Based on an Openstreet map file: how can I determine the shortest distance from a GPS coordinate to the (walking) path.
Can I read an OSM (map) or .osm file ... to access the paths?
Upvotes: 0
Views: 1128
Reputation: 7581
Alas, it is not easy to get graphhopper integrated in a mobile app anymore. I had to switch to the brouter lib. Isolate the findtrack code to determine the shortest distance to a path.
Upvotes: 0
Reputation: 7581
The following solution works:
Make a new (maven) project with this pom.xml file:
nl.xyz graphhopper 1.0 system ${project.basedir}/libs/graphhopper-web-1.0-SNAPSHOT.jar
Copy generated graphhopper-web-1.0-SNAPSHOT.jar from the graphhopper project to the libs folder of your new project so it will be included in your path.
Copy the generated graphhopper data to a folder within your small project.
Create this simple demo:
public static void main(String[] args) {
private static String mapsFolder = "graphhopper-data/yourlocation-latest.osm-gh";
GraphHopper graphHopper = new GraphHopper().forMobile();
graphHopper.load(mapsFolder);
System.out.println("Found graph " + graphHopper.getGraphHopperStorage().toString() + ", nodes:" + graphHopper.getGraphHopperStorage().getNodes());
QueryResult queryResult = graphHopper.getLocationIndex().findClosest( 52.11111, 6.111111, EdgeFilter.ALL_EDGES);
double distance = queryResult.getQueryDistance();
System.out.println( "Shortest distance is: " + distance);
}
UPDATE: Using the JAR on Android may give a lot of compiler errors due to library version mismatchers. A better solution for getting this example run at Android, is using this dependency in your build.gradle:
implementation 'com.graphhopper:graphhopper-core:1.0-pre33'
Consequence is that you have to set the default routing profile. You can achieve via this change:
graphHopper = new GraphHopper().forMobile();
// Next line:
graphHopper.setProfiles( Collections.singletonList(new ProfileConfig("my_car").setVehicle("car").setWeighting("fastest")));
graphHopper.load(routingDataFolder);
I hope you enjoy this wonderful 'graphhopper' software!
Upvotes: 0
Reputation: 17375
There are some misconceptions. First, the OSM file is not the map file. The OSM file can be used to create a format to make map rendering fast e.g. mapsforge format does this.
With e.g. GraphHopper you import the OSM file like .pbf or .osm which then creates a graph which can be used for problems you described. Not exactly sure what problem you have though ;)
Upvotes: 1