2016-09-17 61 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