admin管理员组文章数量:1327849
(function(){
window.onload =function(){
var directionsDisplay;
var directionsService = new google.maps.DirectionsService();
directionsDisplay = new google.maps.DirectionsRenderer();
document.getElementById("lat").style.visibility = "hidden";
document.getElementById("longi").style.visibility = "hidden";
document.getElementById("number").style.visibility = "hidden";
document.getElementById("addressone").style.visibility = "hidden";
document.getElementById("city").style.visibility = "hidden";
document.getElementById("countie").style.visibility = "hidden";
document.getElementById("postcode").style.visibility = "hidden";
var mapDiv = document.getElementById('map');
var latitude = document.frmOne.lat.value;
var longitude = document.frmOne.longi.value;
var number = document.frmOne.number.value;
var addressone = document.frmOne.addressone.value;
var city = document.frmOne.city.value;
var countie = document.frmOne.countie.value;
var postcode = document.frmOne.postcode.value;
var latlng = new google.maps.LatLng(latitude,longitude);
var options ={
center:latlng,
zoom:18,
mapTypeId:google.maps.MapTypeId.ROADMAP
};
var map= new google.maps.Map(document.getElementById('map'),options);
directionsDisplay.setMap(map);
var marker = new google.maps.Marker({
position: new google.maps.LatLng(latitude,longitude),
map: map,
title: 'Click me'
});
var infowindow = new google.maps.InfoWindow({
content: number+" "+addressone+"<br>"+city+"<br>"+countie+"<br>"+postcode
});
google.maps.event.addListener(marker, 'click', function() {
// Calling the open method of the infoWindow
infowindow.open(map, marker);
});
var start = (latitude,longitude);
var end = "51.403650,-1.323252";
var request = {
origin:start,
destination:end,
travelMode: google.maps.TravelMode.DRIVING
};
directionsService.route(request, function(result, status) {
if (status == google.maps.DirectionsStatus.OK) {
directionsDisplay.setDirections(result);
}
});
};
})();
The longitude and latitude is generated with php and works perfect. The map loads including the marker but the directions do not show.
I can't find a good tutorial on directions so if anyone know of that would also be a help I know my code isn't very tidy but I'm not very good with java script.
(function(){
window.onload =function(){
var directionsDisplay;
var directionsService = new google.maps.DirectionsService();
directionsDisplay = new google.maps.DirectionsRenderer();
document.getElementById("lat").style.visibility = "hidden";
document.getElementById("longi").style.visibility = "hidden";
document.getElementById("number").style.visibility = "hidden";
document.getElementById("addressone").style.visibility = "hidden";
document.getElementById("city").style.visibility = "hidden";
document.getElementById("countie").style.visibility = "hidden";
document.getElementById("postcode").style.visibility = "hidden";
var mapDiv = document.getElementById('map');
var latitude = document.frmOne.lat.value;
var longitude = document.frmOne.longi.value;
var number = document.frmOne.number.value;
var addressone = document.frmOne.addressone.value;
var city = document.frmOne.city.value;
var countie = document.frmOne.countie.value;
var postcode = document.frmOne.postcode.value;
var latlng = new google.maps.LatLng(latitude,longitude);
var options ={
center:latlng,
zoom:18,
mapTypeId:google.maps.MapTypeId.ROADMAP
};
var map= new google.maps.Map(document.getElementById('map'),options);
directionsDisplay.setMap(map);
var marker = new google.maps.Marker({
position: new google.maps.LatLng(latitude,longitude),
map: map,
title: 'Click me'
});
var infowindow = new google.maps.InfoWindow({
content: number+" "+addressone+"<br>"+city+"<br>"+countie+"<br>"+postcode
});
google.maps.event.addListener(marker, 'click', function() {
// Calling the open method of the infoWindow
infowindow.open(map, marker);
});
var start = (latitude,longitude);
var end = "51.403650,-1.323252";
var request = {
origin:start,
destination:end,
travelMode: google.maps.TravelMode.DRIVING
};
directionsService.route(request, function(result, status) {
if (status == google.maps.DirectionsStatus.OK) {
directionsDisplay.setDirections(result);
}
});
};
})();
The longitude and latitude is generated with php and works perfect. The map loads including the marker but the directions do not show.
I can't find a good tutorial on directions so if anyone know of that would also be a help I know my code isn't very tidy but I'm not very good with java script.
Share Improve this question edited Nov 8, 2016 at 11:53 duncan 31.9k15 gold badges81 silver badges101 bronze badges asked Oct 12, 2011 at 14:40 Chris SmithChris Smith 351 silver badge4 bronze badges1 Answer
Reset to default 6You've copied the example from their own reference it looks like to me, which is fine. However they're using place names, and you're using latlng coordinates. What you therefore need to do is use actual latlng objects instead of just "51.403650,-1.323252" and (latitude,longitude).
var request = {
origin:new google.maps.LatLng(latitude,longitude),
destination:new google.maps.LatLng(51.403650,-1.323252),
travelMode: google.maps.TravelMode.DRIVING
};
本文标签: Google Api map with directions using javascriptStack Overflow
版权声明:本文标题:Google Api map with directions using javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742219122a2435117.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论