我一直在遵循一个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>
发布于 2012-06-30 22:56:09
你可以这样做:
var dest = document.getElementById('myInput').value;
request = {
origin: latlng,
destination: dest,
//...
}
...and然后在您的文档正文中:
<input id='myInput'>
在看到链接后添加:
没有定义hereWeGooo(),因为您在应该有分号的地方使用了逗号- direction.js第57行崩溃。
第54到57行应该是:
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
https://stackoverflow.com/questions/11273655
复制相似问题