简体   繁体   English

如何使用 Google Maps API 自定义不同的标记 CSS?

[英]How do I customize different markers CSS with Google Maps API?

I'd like to do this: http://bl.ocks.org/amenadiel/f4e5bd78b214ff081254我想这样做: http : //bl.ocks.org/amenadiel/f4e5bd78b214ff081254

In the example, markerLayer overrides ALL the markers on the map, and I'd only like it to apply to particular markers.在这个例子中,markerLayer 覆盖了地图上的所有标记,我只希望它应用于特定的标记。

 var myoverlay = new google.maps.OverlayView(); myoverlay.draw = function() { //this assigns an id to the markerlayer Pane, so it can be referenced by CSS this.getPanes().markerLayer.id = 'markerLayer'; }; myoverlay.setMap(map);

 <!DOCTYPE html> <html> <head> <title>Simple Map</title> <meta name="viewport" content="initial-scale=1.0, user-scalable=no"> <meta charset="utf-8"> <style> html, body, #map-canvas { height: 500px; width: 960px; margin: 0px; padding: 0px } #markerLayer img { border: 2px solid red !important; width: 85% !important; height: 90% !important; border-radius: 5px; } </style> <script src="https://maps.googleapis.com/maps/api/js?v=3.exp"></script> <script> var map; function initialize() { var mapOptions = { zoom: 9, center: new google.maps.LatLng(40.6, -74) }; map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions); // I create 3 markers using http://ruralshores.com/assets/marker-icon.png as icon var myIcon = 'http://ruralshores.com/assets/marker-icon.png'; var marker1 = new google.maps.Marker({ position: { lat: 40.8, lng: -74 }, map: map, icon: myIcon, optimized: false }); var marker2 = new google.maps.Marker({ position: { lat: 40.6, lng: -74.5 }, map: map, icon: myIcon, optimized: false }); var marker3 = new google.maps.Marker({ position: { lat: 40.5, lng: -74.3 }, map: map, icon: myIcon, optimized: false }); // I create an OverlayView, and set it to add the "markerLayer" class to the markerLayer DIV var myoverlay = new google.maps.OverlayView(); myoverlay.draw = function() { this.getPanes().markerLayer.id = 'markerLayer'; }; myoverlay.setMap(map); } google.maps.event.addDomListener(window, 'load', initialize); </script> </head> <body> <div id="map-canvas"></div> </body> </html>

It seems that using the js-rich-markers library is the best way forward for this kind of thing, makes it easier and 'targetable' for each marker.似乎使用 js-rich-markers 库是处理此类事情的最佳方法,它使每个标记更容易且“可定位”。

Here's the link: https://libraries.io/bower/js-rich-marker这是链接: https : //libraries.io/bower/js-rich-marker

Also for anyone looking for how to operate the js-rich-marker library, this question has a neat answer: Animate Google API Map Marker with CSS?同样对于任何寻找如何操作 js-rich-marker 库的人来说,这个问题有一个简洁的答案: Animate Google API Map Marker with CSS?

You just need to create a "regular" marker, like the third in my example:您只需要创建一个“常规”标记,就像我示例中的第三个:

var marker3 = new google.maps.Marker({
    position: {
          lat: 40.5,
          lng: -74.3
    },
    map: map,
    optimized: true // This will NOT draw the marker in the custom `draw` function
});

 <!DOCTYPE html> <html> <head> <title>Simple Map</title> <meta name="viewport" content="initial-scale=1.0, user-scalable=no"> <meta charset="utf-8"> <style> html, body, #map-canvas { height: 500px; width: 960px; margin: 0px; padding: 0px } #markerLayer img { border: 2px solid red !important; width: 85% !important; height: 90% !important; border-radius: 5px; } </style> <script src="https://maps.googleapis.com/maps/api/js?v=3.exp"></script> <script> var map; function initialize() { var mapOptions = { zoom: 9, center: new google.maps.LatLng(40.6, -74) }; map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions); // I create 3 markers using http://ruralshores.com/assets/marker-icon.png as icon var myIcon = 'http://ruralshores.com/assets/marker-icon.png'; var marker1 = new google.maps.Marker({ position: { lat: 40.8, lng: -74 }, map: map, icon: myIcon, optimized: false }); var marker2 = new google.maps.Marker({ position: { lat: 40.6, lng: -74.5 }, map: map, icon: myIcon, optimized: false }); var marker3 = new google.maps.Marker({ position: { lat: 40.5, lng: -74.3 }, map: map, optimized: true }); // I create an OverlayView, and set it to add the "markerLayer" class to the markerLayer DIV var myoverlay = new google.maps.OverlayView(); myoverlay.draw = function() { this.getPanes().markerLayer.id = 'markerLayer'; }; myoverlay.setMap(map); } google.maps.event.addDomListener(window, 'load', initialize); </script> </head> <body> <div id="map-canvas"></div> </body> </html>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Google Maps API中添加多个叠加标记 - How do I add multiple overlay markers in google maps api 我们如何在Google Maps API中删除绑定到标记的标签 - How do we remove labels bound to markers in google maps api 如何向多个标记添加相同的事件侦听器,然后区分 Google Maps API v3 中侦听器中的标记? - How do I add same event listener to many markers and then differentiate between the markers in the listeners in Google Maps API v3? Google Maps API:如何使用户添加的标记成为永久标记? - Google Maps API: how do I make user-added markers permanent? 如何使标记可拖动,标记和删除? 使用Google Maps JavaScript API - How do I make markers draggable, labeled, and deletable? with Google maps javascript API 如何在点击时显示标记并使用 google-maps-react API 获取坐标 - How do I display markers on click and get coordinates using google-maps-react API 如何将位智事件标记添加到带有流量的Google Maps javascript API生成的地图中? - How do I add waze incident markers to a google maps javascript API-generated map with traffic? 在Google Maps API中,如何插入带有多个信息窗口的多个标记? - In the Google Maps API, how do I insert multiple markers with multiple info windows? 我如何从 MySQL db 和谷歌地图上的 pin 标记 api 获取坐标 - How do i get coords from MySQL db and pin markers on google maps api 如何将标记添加到带有延迟的Google地图 - How do I add the markers to the google maps wth delays
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM