HTML5ジオロケーションのチュートリアルに従っていて、固定の宛先ではなく、宛先アドレスのテキスト入力を持つようにコードを変更したいと思います。このコードでそれを実装するための任意の方法が非常によく来ています。
//これがjavaスクリプトコードです
(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>