0

这是概率。很简单,但我无法理解。我有一个 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);
  }

帮助感谢感谢

4

3 回答 3

0

嗨,如果你准备好了坐标,那么为什么不google.maps.LatLng通过这种方式创建对象:new google.maps.LatLng(your lat, your lon)如果你想指定,string那么这样做new google.maps.LatLng("address")并分配这个 latlng 对象开始和结束。这将解决您的问题。有关更多详细信息,请访问 Google 的文档。:)

于 2012-05-22T06:52:08.137 回答
0

您可以获取 PHP 变量,将 lat 和 long 传递给 javascript var,然后在 calcRoute 函数中使用这两个 var:

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);
于 2012-05-22T07:14:59.297 回答
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");

如何在 JavaScript 中获取查询字符串值?.

于 2012-05-22T08:07:35.577 回答