代碼在這裏。在本地主機,它工作正常。我所做的只是在Bootstrap Modal中獲取用戶位置,然後爲用戶創建路線圖。雖然製作路線谷歌地圖通過JavaScript其不工作在鉻,但其工作在Firefox中
< script src = "https://maps.google.com/maps/api/js?key=***************************&sensor=true" > < /script> < script src = "https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js" > < /script> <script>
function calculateRoute(from, to) {
var myLatLng = {
lat: 26.929307,
lng: 75.884867
};
// Center initialized to HeiwaHeaven
var myOptions = {
zoom: 15,
center: myLatLng,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
// Draw the map
var mapObject = new google.maps.Map(document.getElementById("map"), myOptions);
var directionsService = new google.maps.DirectionsService();
var directionsRequest = {
origin: from,
destination: to,
travelMode: google.maps.DirectionsTravelMode.DRIVING,
unitSystem: google.maps.UnitSystem.METRIC
};
directionsService.route(
directionsRequest,
function(response, status) {
if (status == google.maps.DirectionsStatus.OK) {
new google.maps.DirectionsRenderer({
map: mapObject,
directions: response
});
$('.distance-in-km').text(response.routes[0].legs[0].distance.value/1000 + "km");
alert(response.routes[0].legs[0].distance.value/1000 + "km"); // the distance in metres
} else
$("#error").append("Unable to retrieve your route<br />");
}
);
}
$(document).ready(function() {
// If the browser supports the Geolocation API
if (typeof navigator.geolocation == "undefined") {
$("#error").text("Your browser doesn't support the Geolocation API");
return;
}
navigator.geolocation.getCurrentPosition(function(position) {
var geocoder = new google.maps.Geocoder();
geocoder.geocode({
"location": new google.maps.LatLng(position.coords.latitude, position.coords.longitude)
},
function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
calculateRoute(results[0].formatted_address, "Jamdoli Chauraha To Jaisinghpura Khor Road, Near Keshav Vidyapeeth, Jaipur, Agra Rd, Jaipur");
} else {
var marker = new google.maps.Marker({
position: myLatLng,
title: 'Hello World!'
});
marker.setMap(mapObject);
$("#error").append("Unable to retrieve your address<br />");
}
});
});
calculateRoute($("#from").val(), "Jamdoli Chauraha To Jaisinghpura Khor Road, Near Keshav Vidyapeeth, Jaipur, Agra Rd, Jaipur");
$("#calculate-route").submit(function(event) {
event.preventDefault();
calculateRoute($("#from").val(), "Jamdoli Chauraha To Jaisinghpura Khor Road, Near Keshav Vidyapeeth, Jaipur, Agra Rd, Jaipur");
});
$('.verify-location > a').click(function() {
$('.verify-location').hide();
$('#calculate-route').show();
});
}); < /script>
和HTML是
<button type="button" class="close" data-dismiss="modal">×</button>
<div class="verify-location">Is the your location incorrect? <a>Click here to enter your location manually</a></div>
<form id="calculate-route" name="calculate-route" action="#" method="get">
<label for="from">From:</label>
<input type="text" id="from" name="from" placeholder="An address" size="30" />
<button type="submit">Submit</button>
一旦點擊獲取地圖上的路線按鈕(啓動引導莫代爾),在Firefox中也沒有顯示在地圖上,直到我上,但在點擊鉻它甚至不工作如果我點擊地圖或不。
是否有在控制檯中的任何錯誤?你使用'mapObject'變量的地方超出了範圍。 – Titus
它說傳感器不需要ñ當我使用HTTP而不是HTTPS然後它說無法加載使用HTTPS的服務器我不知道什麼是錯的。 –
@amitchauhan如果有幫助,你能否接受答案! – Danish