我在将v2中已经正在运行的方向脚本翻译为v3时遇到问题。 错误是未正确执行的tohere / fromhere函数。
首次单击infowindow打开后,标记会正确显示。 当我点击“tohere”或“fromhere”时,它会抛出错误“tohere 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
答案 0 :(得分:1)
所有代码都是jQuery(document).ready函数的本地代码,因此在运行HTML单击处理程序的全局上下文中不可用。将函数声明移到该函数之外。
看起来你的v2代码来自Mike Williams的v2教程。我移植到v3的This example of his "Getting Directions" map可以帮到你。