简体   繁体   中英

Trying to call a function with an existing eventListener

My knowledge of javascript isnt great, so im not sure exactly how to load this second function from the same event listener.

    var tutsImg = "houseProxy.jpg";
    var kingTuts = new google.maps.LatLng(55.86256, -4.265);
    var tutsDisplay = new google.maps.Marker({
    position: kingTuts,
    map: map,
    icon: tutsImg,
    title:"King Tut's Wah Wah Hut"
    });

    var contentString = '<div id="content">'+
    '<div id="siteNotice">'+
    '</div>'+
    '<h2 id="firstHeading" class="firstHeading">King Tuts Wah Wah Hut</h2>'+
    '<div id="bodyContent">'+
    '</div>'+
    '</div>';

    var infowindow = new google.maps.InfoWindow({
         content: contentString
    });


    // Creating directions
    function calcRoute () {
            var request = {
            origin: 'Glasgow', 
            destination: kingTuts,
            travelMode: google.maps.DirectionsTravelMode.DRIVING
         }

        directionsService.route(request, function(response, status) {
            if (status == google.maps.DirectionsStatus.OK) {

            directionsDisplay.setDirections(response);
            }
        });
        }

    google.maps.event.addListener(tutsDisplay, 'click', function() {
         infowindow.open(map,tutsDisplay);
         calcRoute();
    });

So here i create the variables to display a custom marker on the location of my chosen building. But i also want to call the function which calculates and displays the directions from the users current location to the location of the clicked icon.

any help guys?

I can not fully understand what is the problem. But I write code which will be show direction from user location to the location of clicked on map. For getting user location I using geolocation API (HTML5).

I hope this helps you :)

(function(global) {
    var google = global.google;
    var directionsService = new google.maps.DirectionsService();
    var directionsDisplay;

    // initialize google map
    var initialize = function() {
        var mapOptions = {
            center: new google.maps.LatLng(-34.397, 150.644),
            zoom: 5,
            mapTypeId: google.maps.MapTypeId.ROADMAP
        };

        // initialize map
        var map = new google.maps.Map(document.getElementById("map-canvas"), mapOptions);

        directionsDisplay = new google.maps.DirectionsRenderer();
        directionsDisplay.setMap(map);


        // add event on map click
        // @param {Object} event The custom map event object
        // which contain latitude and langitude
        google.maps.event.addListener(map, 'click', function(event) {
            // getUserPosition is async function
            // because it require user permission
            getUserPosition(calcRoute.bind(global, event.latLng));
        });
    };

    // html 5 geolocation API
    // Get user position
    var getUserPosition = function(callback) {
        global.navigator.geolocation.getCurrentPosition(function(position) {
            var latitude = position.coords.latitude;
            var langitude = position.coords.longitude;
            // convert user position to google.maps
            // LatLng object
            var origin = new google.maps.LatLng(latitude, langitude);

            callback(origin);
        } /*, onError */ );
    };

    // calc direction from user location
    // to position clicked on map
    var calcRoute = function(distPosition, originPosition) {

        var request = {
            origin: originPosition,
            destination: distPosition,
            travelMode: google.maps.TravelMode.DRIVING // it's work onliy for one continent
        };

        directionsService.route(request, function(result, status) {
            if (status === google.maps.DirectionsStatus.OK) {
                directionsDisplay.setDirections(result);
            }
        });
    };

    // events
    google.maps.event.addDomListener(window, 'load', initialize);
}(this));

Link on gist

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM