2013-05-29 127 views
1

我想添加一個標記到我的自定義谷歌地圖API v3。地圖顯示正常,但我似乎無法添加標記。這似乎是一件基本的事情,但我一直陷入困境。我試過按照https://developers.google.com/maps/documentation/javascript/overlays#AddingOverlays將基本標記添加到谷歌地圖api v3

的說明。但是,我對JavaScript和API的知識總的來說是相當有限的,所以我覺得在這裏必須缺少一些基本的東西。 Google說明基本上可能不包含它。缺少冒號,括號。也許變量的順序是錯誤的?任何輸入非常感謝!這裏的代碼如下:

<script type="text/javascript"> 
function initialize() { 

    var myLatlng = new google.maps.LatLng(51.60505, -0.18989); 

    var mapOptions = { 
     center: myLatlng, 
     zoom: 18, 
     mapTypeId: google.maps.MapTypeId.ROADMAP 
     }; 

    var map = new google.maps.Map(document.getElementById("map-canvas"), mapOptions); 

    //the code for the marker, as copy pasted from google 
    var marker = new google.maps.Marker({ 
     position: myLatlng, 
     title:"Hello World!" 
    }); 

    var styles = [ 
     {featureType: "landscape", 
     stylers: [ 
      { color: "#378fb5" } 
     ] 
     },{ 
     ETC 
     ETC 
     ETC 
     } 
    ]; 

    map.setOptions({styles: styles}); 
} 
google.maps.event.addDomListener(window, 'load', initialize); 
</script> 

回答

2

你必須精確您要使用的地圖像這樣的標記:

var marker = new google.maps.Marker({ 
    map: map, 
    position: myLatlng, 
    title:"Hello World!" 
}); 

有一個美好的一天!

+0

我的日子很好地改進了!謝謝Alexis! – bestfriendsforever

2

你需要調用

marker.setMap(map) 

你已經創造了它之後,即

<script type="text/javascript"> 
function initialize() { 

var myLatlng = new google.maps.LatLng(51.60505, -0.18989); 

var mapOptions = { 
    center: myLatlng, 
    zoom: 18, 
    mapTypeId: google.maps.MapTypeId.ROADMAP 
    }; 

var map = new google.maps.Map(document.getElementById("map-canvas"), mapOptions); 

//the code for the marker, as copy pasted from google 
var marker = new google.maps.Marker({ 
    position: myLatlng, 
    title:"Hello World!" 
}); 

// Add the marker to the map 
marker.setMap(map); 

var styles = [ 
    {featureType: "landscape", 
    stylers: [ 
     { color: "#378fb5" } 
    ] 
    },{ 
    ETC 
    ETC 
    ETC 
    } 
]; 

map.setOptions({styles: styles}); 
} 
google.maps.event.addDomListener(window, 'load', initialize); 
</script> 
+0

這也很完美。謝謝戴夫。 – bestfriendsforever

0

如果要將標記拖放到所需的位置,請設置draggable = true。 (map:map, draggable:true });