2013-04-25 104 views
4

我想將COUNTY圖標更改爲藍色(除紅色以外的任何內容)。如何更改Google Maps API V3中的圖標顏色?

有沒有辦法爲特定點定義不同的圖標顏色?

<script type="text/javascript"> 
    //<![CDATA[ 
    var map = null; 
function initialize() { 
var myOptions = { 
    zoom: 8, 
    center: new google.maps.LatLng(39.831125875,-112.15968925), 
    mapTypeControl: true, 
    mapTypeControlOptions: {style: google.maps.MapTypeControlStyle.DROPDOWN_MENU}, 
    navigationControl: true, 
    mapTypeId: google.maps.MapTypeId.ROADMAP 
} 
    map = new google.maps.Map(document.getElementById("map_canvas"), 
          myOptions); 

    google.maps.event.addListener(map, 'click', function() { 
    infowindow.close(); 
    }); 

    // Add markers to the map 
    // Set up three markers with info windows 


    var point = new google.maps.LatLng(40.970826,-112.048187) 
    var marker = createMarker(point,'Utah-Davis County'); 


    var point = new google.maps.LatLng(40.235509,-111.660576) 
    var marker = createMarker(point,'Utah-Provo'); 

    var point = new google.maps.LatLng(40.766502,-111.897812) 
    var marker = createMarker(point,'Utah-Salt Lake City'); 


    } 

    var infowindow = new google.maps.InfoWindow(
    { 
    size: new google.maps.Size(150,50) 
    }); 




function createMarker(latlng, html) { 
    var contentString = html; 
    var marker = new google.maps.Marker({ 
    position: latlng, 
    map: map, 
    zIndex: Math.round(latlng.lat()*-100000)<<5 
    }); 

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


function animate(lati,long) { 
    var latLng = new google.maps.LatLng(lati, long); //Makes a latlng 
    map.panTo(latLng); //Make map global 
} 


//]]> 
</script> 

有沒有辦法爲特定點定義不同的圖標顏色?

+0

FIXED:謝謝。 – 2013-04-25 06:06:12

回答

14

我認爲這會有所幫助。你必須創建一個自定義標記。

Google Maps API 3 - Custom marker color for default (dot) marker

專門針對你的問題,你可以做這樣的事情:

var pinColor = "2F76EE"; // a random blue color that i picked 
var pinImage = new google.maps.MarkerImage("http://chart.apis.google.com/chart?chst=d_map_pin_letter&chld=%E2%80%A2|" + pinColor, 
      new google.maps.Size(21, 34), 
      new google.maps.Point(0,0), 
      new google.maps.Point(10, 34)); 
var pinShadow = new google.maps.MarkerImage("http://chart.apis.google.com/chart?chst=d_map_pin_shadow", 
      new google.maps.Size(40, 37), 
      new google.maps.Point(0, 0), 
      new google.maps.Point(12, 35)); 

然後

var marker = new google.maps.Marker({ 
      position: new google.maps.LatLng(0,0), 
      map: map, 
      icon: pinImage, 
      shadow: pinShadow 
     }); 

信貸matt burns

+0

謝謝。它確實顯示一個藍色標記。但是沒有彈出式窗口/文本消息來說明標記的用途。 var point = new google.maps.LatLng(37.679405,-113.063736) var marker = createMarker(point,'Utah-Cedar City');如何修改它以添加消息? 變種標記=新google.maps.Marker({ 位置:新google.maps.LatLng(40.970826,-112.048187) 圖:圖, 圖標:pinImage, 陰影:pinShadow }); var point = new google.maps.LatLng(39.375196,-112.335784) var marker = createMarker(point,'Utah-Oak City-Delta'); – 2013-04-25 05:20:52

+0

這個藍色標記創建的方式與'createMarker(point,content)'函數創建的其他標記不同。如果你想讓它在點擊時彈出,你需要添加一個'eventListener'到藍色標記,就像你在'createMarker'函數中對其他標記的操作一樣。 – 2013-04-26 02:43:32

相關問題