Javascript 此.setMap()在自定义覆盖中不起作用
我正在为谷歌地图编写一个自定义覆盖图。我有一个很长的点,我想用箭头指向和从他们的图像严重。我遵循谷歌的自定义覆盖教程(https://developers.google.com/maps/documentation/javascript/overlays#CustomOverlays) 在我的覆盖的“构造函数”中,此行失败: 'this.setMap(this.map);' 我需要做什么才能让它工作 这是我的叠加图的“构造器”。虚线在底部。当我测试时,所有对“警报”的调用都在那里。未到达最后一个调用,因此我认为“this.setMap()”不起作用Javascript 此.setMap()在自定义覆盖中不起作用,javascript,google-maps-api-3,Javascript,Google Maps Api 3,我正在为谷歌地图编写一个自定义覆盖图。我有一个很长的点,我想用箭头指向和从他们的图像严重。我遵循谷歌的自定义覆盖教程(https://developers.google.com/maps/documentation/javascript/overlays#CustomOverlays) 在我的覆盖的“构造函数”中,此行失败: 'this.setMap(this.map);' 我需要做什么才能让它工作 这是我的叠加图的“构造器”。虚线在底部。当我测试时,所有对“警报”的调用都在那里。未到达最后一个
//This serves as a constructor for a link overlay
function LinkOverlay(startNodeCoordinates, endNodeCoordinates, map)
{
alert("constructor start");
this.map_ = map;
//These are the lat-long coordinates of where the link starts and ends
this.startNodeCoordinates_ = startNodeCoordinates;
this.endNodeCoordinates_ = endNodeCoordinates;
alert("constructor coordinates stored");
// We define a property to hold the image's
// div. We'll actually create this div
// upon receipt of the add() method so we'll
// leave it null for now.
this.div_ = null;
alert("constructor div saved");
//We need to know if we draw the arrow up or down and left or right.
//We calculate this by finding the bearing between the two nodes. If
//the bearing is N to NE, then the arrow goes up and to the right,
//for example. If the bearing is E to SE, then the arrow goes down
//and to the right, and so on.
//Calculate bearing
/*
* This algorithm determines the bearing (or angle) between two coordinate points.
* It was adapted from http://www.movable-type.co.uk/scripts/latlong.html
*/
alert("constructor calculating bearing")
this.bearing_ = null;
var lat1 = this.startNodeCoordinates_.lat();
var lat2 = this.endNodeCoordinates_.lat();
var dLon = this.startNodeCoordinates_.lng() - this.endNodeCoordinates_.lng();
var y = Math.sin(dLon) * Math.cos(lat2);
var x = Math.cos(lat1)*Math.sin(lat2) -
Math.sin(lat1)*Math.cos(lat2)*Math.cos(dLon);
this.bearing_ = Math.atan2(y, x);
alert("constructor bearing found (bearing = " + this.bearing_ + ")");
this.arrowUp_ = null;
this.arrowRight_ = null;
this.image_ = null;
alert("constructor picking image");
if((this.bearing_ >= 0 && this.bearing_ < (Math.PI * 0.5)) || this.bearing_ == (Math.PI * 2))
{
alert("constructor NE");
//If bearing is N to NE, the arrow goes up and to the right
this.arrowUp_ = new Boolean(true);
this.arrowRight_ = new Boolean(true);
this.image_ = "../../Content/map_images/link_overlay/up_right_black.png";
}
else if(this.bearing_ >= (Math.PI * 0.5) && this.bearing_ < Math.PI)
{
alert("constructor SE");
//If bearing is E to SE, the arrow goes down and to the right
this.arrowUp_ = new Boolean(false);
this.arrowRight_ = new Boolean(true);
this.image_ = "../../Content/map_images/link_overlay/down_right_black.png";
}
else if(this.bearing_ >= Math.PI && this.bearing_ < (Math.PI * 1.5))
{
alert("constructor SW");
//If bearing is S to SW, the arrow goes down and to the left
this.arrowUp_ = new Boolean(false);
this.arrowRight_ = new Boolean(false);
this.image_ = "../../Content/map_images/link_overlay/down_left_black.png";
}
else
{
alert("constructor NW");
//If bearing is W to NW, the arrow goes up and to the left
this.arrowUp_ = new Boolean(true);
this.arrowRight_ = new Boolean(false);
this.image_ = "../../Content/map_images/link_overlay/up_left_black.png";
}
alert("constructor adding to map");
// Explicitly call setMap() on this overlay
this.setMap(this.map_);
alert("constructor end");
}
//This "subclasses" a link overlay from Google Map's OverlayView class
LinkOverlay.prototype = new google.maps.OverlayView();
我也有同样的问题。我用“新建”来实例化我的自定义覆盖层。 在您的情况下,是否确实在实例化覆盖时使用: linkOverlay=新的linkOverlay(南、东、地图)
当该行“失败”时,错误消息是什么?如果不知道这一点,很难帮助您。您是否在此处设置了断点以验证
map
设置是否准确/您是否可以提供更多代码或链接?我正在专门寻找覆盖的onAdd和draw方法。您是否实现了LinkOverlay.prototype.draw
和LinkOverlay.prototype.onAdd
方法?我不确定失败线路的错误消息是什么。由于某些原因,我的网站的设置方式不会自动显示消息。我将尽快找出如何查看错误。我确实实现了onAdd和draw。不过,我很犹豫是否发布它们,因为我还没有机会测试它们,而且我相信它们会有很多新手错误。我怀疑问题出在他们身上,因为我在他们身上也有很多alert(),而且他们的消息都没有显示出来。不过,如果人们认为这会有帮助,我仍然可以发布它们。我在失败的地方写了以下代码:试试{
if(this.map){
this.setMap(this.map);
警报(err.Message)
当代码运行时,此时会出现一个警告框,提示“未定义”。这意味着已定义“This.map”,因为if语句不会跳过This.setMap(This.map)
,因此唯一可以未定义的是setMap()函数。
@:var startNodeCoordinates = new google.maps.LatLng('@startNode.Latitude', '@startNode.Longitude');
@:var endNodeCoordinates = new google.maps.LatLng('@endNode.Latitude', '@endNode.Longitude');
@:var routeLine = new LinkOverlay(startNodeCoordinates, endNodeCoordinates, networkMap);