1

以下代码工作:

function()
    {
    var Marker = new google.maps.Marker();
    var MarkerOptions = {};
    MarkerOptions.map = my_map;
    MarkerOptions.position = new google.maps.LatLng(0,0);
    MarkerOptions.animation = google.maps.Animation.DROP;
    MarkerOptions.clickable = true;
    MarkerOptions.cursor = 'pointer';

    Marker.smallicon = [{
        anchor:null,
        origin:null,
        scaledSize:null,
        size:new google.maps.Size(20,32),
        url:'http://maps.gstatic.com/mapfiles/markers2/marker_sprite.png'
        }];

    Marker.setOptions(MarkerOptions);
    }

而添加这一行后,控制台拍摄充满错误

function()
    {
    var Marker = new google.maps.Marker();
    var MarkerOptions = {};
    MarkerOptions.map = way_map;
    MarkerOptions.position = new google.maps.LatLng(0,0);
    MarkerOptions.animation = google.maps.Animation.DROP;
    MarkerOptions.clickable = true;
    MarkerOptions.cursor = 'pointer';

    Marker.smallicon = [{
        anchor:null,
        origin:null,
        scaledSize:null,
        size:new google.maps.Size(20,32),
        url:'http://maps.gstatic.com/mapfiles/markers2/marker_sprite.png'
        }];

    Marker.setIcon(Marker.smallicon);
    Marker.setOptions(MarkerOptions);
    }

我不太了解它,它可以使用,google.maps.MarkerImage但在参考中写入

表示标记图标或阴影图像的结构。此类已弃用,取而代之的是 Icon。

所以我尝试使用 Icon,他们已将其从构造函数更改为 Object,因此更改了调用模型。

4

3 回答 3

2

Marker.smallicon当前是一个数组,而不是一个google.maps.Icon

使用:

Marker.setIcon(Marker.smallicon[0]);

或者让它成为一个对象:

Marker.smallicon = {
        size:new google.maps.Size(20,32),
        url:'http://maps.gstatic.com/mapfiles/markers2/marker_sprite.png'
        };

注意:您不需要设置 a 的所有选项google.maps.Icon,省略您不想设置的选项。

于 2013-01-21T15:24:34.663 回答
1

采用:

icon: {url:'http://maps.gstatic.com/mapfiles/markers2/marker_sprite.png'}

代替

url:'http://maps.gstatic.com/mapfiles/markers2/marker_sprite.png'
于 2013-01-21T15:20:03.117 回答
0

我认为这对你来说很好

//adapted from http://gmaps-samples-v3.googlecode.com/svn/trunk/overlayview/custommarker.html
function CustomMarker(latlng, map, imageSrc) {
    this.latlng_ = latlng;
    this.imageSrc = imageSrc;
    // Once the LatLng and text are set, add the overlay to the map.  This will
    // trigger a call to panes_changed which should in turn call draw.
    this.setMap(map);
}

CustomMarker.prototype = new google.maps.OverlayView();

CustomMarker.prototype.draw = function () {
    // Check if the div has been created.
    var div = this.div_;
    if (!div) {
        // Create a overlay text DIV
        div = this.div_ = document.createElement('div');
        // Create the DIV representing our CustomMarker
        div.className = "customMarker"


        var img = document.createElement("img");
        img.src = this.imageSrc;
        div.appendChild(img);
        google.maps.event.addDomListener(div, "click", function (event) {
            google.maps.event.trigger(me, "click");
        });

        // Then add the overlay to the DOM
        var panes = this.getPanes();
        panes.overlayImage.appendChild(div);
    }

    // Position the overlay 
    var point = this.getProjection().fromLatLngToDivPixel(this.latlng_);
    if (point) {
        div.style.left = point.x + 'px';
        div.style.top = point.y + 'px';
    }
};

CustomMarker.prototype.remove = function () {
    // Check if the overlay was on the map and needs to be removed.
    if (this.div_) {
        this.div_.parentNode.removeChild(this.div_);
        this.div_ = null;
    }
};

CustomMarker.prototype.getPosition = function () {
    return this.latlng_;
};

var map = new google.maps.Map(document.getElementById("map"), {
    zoom: 17,
    center: new google.maps.LatLng(37.77088429547992, -122.4135623872337),
    mapTypeId: google.maps.MapTypeId.ROADMAP
});

var data = [{
    profileImage: "http://www.gravatar.com/avatar/d735414fa8687e8874783702f6c96fa6?s=90&d=identicon&r=PG",
    pos: [37.77085, -122.41356],
}, {
    profileImage: "http://placekitten.com/90/90",
    pos: [37.77220, -122.41555],
}]

for(var i=0;i<data.length;i++){
new CustomMarker(new google.maps.LatLng(data[i].pos[0],data[i].pos[1]), map,  data[i].profileImage)
}
.customMarker {
        position:absolute;
        cursor:pointer;
        background:#424242;
        width:48px;
        height:48px;
        /* -width/2 */
        margin-left:-42px;
        /* -height + arrow */
        margin-top:-84px;
        border-radius:24px;
        padding:0px;
    }
    .customMarker:after {
        content:"";
        position: absolute;
        bottom: -13px;
        left: 10px;
        border-width: 18px 14px 0;
        border-style: solid;
        border-color: #424242 transparent;
        display: block;
        width: 0;
    }
    .customMarker img {
        width:36px;
        height:36px;
        margin:6px;
        border-radius:20px;
    }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<script src="http://maps.google.com/maps/api/js?sensor=false"></script>

<div id="map" style="width: 640pxpx; height: 480px;">map div</div>

于 2017-07-01T07:27:40.960 回答