我正在寻找一种方法来使标记阴影与谷歌地图即将推出的“视觉刷新”一起工作,但似乎不能。
我想以下是推荐的方法: https ://developers.google.com/maps/tutorials/customizing/custom-markers
任何人都可以在该教程的示例中看到标记阴影吗?我不能。
你们在做什么来完成这项工作?
提前致谢!
我正在寻找一种方法来使标记阴影与谷歌地图即将推出的“视觉刷新”一起工作,但似乎不能。
我想以下是推荐的方法: https ://developers.google.com/maps/tutorials/customizing/custom-markers
任何人都可以在该教程的示例中看到标记阴影吗?我不能。
你们在做什么来完成这项工作?
提前致谢!
Why not create an extra marker with lower z-index?
createMarkerShadow = function(map, data) {
var latLng = new google.maps.LatLng(data.latitude, data.longitude);
var markerShadow = new google.maps.Marker({
clickable: false,
position: latLng,
map: map,
icon:{
url: '/frontend/img/icons/google-map-marker-shadow.svg',
//The size image file.
size: new google.maps.Size(225, 120),
//The point on the image to measure the anchor from. 0, 0 is the top left.
origin: new google.maps.Point(0, 0),
//The x y coordinates of the anchor point on the marker. e.g. If your map marker was a drawing pin then the anchor would be the tip of the pin.
anchor: new google.maps.Point(115, 82)
},
zIndex: (Math.round(latLng.lat()*-100000)<<5)-1
});
return markerShadow;
};
setMarkerShadows = function (map, locations, bound) {
for (var i = 0; i < locations.length; i++) {
var data = locations[i];
var markerShadow = createMarkerShadow(map, data);
bound.extend(markerShadow.getPosition());
}
};
bound = new google.maps.LatLngBounds();
添加阴影以发布视觉刷新 Google Maps Javascript API v3 地图的一种选择:
添加带有阴影的标记,如下所示:
var marker = new google.maps.Marker({
position: myLatLng,
map: map,
icon: getMarkerImage(beach[4]),
shape: iconShape,
title: beach[0],
zIndex: Math.round(myLatLng.lat()*-100000)<<5
});
var shadow = new MarkerShadow(myLatLng, iconShadow, map);
marker.bindTo('map',shadow,'map');
MarkerShadow 代码(根据 Google 的自定义叠加示例修改):
MarkerShadow.prototype = new google.maps.OverlayView();
/** @constructor */
function MarkerShadow(position, options, map) {
// Initialize all properties.
this.posn_ = position;
this.map_ = map;
if (typeof(options) == "string") {
this.image = options;
} else {
this.options_ = options;
if (!!options.size) this.size_ = options.size;
if (!!options.url) this.image_ = options.url;
}
// Define a property to hold the image's div. We'll
// actually create this div upon receipt of the onAdd()
// method so we'll leave it null for now.
this.div_ = null;
// Explicitly call setMap on this overlay.
this.setMap(map);
}
/**
* onAdd is called when the map's panes are ready and the overlay has been
* added to the map.
*/
MarkerShadow.prototype.onAdd = function() {
// if no url, return, nothing to do.
if (!this.image_) return;
var div = document.createElement('div');
div.style.borderStyle = 'none';
div.style.borderWidth = '0px';
div.style.position = 'absolute';
// Create the img element and attach it to the div.
var img = document.createElement('img');
img.src = this.image_;
img.style.width = this.options_.size.x + 'px';
img.style.height = this.options_.size.y +'px';
img.style.position = 'absolute';
div.appendChild(img);
this.div_ = div;
// Add the element to the "overlayLayer" pane.
var panes = this.getPanes();
panes.overlayShadow.appendChild(div);
};
MarkerShadow.prototype.draw = function() {
// if no url, return, nothing to do.
if (!this.image_) return;
// We use the coordinates of the overlay to peg it to the correct position
// To do this, we need to retrieve the projection from the overlay.
var overlayProjection = this.getProjection();
var posn = overlayProjection.fromLatLngToDivPixel(this.posn_);
// Resize the image's div to fit the indicated dimensions.
if (!this.div_) return;
var div = this.div_;
if (!!this.options_.anchor) {
div.style.left = Math.floor(posn.x-this.options_.anchor.x) + 'px';
div.style.top = Math.floor(posn.y-this.options_.anchor.y) + 'px';
}
if (!!this.options_.size) {
div.style.width = this.size_.x + 'px';
div.style.height = this.size_.y + 'px';
}
};
// The onRemove() method will be called automatically from the API if
// we ever set the overlay's map property to 'null'.
MarkerShadow.prototype.onRemove = function() {
if (!this.div_) return;
this.div_.parentNode.removeChild(this.div_);
this.div_ = null;
};