首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >this.setMap()在自定义覆盖中不起作用

this.setMap()在自定义覆盖中不起作用
EN

Stack Overflow用户
提问于 2012-04-13 19:12:33
回答 1查看 2.5K关注 0票数 0

我正在为Google Map编写一个自定义覆盖图。我有一系列的lat-long点,我想要用箭头指向和离开它们的图像。我正在遵循谷歌的自定义覆盖教程(https://developers.google.com/maps/documentation/javascript/overlays#CustomOverlays)。

在overlay的“构造函数”中,这一行失败了:'this.setMap(this.map_);‘

我需要做些什么才能让它工作?

这是我的overlay的“构造函数”。虚线在底部。在我测试的时候,所有对"alert“的调用都在那里。没有到达最后一个调用,这就是我认为'this.setMap()‘不起作用的原因。

代码语言:javascript
复制
 //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();

这是创建新LinkOverlays的代码(在Razor语法中):

代码语言:javascript
复制
@: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);
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-06-14 04:33:22

我也有同样的问题。我修复了它使用'new‘来实例化我的自定义覆盖。在您的示例中,您确定在实例化overlay时,使用:

linkOverlay =新映射(s,e,LinkOverlay)

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10139753

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档