简体   繁体   English

Android中的Google Maps Navigation

[英]Google Maps Navigation in Android

I am working with Android and building an app that wants to navigate between 2 points. 我正在使用Android,正在构建一个要在2点之间导航的应用程序。 I already have the points with me. 我已经有了要点。 I have the route between them as well on google maps. 我在谷歌地图上也有它们之间的路线。 I just want to navigate between them. 我只想在它们之间导航。 Could you please help me on this? 您能帮我吗?

Consider you have two locations source, destination, The below code will redirect you to google navigation, Make it as a method and call when ever you want!.. Let me know if you need any other help 考虑到您有两个位置的来源,目的地,下面的代码将您重定向到google导航,将其作为方法并在需要时调用!..让我知道您是否需要其他帮助

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);

You can try to use Directions API . 您可以尝试使用Directions API

The Google Maps Directions API is a service that calculates directions between locations using an HTTP request. Google Maps Directions API是一项使用HTTP请求计算位置之间的方向的服务。

This service is generally designed for calculating directions for static (known in advance) addresses for placement of application content on a map, however this service is not designed to respond in real time to user input. 该服务通常设计用于计算用于将应用程序内容放置在地图上的静态(预先已知)地址的方向,但是该服务并非设计用于实时响应用户输入。

A Google Maps Directions API request takes the following form: Google Maps Directions API请求采用以下形式:

https://maps.googleapis.com/maps/api/directions/output?parameters https://maps.googleapis.com/maps/api/directions/output?参数

where output may be either of the following values: 其中输出可能是以下值之一:

  • json (recommended) indicates output in JavaScript Object Notation (JSON) json(推荐)指示以JavaScript对象表示法(JSON)输出

  • xml indicates output as XML xml表示输出为XML

To access the Google Maps Directions API over HTTP, use: 要通过HTTP访问Google Maps Directions API,请使用:

http://maps.googleapis.com/maps/api/directions/output?parameters http://maps.googleapis.com/maps/api/directions/output?参数

HTTPS is recommended for applications that include sensitive user data, such as a user's location, in requests. 建议将HTTPS用于在请求中包含敏感用户数据(例如用户位置)的应用程序。

You need this parameter in getting the directions: 您需要此参数来获取路线:

  • origin - The address, textual latitude/longitude value, or place ID from which you wish to calculate directions. origin您希望从中计算路线的地址,文本纬度/经度值或地点ID。

  • destination - The address, textual latitude/longitude value, or place ID to which you wish to calculate directions. destination -您要计算路线的地址,文本纬度/经度值或地点ID。 The options for the destination parameter are the same as for the origin parameter, described above. 如上所述,目标参数的选项与原始参数的选项相同。

Check also this tutorial for more information. 另请参阅本教程以获取更多信息。

It's working fine. 一切正常。

 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"));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM