0

我在将 v2 中已经工作的方向脚本翻译成 v3 时遇到问题。错误是未正确执行的 tohere/fromhere 函数。

标记显示正确,第一次单击后打开信息窗口。当我然后单击“tohere”或“fromhere”时,它会引发错误“tohere is not defined”...

任何提示我做错了什么?

jQuery(document).ready(function () {

if (jQuery('#map_canvas').length) {
initActive();
}


function initialize(lat, long,html) {
var latid = lat;
var longit = long;
var myLatlng = new google.maps.LatLng(latid, longit);
//var myLatlng = new google.maps.LatLng(51.512098,-0.137692);
var myOptions = {
    zoom:17,
    navigationControl:true,
    mapTypeControl:false,
    scaleControl:false,
    scrollwheel:false,
    disableDefaultUI:true,
    disableDoubleClickZoom:true,
    center:myLatlng,
    mapTypeId:google.maps.MapTypeId.ROADMAP
}
var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
var styles = [
    {
        stylers:[

            { saturation:-100 }
        ]
    }
];

map.setOptions({styles:styles});

var myLatLng = new google.maps.LatLng(latid, longit);
var marker = createMarker(map,myLatLng,html);
}

function initActive() {
var flat = jQuery('#map_canvas').data('coord-lat');
var flong = jQuery('#map_canvas').data('coord-lon');
var title = jQuery('#map_canvas').data('content');
initialize(flat, flong,title);
}   


function createMarker(map, latlng,html) {
var marker = new google.maps.Marker({
    position: latlng,
    map: map
    });

var infowindow = new google.maps.InfoWindow({ size: new google.maps.Size(150,50)});
marker.setIcon("mapIcons/marker_red.png");




    var gmarkers = []; 
    var to_htmls = [];
    var from_htmls = [];
    var htmls = [];
    var i = gmarkers.length;

    // The info window version with the "to here" form open
    to_htmls[i] = html + '<br>Directions: <b>To here<\/b> - <a href="javascript:fromhere(' + i + ')">From here<\/a>' +
       '<br>Start address:<form action="http://maps.google.com/maps" method="get" target="_blank">' +
       '<input type="text" SIZE=40 MAXLENGTH=40 name="saddr" id="saddr" value="" /><br>' +
       '<INPUT value="Get Directions" TYPE="SUBMIT">' +
       '<input type="hidden" name="daddr" value="' + latlng.lat() + ',' + latlng.lng() + 
              // "(" + name + ")" + 
       '"/>';
    // The info window version with the "to here" form open
    from_htmls[i] = html + '<br>Directions: <a href="javascript:tohere(' + i + ')">To here<\/a> - <b>From here<\/b>' +
       '<br>End address:<form action="http://maps.google.com/maps" method="get"" target="_blank">' +
       '<input type="text" SIZE=40 MAXLENGTH=40 name="daddr" id="daddr" value="" /><br>' +
       '<INPUT value="Get Directions" TYPE="SUBMIT">' +
       '<input type="hidden" name="saddr" value="' + latlng.lat() + ',' + latlng.lng() +
              // "(" + name + ")" + 
       '"/>';
    // The inactive version of the direction info
    html = html + '<br>Directions: <a href="javascript:tohere('+i+')">To here<\/a> - <a href="javascript:fromhere('+i+')">From here<\/a>';
    var contentString = html;

    google.maps.event.addListener(marker, 'click', function() {
    infowindow.setContent(contentString); 
    infowindow.open(map,marker);
    });

    gmarkers.push(marker);
    htmls[i] = html;
}


// functions that open the directions forms
function tohere(i) {
 infowindow.setContent(to_htmls[i]); 
}
function fromhere(i) {
  infowindow.setContent(from_htmls[i]); 
}


}); //documentready close
4

1 回答 1

1

您的所有代码都是 jQuery(document).ready 函数的本地代码,因此在运行 HTML 单击处理程序的全局上下文中不可用。将函数声明移到该函数之外。

看起来您的 v2 代码来自 Mike Williams 的 v2 教程。 我移植到 v3的他的“Getting Directions”地图示例可能会对您有所帮助。

于 2012-12-06T13:25:34.550 回答