2014-09-19 29 views
-1

您好已經試圖將多個標記與谷歌地圖的自定義樣式結合,我不盡人意。如果有人能指出我的錯誤,我將不勝感激。由於標記中的「地圖」變得自定義,我可以將兩者分開但不能連在一起。我可以如何獲得2個圖標中的1個來顯示。也許我必須分別顯示地圖和昨晚開始的圖標,所以idk。谷歌地圖API v3自定義樣式地圖和多個標記不顯示

var LocationData = [ 
    [42.,-70., "26 E Hastings St, Vancouver" ], 
[42.,-70., "71 E Hastings St, Vancouver" ] 

]; 
var imageIcon ='micon.png'; 

var map; 
var MY_MAPTYPE_ID = 'custom_style'; 

function initialize() { 

    var featureOpts = [ 
    { 
    stylers: [ 
    { hue: '#000089' }, 
    { visibility: 'simplified' }, 
    { gamma: 0.5 }, 
    { weight: 0.5 } 
    ] 
}, 

{ 
    featureType: 'water', 
    stylers: [ 
    { color: '#890000' } 
    ] 
} 
]; 

Map(document.getElementById('map-canvas')); 
var bounds = new google.maps.LatLngBounds(); 
var infowindow = new google.maps.InfoWindow(); 

for (var i in LocationData) 
{ 
    var p = LocationData[i]; 
    var latlng = new google.maps.LatLng(p[0], p[1]); 
    bounds.extend(latlng); 

    var marker = new google.maps.Marker({ 
     position: latlng, 
//   map: map, 
     title: p[2], 
    icon: imageIcon, 
    mapTypeControlOptions: { 
    mapTypeIds: [google.maps.MapTypeId.ROADMAP, MY_MAPTYPE_ID] 
}, 
mapTypeId: MY_MAPTYPE_ID 
    }); 


    google.maps.event.addListener(marker, 'click', function() { 
     infowindow.setContent(this.title); 
     infowindow.open(map, this); 
    }); 



    map = new google.maps.Map(document.getElementById('map-canvas'), marker); 
var styledMapOptions = { 
    name: 'Custom Style' 
}; 

var customMapType = new google.maps.StyledMapType(featureOpts, styledMapOptions); 
map.mapTypes.set(MY_MAPTYPE_ID, customMapType); 
// To add the marker to the map, call setMap(); 
marker.setMap(map); 
} 

    map.fitBounds(bounds); 
} 

google.maps.event.addDomListener(window, 'load', initialize); 

回答

3
  1. 您需要正確定義地圖
map = new google.maps.Map(document.getElementById('map-canvas'), { 
     mapTypeControlOptions: { 
      mapTypeIds: [google.maps.MapTypeId.ROADMAP, MY_MAPTYPE_ID] 
     }, 
     mapTypeId: MY_MAPTYPE_ID 
    }); 
    var styledMapOptions = { 
     name: 'Custom Style' 
    }; 
    var customMapType = new google.maps.StyledMapType(featureOpts, styledMapOptions); 
    map.mapTypes.set(MY_MAPTYPE_ID, customMapType); 
  • 然後將標記添加到它
  • 完整代碼

    var LocationData = [ 
     
        [42., -70., "26 E Hastings St, Vancouver"], 
     
        [42., -71., "71 E Hastings St, Vancouver"] 
     
    
     
    ]; 
     
    var imageIcon = 'micon.png'; 
     
    
     
    var map; 
     
    var MY_MAPTYPE_ID = 'custom_style'; 
     
    
     
    function initialize() { 
     
    
     
        var featureOpts = [{ 
     
         stylers: [{ 
     
          hue: '#000089' 
     
         }, { 
     
          visibility: 'simplified' 
     
         }, { 
     
          gamma: 0.5 
     
         }, { 
     
          weight: 0.5 
     
         }] 
     
        }, 
     
    
     
        { 
     
         featureType: 'water', 
     
         stylers: [{ 
     
          color: '#890000' 
     
         }] 
     
        }]; 
     
    
     
        map = new google.maps.Map(document.getElementById('map-canvas'), { 
     
         mapTypeControlOptions: { 
     
          mapTypeIds: [google.maps.MapTypeId.ROADMAP, MY_MAPTYPE_ID] 
     
         }, 
     
         mapTypeId: MY_MAPTYPE_ID 
     
        }); 
     
        var styledMapOptions = { 
     
         name: 'Custom Style' 
     
        }; 
     
    
     
        var customMapType = new google.maps.StyledMapType(featureOpts, styledMapOptions); 
     
        map.mapTypes.set(MY_MAPTYPE_ID, customMapType); 
     
        var bounds = new google.maps.LatLngBounds(); 
     
        var infowindow = new google.maps.InfoWindow(); 
     
    
     
        for (var i in LocationData) { 
     
         var p = LocationData[i]; 
     
         var latlng = new google.maps.LatLng(p[0], p[1]); 
     
         bounds.extend(latlng); 
     
    
     
         var marker = new google.maps.Marker({ 
     
          position: latlng, 
     
          map: map, 
     
          title: p[2], 
     
          // icon: imageIcon, 
     
    
     
         }); 
     
    
     
    
     
         google.maps.event.addListener(marker, 'click', function() { 
     
          infowindow.setContent(this.title); 
     
          infowindow.open(map, this); 
     
         }); 
     
        } 
     
        map.fitBounds(bounds); 
     
    } 
     
    
     
    google.maps.event.addDomListener(window, 'load', initialize);
    html, body, #map-canvas { 
     
        height: 100%; 
     
        margin: 0px; 
     
        padding: 0px 
     
    }
    <script type="text/javascript" src="https://maps.googleapis.com/maps/api/js"> 
     
        </script> 
     
    <div id="map-canvas"></div>

    +0

    嘿,非常感謝!我有css代碼,對圖像造成嚴重破壞,令我和代碼混淆不清。我覺得我有類似的東西,但是謝謝你,它發送它的工作。 – user1198289 2014-09-19 21:08:28