价格计算器使用谷歌地图
Price Calculator using google maps
嗨,我有这个价格计算器,它根据距离计算价格。但如果地点在伦敦市中心,我想再加11.50英镑的费用。
使用中的计算器:http://unicornlogistics.com/price-calculator
,下面是js代码:
//global map variable;
$(window).ready(function () {
initialize();
$('#calculateform .submitbtn').on({
click: getPriceAndDistance
});
});
function getPriceAndDistance(event) {
event.preventDefault();
//validating Input
var state = 1;
var form = $('#calculateform');
var validateable = form.find('input');
validateable.each(function () {
elem = $(this);
$(elem).removeClass('invalid');
if (elem.val().length < 2) {
$(elem).addClass('invalid');
state = 0;
}
})
//doing the actual stuff
if(state == 1)
{
var from = form.find('#from').val().trim().replace(/[^a-z0-9's]/gi, '');
var to = form.find('#to').val().trim().replace(/[^a-z0-9's]/gi, '');
var travelMode = form.find('#travelMode option:selected').val();
var travelModeText = form.find('#travelMode option:selected').text();
var price = Number(form.find('#travelMode option:selected').attr('price')).valueOf();
var err = '';
var locationFrom;
var locationTo;
$('#map-canvas').html('');
var mapOptions = {
center: new google.maps.LatLng(55.378051, -3.43597299999999),
zoom: 5,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById("map-canvas"),
mapOptions);
$(form).find('p.msg').remove();
$(form).find('p.loading').remove();
loading = '<p class="loading">Loading Please Wait...<p>';
$(form).append(loading);
//initiate gecoder
geocoder = new google.maps.Geocoder();
if(geocoder)
{
geocoder.geocode({ 'address': from }, function (results, status) {
if (status == google.maps.GeocoderStatus.OK) {
locationFrom = results[0].geometry.location;
if (locationFrom)
{
geocoder.geocode({ 'address': to }, function (results, status) {
if (status == google.maps.GeocoderStatus.OK) {
locationTo = results[0].geometry.location;
if (locationTo) {
updateMap();
}
else {
err = '<p class="invalid msg">Destination location is not found.<p>';
$(form).append(err);
}
}
else {
$(form).find('.loading').remove();
err = '<p class="invalid msg">Geocoding failed due to following reasons ' + status + '.<p>';
$(form).append(err);
}
});
}
else{
err = '<p class="invalid msg">Starting location is not found.<p>';
$(form).append(err);
}
}
else {
//err = '<p class="invalid msg">Geocoding failed due to following reasons ' + status + '.<p>';
err = '<p class="invalid msg">Sorry there seems to be some problem.<p>';
errmsg = '<p class="invalid msg">Please check the location that you entered and try again.<p>';
$(form).append(err);
$(form).append(errmsg);
}
});
function updateMap()
{
latlngCen = new google.maps.LatLng((locationFrom.lat()+locationTo.lat())/2,(locationFrom.lng()+locationTo.lng())/2);
map.panTo(latlngCen);
map.setZoom(1);
directionsService = new google.maps.DirectionsService();
directionsDisplay = new google.maps.DirectionsRenderer();
directionsDisplay.setMap(map);
var req = {
origin:locationFrom,
destination:locationTo
}
if (travelMode == 'driving')
{
req.travelMode = google.maps.DirectionsTravelMode.DRIVING;
}
else if (travelMode == 'transit')
{
req.travelMode = google.maps.DirectionsTravelMode.TRANSIT;
}
directionsService.route(req, function (response, status) {
if (status == google.maps.DirectionsStatus.OK) {
directionsDisplay.setDirections(response);
if (response.routes[0].legs[0].distance) {
console.log('here');
var d = response.routes[0].legs[0].distance.value / 1609;
var estTotal = d * price;
$(form).find('.loading').remove();
dText = "The distance between <strong>" + from + "</strong> and <strong>" + to + "</strong> is <strong>" + d.toFixed(2) + " miles</strong>";
pText = "The estimated price via <strong>" + travelModeText + "</strong> is <strong>£ " + estTotal.toFixed(2) + "</strong>";
$(form).prepend('<p class="valid msg">' + pText + '.</p>');
$(form).prepend('<p class="valid msg">' + dText + '.</p>');
$('html, body').animate({
scrollTop: 240
}, 200);
}
else {
$(form).find('.loading').remove();
err = '<p class="invalid msg">Sorry there seems to be some problem. You can contact us <a href="contact.html">here</a>.</p>'
$(form).append(err);
}
}
});
}
}
}
};
function initialize() {
// declaring map variable;
var mapOptions = {
center: new google.maps.LatLng(55.378051, -3.43597299999999),
zoom: 5,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map;
map = new google.maps.Map(document.getElementById("map-canvas"),
mapOptions);
};
//google.maps.event.addDomListener(window, 'load', initialize);
下面是html代码:
<div id="map-holder">
<div class="container">
<div id="map-canvas"></div>
</div>
</div>
<div id="main">
<div class="container">
<div class="maintext">
<h2>Instant Quote for Same Day deliveries Across UK mainland</h2>
</div>
<div id="calculateform" class="form">
<p>Use the following form to calculate the estimated travel distance and price for your courier.</p>
<p class="instructions">You may search by Postcode,Street and/or City.
<br/>For Example: 10 Downing Street.
</p>
<form>
<p class="label">From</p>
<input id="from" class="" type="text" name="from">
<p class="label">To</p>
<input id="to" class="" type="text" name="to">
<p class="label">Mode of Transport</p>
<select id="travelMode">
<option value="driving" price=".80">Bike/Car</option>
<option value="driving" price=".85">Small Van</option>
<option value="driving" price=".95">Medium Van</option>
<option value="driving" price="1.05">Large Van</option>
<option value="driving" price="1.15">Extra Large Van</option>
</select>
<div class="clearfix"></div>
<input class="submitbtn" type="submit" value="Submit" />
</form>
</div>
</div>
</div>
如果您查询伦敦,geocoder 将始终返回相同的坐标,因此您只需标记和然后比较从查询与保存的的的新的坐标。如果他们匹配,你会增加成本。
相关文章:
- 谷歌地图固定位置覆盖
- 不显示带有本地json文件数据的谷歌地图脚本
- 谷歌地图不是以HTML显示,而是在JS Fiddle上工作
- 谷歌地图标记不会显示
- 无法在JS中显示谷歌地图
- 科尔多瓦页面类应用程序中的多个谷歌地图
- 需要帮助谷歌地图方向面板在FancyBox中显示
- 在谷歌地图上获取事件的x,y坐标
- 谷歌地图API v3不适用于移动浏览器或PhoneGap
- 模拟谷歌地图中的点击
- 如何知道哪个标记被谷歌地图点击了
- 谷歌地图JS API+JSON-多个标记没有显示
- 缓存谷歌地图数据
- 谷歌地图劫持了iphone's滚动(触摸事件)-如何恢复
- 在谷歌地图上绘制位置数据库
- 标记的实时更新,无需加载页面谷歌地图API V3
- 可以't计算自定义谷歌地图的js
- 谷歌地图可以't添加正确的标记ID
- 出租车计算器谷歌地图
- 价格计算器使用谷歌地图