本文主要记录在运行graphhopper安卓版路径规划导航源码的步骤和遇到的问题。
成功运行了程序,但是路劲规划一直不成功,问题一开始是服务地址,后来又是key的问题,在这个项目中涉及到了graphhopper、mapbox、mapilion的key,mapbox带导航的key我一直无法获取。目前最大的问题:我无法注册mapbox,求一个带导航功能的测试key
项目遇到了瓶颈-_-有在搞或者搞过graphhopper安卓版的童鞋指导一下,**谢谢!**😀
1、源码下载
graphhopper-Android源码:graphhopper-navigation-example
2、申请Key
- graphHopper的key,进入官网并登录:GraphHopper官网地址
- mapilion的key:没有登录上去,自己扒一下Mapilion地址
- mapbox-key:mapbox的key我一直无法登录;找了个方法MapBox账号注册(重大更新) ,还没测试。🤔
代码中显式的是mpabox_access_token
,而md说明文档中又说是graphhopper的key,给我整蒙了,目前还没解决,最主要的问题还是无法获取Mapbox带导航功能的key。
3、运行程序调试代码
更改了developer-config.xml
中的请求地址base_url
,添加了相关的key,如果不设置,会遇到如下问题:
问题:请求地址无效,404错误
- 问题日志
onFailure: navigation.getRoute() java.net.SocketTimeoutException:
failed to connect to graphhopper.com/108.160.172.232 (port 443) from /192.168.100.100 (port 42782) after 10000ms
在graphhopper官网找到api相关的内容,graphhopper没有严格区分js-api、Android-api,更改base_url测试结果如下
- 原请求:
https://graphhopper.com/api/1/navigate?point=51.131,12.414&point=48.224,3.867&profile=car&locale=de&calc_points=false&key=88c4066a-c52c-4366-88c9-675bc5db4597
- 原响应
{
"message": "HTTP 404 Not Found"
}
- 新请求
https://graphhopper.com/api/1/route?point=51.131,12.414&point=48.224,3.867&profile=car&locale=de&calc_points=false&key=88c4066a-c52c-4366-88c9-675bc5db4597
- 新响应
{
"hints": {
"visited_nodes.sum": 4783,
"visited_nodes.average": 4783
},
"info": {
"copyrights": [
"GraphHopper",
"OpenStreetMap contributors"
],
"took": 27,
"road_data_timestamp": "2024-07-20T02:00:00Z"
},
"paths": [
{
"distance": 883834.768,
"weight": 42799.118711,
"time": 29541602,
"transfers": 0,
"points_encoded": true,
"points_encoded_multiplier": 100000,
"snapped_waypoints": "ivawHebxjAz_wPlyds@"
}
]
}
配置文件
重点在于:base_url:https://graphhopper.com/api/1/route/
;源码下载下来的base_url:https://graphhopper.com/api/1/navigate/
无法访问,可以自行测试一下
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="base_url" translatable="false">https://graphhopper.com/api/1/route/</string> <!-- Your GraphHopper API key -->
<!-- <string name="mapbox_access_token" translatable="false">pk.88c4066a-c52c-4366-88c9-675bc5db4597</string>-->
<!-- <string name="mapbox_access_token" translatable="false">pk.eyJ1IjoiYW5kcm9pZHNoYWtpbCIsImEiOiJja2k0bDMzcjcwMGZ5MnBuMml5ZGtyeDd6In0.cxt1NubVuc6yVE5CdS6iUQ</string>-->
<string name="mapbox_access_token" translatable="false">pk.eyJ1Ijoic2hhZG93aGlqYWNrZXJzIiwiYSI6ImNrOXMxcW8yZjA1amgzZXBjemdhdXF6cTMifQ.A5ibSD0Tf9rqo3mpBVo19Q</string>
<!-- The "day" style used for navigation, here mapilion is used but you can use maptiler too -->
<string name="map_style_light" translatable="false">https://tiles.mapilion.com/assets/osm-bright/style.json?key=3d92bc05-5f36-421b-8457-18bbf96540b7</string>
<!-- The "night" style used for navigation -->
<string name="map_style_dark" translatable="false">https://tiles.mapilion.com/assets/dark-matter/style.json?key=3d92bc05-5f36-421b-8457-18bbf96540b7</string>
</resources>