2016-09-17 63 views
-1

當我有一個谷歌地圖以下初始化:谷歌地圖初始化不能看地圖變量添加標記

function initialize() 
    { 
     var mapOptions = { 
      zoom: 15, 
      center: myLatLng, 
      mapTypeControl: true, 
      mapTypeControlOptions: { 
      style: google.maps.MapTypeControlStyle.DROPDOWN_MENU 
      }, 
      zoomControl: true, 
      scaleControl: true, 
      scrollwheel: true, 
      disableDoubleClickZoom: true, 
     }; 
     var map = new google.maps.Map(document.getElementById("googleMap"), 
      mapOptions); 
     } 

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

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

當試圖添加的標記,我得到的錯誤:

Uncaught ReferenceError: map is not defined

如何查看此地圖或向地圖添加標記?

+1

我得到的錯誤是'Uncaught ReferenceError:myLatLng is not defined'。在'initialize'函數中移動標記的定義(當前'map'變量是函數的局部變量)。 ([工作小提琴](http://jsfiddle.net/o6ncm9dd/1/)) – geocodezip

回答

1

似乎你沒有適合你的標記的myLatLng位置嘗試添加一個例如:myLatLng = new google.maps.LatLng(45.00,10.00);

function initialize() 
    { 
     var mapOptions = { 
      zoom: 15, 
      center: myLatLng, 
      mapTypeControl: true, 
      mapTypeControlOptions: { 
      style: google.maps.MapTypeControlStyle.DROPDOWN_MENU 
      }, 
      zoomControl: true, 
      scaleControl: true, 
      scrollwheel: true, 
      disableDoubleClickZoom: true, 
     }; 
     var map = new google.maps.Map(document.getElementById("googleMap"), 
      mapOptions); 
     } 

    var myLatLng = new google.maps.LatLng(45.00, 10.00); 
    var marker = new google.maps.Marker({ 
     position: myLatLng, 
     map: map, 
     title: 'Hello World!' 
     }); 

    google.maps.event.addDomListener(window, "load", initialize); 
    }; 
+0

它擺脫了錯誤,但標記不顯示。 – Atma

+0

marker是initialize()函數中的一個嗎? – scaisEdge

+0

我已經更新了答案,似乎myLatLng沒有定義。 – scaisEdge