谷歌地图/ gmap3 - 从用户的地理位置绘制到已知目的地的路线 - 需要帮助 [英] google maps/gmap3 - plotting a route to a known destination from a user's geolocation - help needed

查看:155
本文介绍了谷歌地图/ gmap3 - 从用户的地理位置绘制到已知目的地的路线 - 需要帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个脚本来获取用户的地理位置 - 如果他们启用了该地理位置,并将路线绘制到预定义的目的地。如果他们没有启用地理定位,则应该绘制预定义的位置。该脚本不起作用,但您应该能够通过查看代码来了解我正在尝试做什么。我在正确的轨道上吗?任何人都可以发现它为什么不起作用吗?

 < script type =text / javascript> 
$(function(){

var dest =Unit 20,Tallaght Business Centre,Whitestown Road,Tallaght Business Park,Ireland;

if(geolocEnabled ()){
getLocation();
} else {
plotMarker(dest);
}

//检查是否启用geolocation
函数geolocEnabled(){
return navigator.geolocation;
}

// VRF办公室的绘图标记
函数plotMarker(dest){
$ ('#map')。gmap3(
{action:'addMarker',
address:dest,
map:{
center:true,
zoom:14
},
标记:{
options:{
draggable:false
}
}
}
);
}

// get u ser's location
function getLocation(){
$('#map')。gmap3(
{action:'geoLatLng',
callback:function(latLng){$ b $ (latLng){
plotRoute(latLng,dest);
return;
} else {
alert(无法确定您的位置,启用地理位置服务并再试一次,或查看我们位置的地图。);
plotMarker(dest);
}
}
});
}

// plot route
function plotRoute(latLng,dest){
$('#map')。gmap3(
{action: 'getRoute',
选项:{
origin:latLng,
destination:dest,
travelMode:google.maps.DirectionsTravelMode.DRIVING
},
callback:function(results){
if(!results)return;
$(this).gmap3(
{action:'init',
zoom:7,
mapTypeId:google.maps.MapTypeId.ROADMAP,
streetViewControl:true,
center:[53.337433,-6.2661]
},
{action:'addDirectionsRenderer',
panelID:'directions-panel',
选项:{
preserveViewport:true,
draggable:false,
路线:结果
}
}
);
}
}
);
}

});
< / script>

全部都很有用。

编辑:当我运行脚本时,我甚至没有在浏览器中看到地理位置警告。

编辑:我从getLocation中删除了{timeout:10000},它现在正在获取提醒。脚本更新。

解决方案

geolocating是一个异步过程,结果在 getLocation( )完成。



在<$ c的回调中调用 plotRoute() $ c> $。gmap3.geoLatLng 并提供期望的参数(latLng,dest)

i'm trying to write a script that will get a user's geolocation - if they have it enabled, and plot a route to a predefined destination. if they don't have geolocation enabled, it should just plot the predefined location. the script isn't working, but you should be able to get a good idea of what i'm trying do do by looking through the code. am i on the right track? can anyone spot why it isn't working?

<script type="text/javascript">
       $(function (){

        var dest = "Unit 20, Tallaght Business Centre, Whitestown Road, Tallaght Business Park, Ireland";

        if(geolocEnabled()){
            getLocation();
        }else{
            plotMarker(dest);
        }

        //check if geolocation enabled
        function geolocEnabled(){
            return navigator.geolocation;
        }

        //plot marker for VRF office
        function plotMarker(dest){
            $('#map').gmap3(
              { action: 'addMarker',
                address: dest,
                map:{
                  center: true,
                  zoom: 14
                },
                marker:{
                  options:{
                    draggable: false
                  }
                }
              }
            );
        }

        //get user's location
        function getLocation(){
            $('#map').gmap3(
            { action : 'geoLatLng',
              callback : function(latLng){
                if (latLng){
                  plotRoute(latLng, dest);  
                  return;
                } else {
                  alert("Unable to determine your location. Enable geolocation services and try again, or consult the map for our location.");
                  plotMarker(dest);
                }
              }
            });
          }

        //plot route
        function plotRoute(latLng, dest){
        $('#map').gmap3(
          { action:'getRoute',
            options:{
              origin: latLng,
              destination: dest,
              travelMode: google.maps.DirectionsTravelMode.DRIVING
            },
            callback: function(results){
              if (!results) return;
              $(this).gmap3(
                { action:'init', 
                  zoom: 7, 
                  mapTypeId: google.maps.MapTypeId.ROADMAP, 
                  streetViewControl: true,
                  center: [53.337433,-6.2661]
                },
                { action:'addDirectionsRenderer',
                  panelID: 'directions-panel',
                  options:{
                    preserveViewport: true,
                    draggable: false,
                    directions:results
                  }
                }
              );
            }
          }
        );
      }

    });
    </script>

all help much appreciated.

EDIT: i'm not even getting a geolocation warning in-browser when i run the script.

EDIT: i removed the {timeout: 10000} from getLocation, it's now getting to the alert. script updated.

解决方案

geolocating is an asynchronous process, the result will not be available yet when getLocation() is finished.

Call plotRoute() inside the callback of $.gmap3.geoLatLng and provide the expected arguments(latLng, dest)

这篇关于谷歌地图/ gmap3 - 从用户的地理位置绘制到已知目的地的路线 - 需要帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆