Javascript 谷歌地图多个自定义标记

Javascript 谷歌地图多个自定义标记,javascript,google-maps,google-maps-api-3,google-maps-markers,Javascript,Google Maps,Google Maps Api 3,Google Maps Markers,所以我正在努力学习如何制作谷歌地图。我对javascript知之甚少,但我在这里尝试学习 我在网上引用了一段代码,我已经了解了如何添加位置、标记和信息窗口,但我正试图找出如何为每个标记添加多个自定义图标 谢谢你的帮助 function initialize() { //add map, the type of map var map = new google.maps.Map(document.getElementById('map'), {

所以我正在努力学习如何制作谷歌地图。我对javascript知之甚少,但我在这里尝试学习

我在网上引用了一段代码,我已经了解了如何添加位置、标记和信息窗口,但我正试图找出如何为每个标记添加多个自定义图标

谢谢你的帮助

function initialize() {

        //add map, the type of map
        var map = new google.maps.Map(document.getElementById('map'), {
            zoom: 6,
            center: new google.maps.LatLng(37.7749295, -122.4194155),
            mapTypeId: google.maps.MapTypeId.ROADMAP
        });

        //add locations
        var locations = [
            ['San Francisco: Power Outage', 37.7749295, -122.4194155],
            ['Sausalito', 37.8590937, -122.4852507],
            ['Sacramento', 38.5815719, -121.4943996],
            ['Soledad', 36.424687, -121.3263187],
            ['Shingletown', 40.4923784, -121.8891586]
        ];

        //declare marker call it 'i'
        var marker, i;

        //declare infowindow
        var infowindow = new google.maps.InfoWindow();

        //add marker to each locations
        for (i = 0; i < locations.length; i++) {
            marker = new google.maps.Marker({
                position: new google.maps.LatLng(locations[i][1], locations[i][2]),
                map: map,
            });

            //click function to marker, pops up infowindow
            google.maps.event.addListener(marker, 'click', (function(marker, i) {
                return function() {
                    infowindow.setContent(locations[i][0]);
                    infowindow.open(map, marker);
                }
            })(marker, i));
        }
    }
    google.maps.event.addDomListener(window, 'load', initialize);
函数初始化(){
//添加地图,地图的类型
var map=new google.maps.map(document.getElementById('map'){
缩放:6,
中心:新google.maps.LatLng(37.7749295,-122.4194155),
mapTypeId:google.maps.mapTypeId.ROADMAP
});
//添加位置
变量位置=[
[“旧金山:停电”,37.7749295,-122.4194155],
[Sausalito',37.8590937,-122.4852507],
['Sacramento',38.5815719,-121.4943996],
[Soledad',36.424687,-121.3263187],
[Shingletown',40.4923784,-121.8891586]
];
//我叫它‘我’
var标记,i;
//声明信息窗口
var infowindow=new google.maps.infowindow();
//将标记添加到每个位置
对于(i=0;i
有多种方法,最简单的方法是将标记的图标属性设置为要显示为标记的图像的URL

样本:

//modified array with icon-URLs
var locations = [
            ['San Francisco: Power Outage', 37.7749295, -122.4194155,'http://labs.google.com/ridefinder/images/mm_20_purple.png'],
            ['Sausalito', 37.8590937, -122.4852507,'http://labs.google.com/ridefinder/images/mm_20_red.png'],
            ['Sacramento', 38.5815719, -121.4943996,'http://labs.google.com/ridefinder/images/mm_20_green.png'],
            ['Soledad', 36.424687, -121.3263187,'http://labs.google.com/ridefinder/images/mm_20_blue.png'],
            ['Shingletown', 40.4923784, -121.8891586,'http://labs.google.com/ridefinder/images/mm_20_yellow.png']
        ];



//inside the loop
marker = new google.maps.Marker({
                position: new google.maps.LatLng(locations[i][1], locations[i][2]),
                map: map,
                icon: locations[i][3]
            });
Locations表示一组地区,后面紧跟着他们的posicón,特别是其纬度和经度,因此它们将被定位在谷歌地图上,而不总是以这种方式发送纬度和经度,因此您可以接收数据库或json。希望我能帮上忙。


    <div id="map"></div>

  <script type="text/javascript">          
     function initialize() {
         //add map, the type of map
           var mapOptions = {
               zoom: 5,
               draggable: true,
               animation: google.maps.Animation.DROP,
               mapTypeId: google.maps.MapTypeId.ROADMAP,
               center: new google.maps.LatLng(51.4964302,-0.1331412), // area location
               styles:[{"stylers":[{"saturation":-100},{"gamma":1}]},{"elementType":"labels.text.stroke","stylers":[{"visibility":"off"}]},{"featureType":"poi.business","elementType":"labels.text","stylers":[{"visibility":"off"}]},{"featureType":"poi.business","elementType":"labels.icon","stylers":[{"visibility":"off"}]},{"featureType":"poi.place_of_worship","elementType":"labels.text","stylers":[{"visibility":"off"}]},{"featureType":"poi.place_of_worship","elementType":"labels.icon","stylers":[{"visibility":"off"}]},{"featureType":"road","elementType":"geometry","stylers":[{"visibility":"simplified"}]},{"featureType":"water","stylers":[{"visibility":"on"},{"saturation":50},{"gamma":0},{"hue":"#50a5d1"}]},{"featureType":"administrative.neighborhood","elementType":"labels.text.fill","stylers":[{"color":"#333333"}]},{"featureType":"road.local","elementType":"labels.text","stylers":[{"weight":0.5},{"color":"#333333"}]},{"featureType":"transit.station","elementType":"labels.icon","stylers":[{"gamma":1},{"saturation":50}]}]
           };
           var mapElement = document.getElementById('map');
           var map = new google.maps.Map(mapElement, mapOptions);

         //add locations
             var locations = [
                 ['London office', 51.4964302,-0.1331412,'http://labs.google.com/ridefinder/images/mm_20_red.png'],
                 ['Sausalito', 37.8590937, -122.4852507,'http://labs.google.com/ridefinder/images/mm_20_red.png'],
                 ['Sacramento', 38.5815719, -121.4943996,'http://labs.google.com/ridefinder/images/mm_20_green.png'],
                 ['Soledad', 36.424687, -121.3263187,'http://labs.google.com/ridefinder/images/mm_20_blue.png'],
                 ['Shingletown', 40.4923784, -121.8891586,'http://labs.google.com/ridefinder/images/mm_20_yellow.png']
             ];
             //declare marker call it 'i'
             var marker, i;
             //declare infowindow
             var infowindow = new google.maps.InfoWindow();
             //add marker to each locations
             for (i = 0; i < locations.length; i++) {
                 marker = new google.maps.Marker({
                     position: new google.maps.LatLng(locations[i][1], locations[i][2]),
                     map: map,
                     icon: locations[i][3]
             });
             //click function to marker, pops up infowindow
             google.maps.event.addListener(marker, 'click', (function(marker, i) {
                 return function() {
                     infowindow.setContent(locations[i][0]);
                     infowindow.open(map, marker);
                 }
             })(marker, i));
         }
     }
     google.maps.event.addDomListener(window, 'load', initialize);
  </script>
函数初始化(){ //添加地图,地图的类型 变量映射选项={ 缩放:5, 真的, 动画:google.maps.animation.DROP, mapTypeId:google.maps.mapTypeId.ROADMAP, 中心:新的google.maps.LatLng(51.4964302,-0.1331142),//区域位置 样式:[{“stylers”:[{“saturation”:-100},{“gamma”:1}]},{“elementType”:“labels.text.stroke”,“stylers”:[{“visibility”:“off”}},{“featureType”:“poi.business”,“elementType”:“labels.icon”,“stylers”:[{“visibility”:“off”},{“poi.business”,“elementType”:“labels.icon”;“poi.business”,“elementType”:“labels.icon,“elementType”:“labels.text”,“stylers”:[{“visibility”:“off”}],{“featureType”:“poi.place_of_崇拜”,“elementType”:“labels.icon”,“stylers”:[{“visibility”:“off”}],{“featureType”:“geometry”,“stylers”:[{“visibility”:“simplified”}],{“featureType”:“water”,“stylers”:[{“visibility”:“on”;{“visibility”:“on”;{“饱和度”;“50},{“gamma”:0}],{“色调”:“}]},{“featureType”:“administration.neighbor”,“elementType”:“labels.text.fill”,“stylers”:[{“color”:“featureType”:“road.local”,“elementType”:“labels.text”,“stylers”:[{“weight”:0.5},{“color”:“{”333333”},{“featureType”:“transit.station”,“elementType”:“labels.icon”,“stylers”:[{“gamma”:1},{“饱和度”:50}] }; var mapeelement=document.getElementById('map'); var map=new google.maps.map(mapElement,mapOptions); //添加位置 变量位置=[ [“伦敦办事处”,51.4964302,-0.1331412,'http://labs.google.com/ridefinder/images/mm_20_red.png'], [Sausalito',37.8590937,-122.4852507,'http://labs.google.com/ridefinder/images/mm_20_red.png'], ['Sacramento',38.5815719,-121.4943996,'http://labs.google.com/ridefinder/images/mm_20_green.png'], [Soledad',36.424687,-121.3263187,'http://labs.google.com/ridefinder/images/mm_20_blue.png'], [Shingletown',40.4923784,-121.8891586,'http://labs.google.com/ridefinder/images/mm_20_yellow.png'] ]; //我叫它‘我’ var标记,i; //声明信息窗口 var infowindow=new google.maps.infowindow(); //将标记添加到每个位置 对于(i=0;i
    <div id="map"></div>

  <script type="text/javascript">          
     function initialize() {
         //add map, the type of map
           var mapOptions = {
               zoom: 5,
               draggable: true,
               animation: google.maps.Animation.DROP,
               mapTypeId: google.maps.MapTypeId.ROADMAP,
               center: new google.maps.LatLng(51.4964302,-0.1331412), // area location
               styles:[{"stylers":[{"saturation":-100},{"gamma":1}]},{"elementType":"labels.text.stroke","stylers":[{"visibility":"off"}]},{"featureType":"poi.business","elementType":"labels.text","stylers":[{"visibility":"off"}]},{"featureType":"poi.business","elementType":"labels.icon","stylers":[{"visibility":"off"}]},{"featureType":"poi.place_of_worship","elementType":"labels.text","stylers":[{"visibility":"off"}]},{"featureType":"poi.place_of_worship","elementType":"labels.icon","stylers":[{"visibility":"off"}]},{"featureType":"road","elementType":"geometry","stylers":[{"visibility":"simplified"}]},{"featureType":"water","stylers":[{"visibility":"on"},{"saturation":50},{"gamma":0},{"hue":"#50a5d1"}]},{"featureType":"administrative.neighborhood","elementType":"labels.text.fill","stylers":[{"color":"#333333"}]},{"featureType":"road.local","elementType":"labels.text","stylers":[{"weight":0.5},{"color":"#333333"}]},{"featureType":"transit.station","elementType":"labels.icon","stylers":[{"gamma":1},{"saturation":50}]}]
           };
           var mapElement = document.getElementById('map');
           var map = new google.maps.Map(mapElement, mapOptions);

         //add locations
             var locations = [
                 ['London office', 51.4964302,-0.1331412,'http://labs.google.com/ridefinder/images/mm_20_red.png'],
                 ['Sausalito', 37.8590937, -122.4852507,'http://labs.google.com/ridefinder/images/mm_20_red.png'],
                 ['Sacramento', 38.5815719, -121.4943996,'http://labs.google.com/ridefinder/images/mm_20_green.png'],
                 ['Soledad', 36.424687, -121.3263187,'http://labs.google.com/ridefinder/images/mm_20_blue.png'],
                 ['Shingletown', 40.4923784, -121.8891586,'http://labs.google.com/ridefinder/images/mm_20_yellow.png']
             ];
             //declare marker call it 'i'
             var marker, i;
             //declare infowindow
             var infowindow = new google.maps.InfoWindow();
             //add marker to each locations
             for (i = 0; i < locations.length; i++) {
                 marker = new google.maps.Marker({
                     position: new google.maps.LatLng(locations[i][1], locations[i][2]),
                     map: map,
                     icon: locations[i][3]
             });
             //click function to marker, pops up infowindow
             google.maps.event.addListener(marker, 'click', (function(marker, i) {
                 return function() {
                     infowindow.setContent(locations[i][0]);
                     infowindow.open(map, marker);
                 }
             })(marker, i));
         }
     }
     google.maps.event.addDomListener(window, 'load', initialize);
  </script>