我正在使用Android,并构建一个希望在2点之间导航的应用程序。我已经有了要点。我在谷歌地图上也有它们之间的路线。我只想在它们之间导航。你能帮我一下吗?
考虑你有两个位置source,destination,下面的代码会将你重定向到谷歌导航,将其作为一个方法,并在你想要的时候调用!。。如果你需要其他帮助,请告诉我
Intent intent = new Intent(Intent.ACTION_VIEW,
Uri.parse("http://ditu.google.cn/maps?f=d&source=s_d" +
"&saddr="+source.getLatitude()+
","+source.getLongitude()+"&daddr="+
destination.latitude+
","+destination.longitude+
"&hl=zh&t=m&dirflg=d"));
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK & Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
intent.setClassName("com.google.android.apps.maps","com.google.android.maps.MapsActivity");
startActivityForResult(intent, 1);
您可以尝试使用Directions API。
谷歌地图方向API是一项使用HTTP请求计算位置之间方向的服务。
该服务通常被设计用于计算用于在地图上放置应用程序内容的静态(预先已知)地址的方向,然而该服务不是被设计用于实时响应用户输入。
谷歌地图方向API请求采用以下形式:
https://maps.googleapis.com/maps/api/directions/output?parameters
其中输出可以是以下值之一:
-
json(推荐)表示JavaScript Object Notation(json)中的输出
-
xml表示输出为xml
要通过HTTP访问Google Maps Directions API,请使用:
http://maps.googleapis.com/maps/api/directions/output?parameters
对于在请求中包含敏感用户数据(如用户位置)的应用程序,建议使用HTTPS。
你需要这个参数来获得方向:
-
origin
-您希望从中计算方向的地址、文本纬度/经度值或地点ID。 -
destination
-要计算方向的地址、文本纬度/经度值或地点ID。目标参数的选项与原点参数的选项相同,如上所述。
有关更多信息,请参阅本教程。
运行良好。
String uri = "http://maps.google.com/maps?f=d&hl=en&saddr="+startingLocation.latitude+","+startingLocation.longitude+"&daddr="+destinationLocation.latitude+","+destinationLocation.longitude;
Intent intent = new Intent(android.content.Intent.ACTION_VIEW, Uri.parse(uri));
startActivity(Intent.createChooser(intent, "Select an application"));