我使用谷歌地图制作了一个计算器,显示出发地和目的地之间的固定价格。
但价格仅在离开输入并点击地图或div后显示。
$(document).ready(function(){
function initMap() {
var latlng = new google.maps.LatLng(52.379189, 4.899431);
var mapOptions = {
center: latlng,
zoom: 11,
mapTypeControl: false,
streetViewControl: false,
styles: [{"featureType":"water","stylers":[{"saturation":43},{"lightness":-11},{"hue":"#0088ff"}]},{"featureType":"road","elementType":"geometry.fill","stylers":[{"hue":"#ff0000"},{"saturation":-100},{"lightness":99}]},{"featureType":"road","elementType":"geometry.stroke","stylers":[{"color":"#808080"},{"lightness":54}]},{"featureType":"landscape.man_made","elementType":"geometry.fill","stylers":[{"color":"#ece2d9"}]},{"featureType":"poi.park","elementType":"geometry.fill","stylers":[{"color":"#ccdca1"}]},{"featureType":"road","elementType":"labels.text.fill","stylers":[{"color":"#767676"}]},{"featureType":"road","elementType":"labels.text.stroke","stylers":[{"color":"#ffffff"}]},{"featureType":"poi","stylers":[{"visibility":"off"}]},{"featureType":"landscape.natural","elementType":"geometry.fill","stylers":[{"visibility":"on"},{"color":"#b8cb93"}]},{"featureType":"poi.park","stylers":[{"visibility":"on"}]},{"featureType":"poi.sports_complex","stylers":[{"visibility":"on"}]},{"featureType":"poi.medical","stylers":[{"visibility":"on"}]},{"featureType":"poi.business","stylers":[{"visibility":"simplified"}]}]
};
map = new google.maps.Map(document.getElementById('map'), mapOptions);
}
function drivingRoute(from, to) {
var request = {
origin: from,
destination: to,
travelMode: google.maps.DirectionsTravelMode.DRIVING,
unitSystem: google.maps.UnitSystem.METRIC
};
if(typeof(drivingLine) !== 'undefined') drivingLine.setMap(null);
directionsService.route(request, function(response, status){
if(status == google.maps.DirectionsStatus.OK){
var totalKM = Math.round(response.routes[0].legs[0].distance.value / 1000);
var distanceText = totalKM+' km';
$('#controls p').text(distanceText);
var stadsdeelTest = document.getElementById('stadsdeel').value;
if (stadsdeelTest=='Centrum') {
var charges= '€ '+ 32;
}
else if (stadsdeelTest=='Amsterdam-Oost') {
var charges= '€ '+ 35;
}
else if (stadsdeelTest=='Oud-Zuid') {
var charges= '€ '+ 30;
}
$('#controls h2').text(charges);
drivingLine = new google.maps.Polyline({
path: response.routes[0].overview_path,
strokeColor: "#b00",
strokeOpacity: .75,
strokeWeight: 5
});
drivingLine.setMap(map);
map.fitBounds(response.routes[0].bounds);
}
else {
$('#controls p').addClass('error');
}
});
}
$('input').blur(function(){
drivingRoute(
$('input[name=from]').val(),
$('input[name=to]').val()
);
});
$(function(){
$("#geocomplete").geocomplete({
details: "form",
types: ["address"],
country: 'nl'
});
$('input').blur(function(){
$("#geocomplete").trigger("geocode");
});
});
var map;
var drivingLine;
var directionsService = new google.maps.DirectionsService();
initMap();
$('input[name=to]').val('Schiphol');
$('input[name=from]').trigger('blur');
});
// Autocomplete
var fromText = document.getElementById('geocomplete');
var cityBounds = new google.maps.LatLngBounds(
new google.maps.LatLng(52.379189, 4.899431));
var options = {
bounds: cityBounds,
types: ['geocode'],
componentRestrictions: {country: 'nl'}
};
// google.maps.event.addListener(autocomplete, 'place_changed', function() {
// window.alert("you selected an item from suggestion list");
// });
$( "input" ).change(function() {
document.getElementById("bestemming").focus();
return false;
});
document.getElementById('geocomplete').onkeypress = function (e) {
if (e.which === 13) {
document.getElementById("bestemming").focus();
return false;
}
};
示例:http://codepen.io/anon/pen/rMEdKO/
尝试将“Damrak,Amsterdam,Nederland”放在出发地并点击地图。它显示了地下世纪Centrum的价格。
将目的地改为“Ceintuurbaan,Amsterdam,Nederland”之后,只有在点击地图后才会改变价格。
我想在填写目的地后立即显示价格,以及用户是否更改目的地。价格必须自动改变。
答案 0 :(得分:0)
而不是使用获取blur
事件,请尝试change
上的input
事件
//This is what you have
$('input').blur(function(){
drivingRoute(
$('input[name=from]').val(),
$('input[name=to]').val()
);
});
//Remove/modify the code above and try this
$('#geocomplete').bind('input', function() {
drivingRoute(
$('input[name=from]').val(),
$('input[name=to]').val()
);
});
不完全确定这是否能解答您的问题/问题。