如何创build所有移动设备的链接,打开谷歌地图与当前位置开始的路线,命运一个特定的地方?

我宁愿这样做也不会太难找,但是出乎意料的是,像你所期望的那样,find一个很棒的跨设备文章并不容易。

我想创build一个链接,打开移动设备的浏览器和冲浪谷歌地图或打开地图应用程序(苹果地图或谷歌地图),并直接启动一个路线,即:开始在当前位置,结束在给定点纬度/经度)。

我可以在两个设备(除了browserstack),一个Android和一个iPhone上testing。

以下链接仅适用于Android:

<a href="http://maps.google.com/maps?daddr=lat,long&amp;ll=">Take me there!</a> 

点击iPhone的Chrome浏览器中的这个链接,这个奇怪的打开Goog​​le地图的桌面版本与移动应用程序上的广告…

这一个只适用于iOS,打开苹果地图要求我进入一个开始位置(我可以select“当前位置”),并开始路线=所需的行为。 点击此链接在Android上完全失败:

<a href="maps://maps.google.com/maps?daddr=lat,long&amp;ll=">Take me there!</a>

注意maps://协议。

有没有一个优雅的跨设备的方式来创build这样的链接? 一个适用于所有主要手机的链接?

谢谢

更新:find解决scheme(有点)

这是我想出来的。 这不像我想象的那样,虽然它工作。

 var ua = navigator.userAgent.toLowerCase(), plat = navigator.platform, protocol = '', a, href; $.browser.device = ua.match(/android|webos|iphone|ipad|ipod|blackberry|iemobile|opera/i) ? ua.match(/android|webos|iphone|ipad|ipod|blackberry|iemobile|opera/i)[0] : false; if ($.browser.device) { switch($.browser.device) { case 'iphone': case 'ipad': case 'ipod': function iOSversion() { if (/iP(hone|od|ad)/.test(navigator.platform)) { // supports iOS 2.0 and later: <http://bit. ly/TJjs1V> var v = (navigator.appVersion).match(/OS (\d+)_(\d+)_?(\d+)?/); return [parseInt(v[1], 10), parseInt(v[2], 10), parseInt(v[3] || 0, 10)]; } } var ver = iOSversion() || [0]; if (ver[0] >= 6) { protocol = 'maps://'; } else { protocol = 'http://maps.google.com/maps'; } break; case 'android': default: protocol = 'http://maps.google.com/maps'; break; } a.attr('href', protocol + href) 

maps:// protocol是苹果地图应用程序的urlscheme,它只能在iOS 6或更高版本上开始工作。 有许多方法可以testinggmaps是否安装,然后select如何处理这个url,但这对于我的意图来说太过分了。 所以我刚刚创build了一个maps://或maps.google.com/链接,使用上面的参数。

**更新**

可悲的是, $ .browser.device不工作,因为jquery 1.9(来源 – http://api.jquery.com/jquery.browser )

嗯,我还没有用手机工作太多,所以我不知道这是否会工作,但只是从HTML / JavaScript的angular度来看,你可以打开一个不同的url取决于用户的设备是什么?

 <a style="cursor: pointer;" onclick="myNavFunc()">Take me there!</a> function myNavFunc(){ // If it's an iPhone.. if( (navigator.platform.indexOf("iPhone") != -1) || (navigator.platform.indexOf("iPod") != -1) || (navigator.platform.indexOf("iPad") != -1)) window.open("maps://maps.google.com/maps?daddr=lat,long&amp;ll="); else window.open("http://maps.google.com/maps?daddr=lat,long&amp;ll="); } 

有趣的是, http://maps.apple.com链接将直接在iOS设备上的Apple Maps中打开,或者redirect到Google Maps(在Android设备上拦截),这样您就可以制作一个小心的URL在这两种情况下使用“Apple Maps”URL都是正确的:

http://maps.apple.com/?daddr=1600+Amphitheatre+Pkwy,+Mountain+View+CA

或者,您可以直接使用Google地图url(不带/mapsurl组件)在Android设备的Google地图上直接打开,也可以在iOS设备上的Google地图移动networking中打开:

http://maps.google.com/?daddr=1+Infinite+Loop,+Cupertino+CA

刚刚碰到这个问题,发现这里所有的答案,我采取了上述的一些代码,并作出了Android和iPhone(它支持几乎所有的Android和iPhone手机)简单的jsfunction。

  function navigate(lat, lng) { // If it's an iPhone.. if ((navigator.platform.indexOf("iPhone") !== -1) || (navigator.platform.indexOf("iPod") !== -1)) { function iOSversion() { if (/iP(hone|od|ad)/.test(navigator.platform)) { // supports iOS 2.0 and later: <http://bit.ly/TJjs1V> var v = (navigator.appVersion).match(/OS (\d+)_(\d+)_?(\d+)?/); return [parseInt(v[1], 10), parseInt(v[2], 10), parseInt(v[3] || 0, 10)]; } } var ver = iOSversion() || [0]; if (ver[0] >= 6) { protocol = 'maps://'; } else { protocol = 'http://'; } window.location = protocol + 'maps.apple.com/maps?daddr=' + lat + ',' + lng + '&amp;ll='; } else { window.open('http://maps.google.com?daddr=' + lat + ',' + lng + '&amp;ll='); } } 

html:

  <a onclick="navigate(31.046051,34.85161199999993)" >Israel</a> 

这适用于所有设备[iOS,Android和Window Mobile 8.1]。

不以任何方式看起来最好的方式…但不能更简单:)

 <a href="bingmaps:?cp=18.551464~73.951399"> <a href="http://maps.apple.com/maps?q=18.551464, 73.951399"> Open Maps </a> </a> 

http://jsbin.com/dibeq

 if (navigator.geolocation) { //Checks if browser supports geolocation navigator.geolocation.getCurrentPosition(function (position) { var latitude = position.coords.latitude; //users current var longitude = position.coords.longitude; //location var coords = new google.maps.LatLng(latitude, longitude); //Creates variable for map coordinates var directionsService = new google.maps.DirectionsService(); var directionsDisplay = new google.maps.DirectionsRenderer(); var mapOptions = //Sets map options { zoom: 15, //Sets zoom level (0-21) center: coords, //zoom in on users location mapTypeControl: true, //allows you to select map type eg. map or satellite navigationControlOptions: { style: google.maps.NavigationControlStyle.SMALL //sets map controls size eg. zoom }, mapTypeId: google.maps.MapTypeId.ROADMAP //sets type of map Options:ROADMAP, SATELLITE, HYBRID, TERRIAN }; map = new google.maps.Map( /*creates Map variable*/ document.getElementById("map"), mapOptions /*Creates a new map using the passed optional parameters in the mapOptions parameter.*/); directionsDisplay.setMap(map); directionsDisplay.setPanel(document.getElementById('panel')); var request = { origin: coords, destination: 'BT42 1FL', travelMode: google.maps.DirectionsTravelMode.DRIVING }; directionsService.route(request, function (response, status) { if (status == google.maps.DirectionsStatus.OK) { directionsDisplay.setDirections(response); } }); }); } 

不,从iOS开发人员的angular度来看,我知道有两个链接会打开iPhone上的地图应用程序

在iOS 5及更低版本上: http://maps.apple.com?q=xxxx : http://maps.apple.com?q=xxxx

在iOS 6及更高版本中: http://maps.google.com?q=xxxx : http://maps.google.com?q=xxxx

这只是在Safari上。 Chrome会将您引导至Google地图网页。

除此之外,您将需要使用基本上击败目的的URLscheme,因为没有android会知道该协议。

您可能想知道,为什么Safari会打开地图应用程序,Chrome浏览器会将我引导至网页?

那么,因为safari是由苹果公司制造的浏览器的内置,可以检测到上面的URL。 Chrome只是“另一个应用程序”,必须遵守iOS生态系统。 因此,与其他应用程序通信的唯一方法是使用URLscheme。

我发现这是全面的工作:

<a href="https://www.google.com/maps/place/1+Fake+Street,+City+Province/State>Get Directions</a>

对于台式机/笔记本电脑,用户必须在地图加载时单击路线,但是从我的testing中,所有移动设备都将毫无困难地加载Google地图应用中的链接。

基于文档的origin参数是可选的,它默认为用户的位置。

... Defaults to most relevant starting location, such as user location, if available. If none, the resulting map may provide a blank form to allow a user to enter the origin....

例如: https://www.google.com/maps/dir/?api=1&destination=Pike+Place+Market+Seattle+WA&travelmode=bicycling : https://www.google.com/maps/dir/?api=1&destination=Pike+Place+Market+Seattle+WA&travelmode=bicycling

对我来说,这可以在桌面,IOS和Android上运行。