2012-10-15 59 views
0

我有以下腳本在用戶單擊鏈接時用標記填充地圖。谷歌地圖上的不同風格標記

可以有1到12個獨立的標誌,什麼我希望做的是使用不同的標記,如果變量喜愛等於1

有可能是同一個地圖上的多個收藏夾。

我假設在'新的google.maps.Marker'部分添加了'如果最喜歡的== 1'部分,但無法繞過它!

<script> 
$(document).ready(function() { 
$("#map").css({ 
    height: 900, 
    width: 1400 
}); 
var myLatLng = new google.maps.LatLng(<%=dcLatLng%>); //Query database for map center 
MYMAP.init('#map', myLatLng, 12); 

$("#showmarkers").click(function(e){ 
    MYMAP.placeMarkers('markers.asp?WisperID=<%=sWisperID%>'); 
}); 
}); 

var MYMAP = { 
map: null, 
bounds: null 
} 

MYMAP.init = function(selector, latLng, zoom) { 
var myOptions = { 
zoom:zoom, 
center: latLng, 
mapTypeId: google.maps.MapTypeId.ROADMAP 
} 
this.map = new google.maps.Map($(selector)[0], myOptions); 
this.bounds = new google.maps.LatLngBounds(); 
} 

MYMAP.placeMarkers = function(filename) { 
$.get(filename, function(xml){ 
    $(xml).find("marker").each(function(){ 
     var name = $(this).find('name').text(); 
     var address = $(this).find('address').text(); 
     var postcode = $(this).find('postcode').text(); 
     var favourite = $(this).find('favourite').text(); 
     // create a new LatLng point for the marker 
     var lat = $(this).find('lat').text(); 
     var lng = $(this).find('lng').text(); 
     var point = new google.maps.LatLng(parseFloat(lat),parseFloat(lng)); 

     // extend the bounds to include the new point 
     MYMAP.bounds.extend(point); 



     var marker = new google.maps.Marker({ 
      position: point, 
      map: MYMAP.map, 
      icon: new google.maps.MarkerImage(
      '/images/int.png', 
      new google.maps.Size(93, 63), 
      new google.maps.Point(0, 0), 
      new google.maps.Point(0, 63) 
) 
     }); 




     var infoWindow = new google.maps.InfoWindow(); 
     var html='<strong>'+name+'</strong.><br />'+address+'<br />'+postcode+'<br /><p>'+favourite+'</p>'; 
     google.maps.event.addListener(marker, 'mouseover', function() { 
      infoWindow.setContent(html); 
      infoWindow.open(MYMAP.map, marker); 
     }); 
     google.maps.event.addListener(marker, 'mouseout', function() { 
      infoWindow.setContent(html); 
      infoWindow.close(MYMAP.map, marker); 
     }); 
     MYMAP.map.fitBounds(MYMAP.bounds); 
    }); 
}); 
} 
</script> 

回答

0

你已經差不多了。我想這樣的事符合你的要求:

if (favourite == '1') { 
     var marker = new google.maps.Marker({ 
      position: point, 
      map: MYMAP.map, 
      icon: new google.maps.MarkerImage(
       '/images/favourite.png', // different image 
       new google.maps.Size(93, 63), 
       new google.maps.Point(0, 0), 
       new google.maps.Point(0, 63) 
      ) 
     }); 
} else { 
     var marker = new google.maps.Marker({ 
      position: point, 
      map: MYMAP.map, 
      icon: new google.maps.MarkerImage(
       '/images/int.png', 
       new google.maps.Size(93, 63), 
       new google.maps.Point(0, 0), 
       new google.maps.Point(0, 63) 
      ) 
     }); 
} 
+0

謝謝鄧肯,有時你只需要一雙新鮮的眼睛來看看問題。作品一種享受! –