这是可能的。非常简单,但我无法得到它。我有一个href链接,例如点击后使用<a href="includes/driving.php?lat=38.254181&long=20.649962" class="driving" >Driving instructions</a>
(使用jQuery)打开一个像这样的googlemap:
jQuery('a.driving').live('click',function(){
jQuery('#content').empty().load(jQuery(this).attr('href'), function(){
initGeolocation();
})
return false;
});
功能initGeolocation();
抓取HTML5位置,页面上有超过1个class="driving"
个链接。我无法实现的是将lat和long变量传递给地图。我需要让他们进入这个功能:
function calcRoute(position) {
var start = '38.313393,20.562651';
var end = 'lat' , 'long'
var request = {
origin:start,
destination:end,
travelMode: google.maps.DirectionsTravelMode.DRIVING
};
directionsService.route(request, function(response, status) {
if (status == google.maps.DirectionsStatus.OK) {
directionsDisplay.setDirections(response);
}
});
}
如果需要,可以像这样(标准代码)
调用地图function success(position) {
var myOptions = {
zoom: 13,
center: new google.maps.LatLng(38.313393, 20.562651),
mapTypeId: google.maps.MapTypeId.ROADMAP
}
map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
directionsDisplay = new google.maps.DirectionsRenderer({
'map': map,
'preserveViewport': true,
'draggable': true
});
directionsDisplay.setPanel(document.getElementById("directions_panel"));
google.maps.event.addListener(directionsDisplay, 'directions_changed',
function() {
if (currentDirections) {
oldDirections.push(currentDirections);
setUndoDisabled(false);
}
currentDirections = directionsDisplay.getDirections();
});
setUndoDisabled(true);
calcRoute(position);
}
帮助表示感谢
答案 0 :(得分:0)
嗨,如果您准备好了坐标,那么为什么不通过这种方式创建google.maps.LatLng
对象:new google.maps.LatLng(your lat, your lon)
,如果要指定string
那么就做这样new google.maps.LatLng("address")
并将此latlng对象指定为开始和结束。这将解决您的问题。有关详细信息,请访问
谷歌的文档。:))
答案 1 :(得分:0)
您可以获取PHP变量,将lat和long传递给javascript变量,然后在calcRoute函数中使用这两个变量:
var lat = "<?= $lat ?>";
var long = "<?= $long ?>";
function calcRoute(lat, long) {
var start = '38.313393,20.562651';
var end = lat , long;
var request = {
origin:start,
destination:end,
travelMode: google.maps.DirectionsTravelMode.DRIVING
};
directionsService.route(request, function(response, status) {
if (status == google.maps.DirectionsStatus.OK) {
directionsDisplay.setDirections(response);
}
});
你可以打电话:
calcRoute(lat, long);
答案 2 :(得分:0)
哦,我明白了。您希望从链接获取lat和long属性,并将它们传递给calcRoute函数。好的,这样做[elem是当前的<A href...>
元素,名称是'lat'或'long':
function getParameterByName(name) {
name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
var regexS = "[\\?&]" + name + "=([^&#]*)";
var regex = new RegExp(regexS);
var results = regex.exec(window.location.search);
if(results == null)
return "";
else
return decodeURIComponent(results[1].replace(/\+/g, " "));
}
var lat=getParameterByName(elem, "lat");
var long=getParameterByName(elem, "long");