简体   繁体   中英

Google map HTML5 Geolocation

I have been following a HTML5 geolocation tutorial and want to modify the code to have a text input for destination address, instead of a fixed destination. Any method to implement it in this code is very well come.

//here is the java script code

(function(geolocation){

  if (geolocation) return;

  var cache;

  geolocation = window.navigator.geolocation = {};
  geolocation.watchPosition = function(callback){

    if (cache) callback(cache);

    $.getScript('//www.google.com/jsapi',function(){

      cache = {
        coords : {
          "latitude": google.loader.ClientLocation.latitude, 
          "longitude": google.loader.ClientLocation.longitude
        }
      };

      callback(cache);
    });

  };


})(navigator.geolocation);

// Proceed as usual.
(function() {
    if ( navigator.geolocation ) {
    var coords = navigator.geolocation.watchPosition( hereWeGooo,
        function() {
            // failure
                document.body.innerHTML = 'Sorry. We can\'t get your current location.';
            },
            { enableHighAccuracy: true }
        );

    }
function hereWeGooo(coords) {
    coords = coords.coords;
    var latlng = new google.maps.LatLng(coords.latitude, coords.longitude),
        myOptions = {
        //zoom: 15,
        //center: latlng,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    },
    map = new google.maps.Map(document.querySelector( "#map"), myOptions),
      directionsDisplay = new google.maps.DirectionsRenderer(),
      directionsService = new google.maps.DirectionsService(),

        var dest = document.getElementById('d').value;

    request = {

        origin: latlng,
        destination: dest, 
        // replace with your own airport
        travelMode: google.maps.DirectionsTravelMode.DRIVING
    };

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

  <form>
    <p><input type="text" id="d" /></p>
    <input type="button" value="Go" onClick="hereWeGooo()">
    </form>

You could do it like:

var dest = document.getElementById('myInput').value;

request = {
    origin: latlng,
    destination: dest, 
//...
}

...and then in the body of your document:

<input id='myInput'>

Added after seeing the link:

hereWeGooo() is not defined because you're using commas where there should be semi-colons --- direction.js line 57 crashes.

Lines 54 to 57 should be:

map = new google.maps.Map(document.querySelector( "#map"), myOptions); //<----- semi-colon, not comma
        directionsDisplay = new google.maps.DirectionsRenderer(); //<----- semi-colon, not comma
        directionsService = new google.maps.DirectionsService(); //<----- semi-colon, not comma

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