【问题标题】:Google Map Direction issue in cordova ios科尔多瓦ios中的谷歌地图方向问题
【发布时间】:2020-03-11 05:48:27
【问题描述】:

当我点击方向链接时,它会显示从其他地方的方向,而不是显示从当前位置到目的地的方向。

我正在研究 Cordova,在 android 中它显示了从当前位置到目的地的方向,这个问题不存在,但在 iOS 11 版本中我遇到了这个问题,在 iOS 10 版本中它正在工作。

我在下面的 URL 中使用了谷歌地图方向,它会自动填充我当前的位置,但它显示的是其他位置而不是当前位置。

var mapLocationUrl = "https://www.google.com/maps/dir/?api=1&destination=" + lat + "," + long;

window.open(encodeURI(mapLocationUrl), '_system', 'location=yes');

function geocodeAddress() {
    var address = $("#address1").text() + $("#address2").text();
    geocoder.geocode({ address: address }, function (results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
            var p = results[0].geometry.location;
            var lat = p.lat();
            var lng = p.lng();
            datastring = 'compId=' + compid + "&lat=" + lat + "&long=" + lng;
        }
    });
}

Screen shot added for not getting the direction from auto detecting current location instead of that, it pointing some other place

【问题讨论】:

  • 经纬度是怎么计算的,能发一下代码吗?
  • @PrabhjotSinghKainth。它由 gecoder 计算,但 lat 和 long 用于目的地,我使用的代码 - function geocodeAddress() { var address = $("#address1").text() + $("#address2").text() ; geocoder.geocode({address: address}, function(results, status) { if (status == google.maps.GeocoderStatus.OK) { var p = results[0].geometry.location; var lat = p.lat( ); var lng = p.lng(); datastring = 'compId=' + compid + "&lat=" + lat + "&long=" + lng; } }); }

标签: javascript jquery ios google-maps cordova


【解决方案1】:

使用 Javascript 库

<html>
   <head>
      ...    
         ...
         <style>
         #map {
         height: 100%;
                 top: 0px;
             }
         </style>    
             </head>
             <body>
             ...
             <div id="map"></div>
             ...
         <script>
          function initMap() {
         var directionsService = new google.maps.DirectionsService;

                         var directionsDisplay = new google.maps.DirectionsRenderer;

                         map = new google.maps.Map(document.getElementById('map'), {
                             zoom: 10,
                             center: {
                                 lat: 0.0..,
                                 lng: 0.0...
                             },
                             disableDefaultUI: true,
                             mapTypeControl: false
                         });
         map.setTilt(45);
         directionsDisplay.setMap(map);
         calculateAndDisplayRoute(directionsService, directionsDisplay);
         }
         function calculateAndDisplayRoute(directionsService, directionsDisplay) {
                         directionsService.route({
                             origin: startLocation, //String or lat long
                             destination: endLocation, //String or lat long
                             waypoints: waypoints, //[String or lat long]
                             optimizeWaypoints: true, 
                             travelMode: 'DRIVING'
         }, function (response, status) {
         if (status === 'OK') {
         directionsDisplay.setDirections(response);
         } else {
                                 if (status == "ZERO_RESULTS")
                                     window.alert("Wrong address found");
                                 else if (status == "MAX_WAYPOINTS_EXCEEDED")
                                     window.alert("Maximun address entered ");
                                 else
                                     window.alert("Wrong address");
                             }
         });
                     }
      </script>
      <script async defer src="https://maps.googleapis.com/maps/api/js?key=B****&callback=initMap">
      </body>
      <html>

或者直接启动谷歌和苹果地图找方向。

function Dir() {
    var saddr = startLocation;
    var daddr = endLocation;
    //Load Apple Maps
    var url = "http://maps.apple.com/?daddr=" + daddr + "&dirflg=d&t=m"
    //Launch google maps
    var url = "https://www.google.com/maps/dir/?api=1&origin=My+Location&destination=" + daddr + "&travelmode=driving&dir_action=navigate"
    window.location.href = url;
}

更多详情
Apple URL Scheme Reference.

【讨论】:

  • 我尝试使用 origin=Current+Location,它显示从当前位置到目的地的方向,但在地图的顶部,即在源地址字段中,它显示为 Dropped pin(而不是您的位置/我的位置) 我观察到的一件事是,只有当谷歌地图在后台运行并且它只发生在 iOS 11、12、13 版本中时,谷歌地图方向才能正常工作。
  • 在真实设备中 - iPhone 7 (v11.3.1),iPad Retina mini (v13.2)
  • 我添加了屏幕截图,请在我上面的问题中检查它:添加屏幕截图是因为没有从自动检测当前位置而不是那个位置获取方向,它指向其他地方
  • 如果你尝试过以下var url = "https://www.google.com/maps/dir/?api=1&amp;origin=My+Location&amp;destination=" + Destination Address + "&amp;travelmode=driving&amp;dir_action=navigate" window.location.href = url
  • 或者去苹果地图iOS平台=>var url = "http://maps.apple.com/?daddr=" + daddr + "&amp;dirflg=d&amp;t=m"
猜你喜欢
  • 1970-01-01
  • 2020-02-15
  • 2017-12-19
  • 2016-11-21
  • 2015-01-25
  • 2019-01-21
  • 2017-08-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多