google maps/gmap3 - plotting a route to a known destination from a user's geolocation - help needed

三世轮回 提交于 2019-12-11 02:04:31

问题


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.


回答1:


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)



来源:https://stackoverflow.com/questions/12004744/google-maps-gmap3-plotting-a-route-to-a-known-destination-from-a-users-geoloc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!