15

我想在Google地圖上使用我的自定義圖標,並在代碼上添加了圖標網址。但它仍然沒有反映在地圖上。任何人都可以建議,我在這裏失蹤。添加圖標網址「http://google-maps-icons.googlecode.com/files/sailboat-tourism.png」後,爲什麼圖標不會更改。如何更改Google地圖標記上的圖標

<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=false"></script> 

<script type="text/javascript"> 
var markers = [ 
      { 
      "title": 'This is title', 
     "lat": '-37.801578', 
      "lng": '145.060508', 
     "icon": 'http://google-maps-icons.googlecode.com/files/sailboat-tourism.png', 
      "description": 'Vikash Rathee. <strong> This is test Description</strong> <br/><a href="http://www.pricingindia.in/pincode.aspx">Pin Code by 

City</a>' 

     } 
]; 
</script> 
<script type="text/javascript"> 
    window.onload = function() { 
     var mapOptions = { 
      center: new google.maps.LatLng(markers[0].lat, markers[0].lng), 
      zoom: 10, 
     flat: true, 
     styles: [ { "stylers": [ { "hue": "#4bd6bf" }, { "gamma": "1.58" } ] } ], 
      mapTypeId: google.maps.MapTypeId.ROADMAP 
     }; 
     var infoWindow = new google.maps.InfoWindow(); 
     var map = new google.maps.Map(document.getElementById("dvMap"), mapOptions); 
     for (i = 0; i < markers.length; i++) { 
      var data = markers[i] 
      var myLatlng = new google.maps.LatLng(data.lat, data.lng); 
      var marker = new google.maps.Marker({ 
       position: myLatlng, 
       map: map, 
       title: data.title 
      }); 
      (function (marker, data) { 
       google.maps.event.addListener(marker, "click", function (e) { 
        infoWindow.setContent(data.description); 
        infoWindow.open(map, marker); 
       }); 
      })(marker, data); 
     } 
    } 
</script> 


<div id="dvMap" style="width: 100%; height: 100%"> 
</div> 

回答

0

您必須添加有針對性的地圖:

var markers = [ 
      { 
      "title": 'This is title', 
      "lat": '-37.801578', 
      "lng": '145.060508', 
      "map": map, 
      "icon": 'http://google-maps-icons.googlecode.com/files/sailboat-tourism.png', 
      "description": 'Vikash Rathee. <strong> This is test Description</strong> <br/><a href="http://www.pricingindia.in/pincode.aspx">Pin Code by 

City</a>' 
      } 
]; 
+0

EdenSource,沒有工作。你可以在小提琴或cssdeck上發佈完整的代碼或演示嗎? – Vicky

+0

圖標:標記[i] [3],可能是錯誤的。替換爲「圖標」:'http://google-maps-icons.googlecode.com/files/sailboat-tourism.png', – EdenSource

5

試試這個

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] 
      }); 
+0

Manish,沒有工作。你可以在小提琴或cssdeck上發佈完整的代碼或演示嗎? – Vicky

+0

marker = new google.maps.Marker({ position:myLatlng, map:map, icon:locations [0] [3] });檢查這個從數組中獲取第一個圖像圖標。 –

+0

不工作Manish。您能否將您的建議工作代碼發佈在小提琴或cssdeck.com的某處或者發郵件給我:[email protected] – Vicky

0

馬尼什,伊甸園您的建議後:這裏是代碼。但仍顯示紅色(默認)圖標。

<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=false"></script> 

<script type="text/javascript"> 
var markers = [ 
      { 
      "title": 'This is title', 
      "lat": '-37.801578', 
      "lng": '145.060508', 
     "icon": 'http://maps.gstatic.com/mapfiles/ridefinder-images/mm_20_green.png', 
      "description": 'Vikash Rathee. <br/><a href="http://www.pricingindia.in/pincode.aspx">Pin Code by City</a>' 
     } 
]; 
</script> 
<script type="text/javascript"> 
    window.onload = function() { 
     var mapOptions = { 
      center: new google.maps.LatLng(markers[0].lat, markers[0].lng), 
      zoom: 10, 
      flat: true, 
      styles: [ { "stylers": [ { "hue": "#4bd6bf" }, { "gamma": "1.58" } ] } ], 
      mapTypeId: google.maps.MapTypeId.ROADMAP 
     }; 
     var infoWindow = new google.maps.InfoWindow(); 
     var map = new google.maps.Map(document.getElementById("dvMap"), mapOptions); 
     for (i = 0; i < markers.length; i++) { 
      var data = markers[i] 
      var myLatlng = new google.maps.LatLng(data.lat, data.lng); 
      var marker = new google.maps.Marker({ 
       position: myLatlng, 
       map: map, 
     icon: markers[i][3], 
       title: data.title 
      }); 
      (function (marker, data) { 
       google.maps.event.addListener(marker, "click", function (e) { 
        infoWindow.setContent(data.description); 
        infoWindow.open(map, marker); 
       }); 
      })(marker, data); 
     } 
    } 
</script> 


<div id="dvMap" style="width: 100%; height: 100%"> 
</div> 
13

我們可以改變標記的圖標,我做了右鍵單擊事件。 讓我們看看它是否適合你...

// Create a Marker 
var marker = new google.maps.Marker({ 
    position: location, 
    map: map, 
    title:'Sample Tool Tip' 
    }); 


// Set Icon on any event 
google.maps.event.addListener(marker, "rightclick", function() { 
     marker.setIcon('blank.png'); // set image path here... 
}); 
4
var marker = new google.maps.Marker({ 
    position: myLatLng, 
    map: map, 
    icon: 'your-icon.png' 
}); 
+3

雖然這個答案可能是正確和有用的,但如果您[隨同它一起提供一些解釋]( http://meta.stackexchange.com/q/114762/159034)來解釋它如何幫助解決問題。如果存在導致其停止工作並且用戶需要了解其曾經工作的變化(可能不相關),這在未來變得特別有用。謝謝! – Hatchet

+0

贊同@Hatchet,完美的答案! +1 :) – SagarPPanchal