1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Google maps/gmap3 - plotting a route to a known destination from a user's geolocation

Discussion in 'Google API' started by smallmos, Aug 17, 2012.

  1. #1
    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 - it's getting to the alert in getLocation() but that's about it. 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? i'm fairly new to javascript so help would be much appreciated.

    [COLOR=#000000][FONT=Arial]
    [/FONT][/COLOR][COLOR=#800000]<script[/COLOR] [COLOR=#FF0000]type[/COLOR]=[COLOR=#0000FF]"text/javascript"[/COLOR][COLOR=#800000]>[/COLOR]
           $([COLOR=#00008B]function[/COLOR] (){
    
            [COLOR=#00008B]var[/COLOR] dest = [COLOR=#800000]"Tallaght Business Centre, Whitestown Rd, Tallaght Business Park, Dublin 24, Co. Dublin, Ireland"[/COLOR];
    
            [COLOR=#00008B]if[/COLOR](geolocEnabled()){
                getLocation();
                plotRoute();
            }[COLOR=#00008B]else[/COLOR]{
                plotMarker();
            }
    
            [COLOR=gray]//check if geolocation enabled[/COLOR]
            [COLOR=#00008B]function[/COLOR] geolocEnabled(){
                [COLOR=#00008B]return[/COLOR] navigator.geolocation;
            }
    
            [COLOR=gray]//plot marker for VRF office[/COLOR]
            [COLOR=#00008B]function[/COLOR] plotMarker(dest){
                $([COLOR=#800000]'#map'[/COLOR]).gmap3(
                  { action: [COLOR=#800000]'addMarker'[/COLOR],
                    address: dest,
                    map:{
                      center: [COLOR=#00008B]true[/COLOR],
                      zoom: [COLOR=#800000]14[/COLOR]
                    },
                    marker:{
                      options:{
                        draggable: [COLOR=#00008B]false[/COLOR]
                      }
                    }
                  }
                );
            }
    
            [COLOR=gray]//get user's location[/COLOR]
            [COLOR=#00008B]function[/COLOR] getLocation(){
                $([COLOR=#800000]'#map'[/COLOR]).gmap3(
                { action : [COLOR=#800000]'geoLatLng'[/COLOR],
                  callback : [COLOR=#00008B]function[/COLOR](latLng){
                    [COLOR=#00008B]if[/COLOR] (latLng){
                      [COLOR=gray]//var userLocation = latLang; [/COLOR]
                      [COLOR=#00008B]return[/COLOR];
                    } [COLOR=#00008B]else[/COLOR] {
                      alert([COLOR=#800000]"Unable to determine your location. Enable geolocation services\n and try again, or consult the map for out location."[/COLOR]);
                    }
                  }
                });
              }
    
            [COLOR=gray]//plot route[/COLOR]
            [COLOR=#00008B]function[/COLOR] plotRoute(latLng, dest){
            $([COLOR=#800000]'#map'[/COLOR]).gmap3(
              { action:[COLOR=#800000]'getRoute'[/COLOR],
                options:{
                  origin: latLng,
                  destination: dest,
                  travelMode: google.maps.[COLOR=#2B91AF]DirectionsTravelMode[/COLOR].DRIVING
                },
                callback: [COLOR=#00008B]function[/COLOR](results){
                  [COLOR=#00008B]if[/COLOR] (!results) [COLOR=#00008B]return[/COLOR];
                  $([COLOR=#00008B]this[/COLOR]).gmap3(
                    { action:[COLOR=#800000]'init'[/COLOR], 
                      zoom: [COLOR=#800000]13[/COLOR], 
                      mapTypeId: google.maps.[COLOR=#2B91AF]MapTypeId[/COLOR].ROADMAP, 
                      streetViewControl: [COLOR=#00008B]true[/COLOR]
                      [COLOR=gray]//center: [-33.879, 151.235] [/COLOR]
                    },
                    { action:[COLOR=#800000]'addDirectionsRenderer'[/COLOR],
                      options:{
                        panelID: [COLOR=#800000]'directions-panel'[/COLOR],
                        preserveViewport: [COLOR=#00008B]true[/COLOR],
                        draggable: [COLOR=#00008B]false[/COLOR],
                        directions:results
                      }
                    }
                  );
                }
              }
            );
          }
    
          [COLOR=gray]//on click show directions from origin to destination in directions panel[/COLOR]
          $([COLOR=#800000]'#show-directions-button'[/COLOR]).click([COLOR=#00008B]function[/COLOR](){
            $([COLOR=#800000]'#map'[/COLOR]).gmap3({
              action: setDirectionsPanel,
              id: directions-panel
            });
          });
    
        });
        [COLOR=#800000]</script>[/COLOR]
    Code (markup):


     
    smallmos, Aug 17, 2012 IP