2016-12-26 58 views
0

我在使用Google地图添加标记时遇到了问题,但在我看来,代码中存在某些内容,如果以下内容太简单无法实现,请耐心等待:在Google地图中添加简单标记时出现问题

<script type="text/javascript"> 
    google.maps.event.addDomListener(window, 'load', init); 

    function init() { 

     var ll = new google.maps.LatLng(12.0633419, 77.0998847); 

     var mapOptions = { 
      zoom: 12, 
      center: ll, 
      styles: [{ 
       featureType:"all", 
       elementType:"all", 
       stylers: [ 
        { invert_lightness:false }, 
        { saturation:0 }, 
        { lightness:0 }, 
        { gamma:0.5 }, 
        { hue:"#2f7ed8" } 
       ] 
      }] 
     }; 

     // add marker to the map 
     var marker = new google.maps.Marker({ 
      position: ll, 
      icon: ll, 
      map: map, 
      title: 'IT DOES NOT WORK!!!!' 
     }); 
     //marker.setMap(map); 
     //marker.setPosition(ll); 

     var mapElement = document.getElementById('map'); 

     var map = new google.maps.Map(mapElement, mapOptions); 
    } 

</script> 
+0

标记看起来不错,也许你应该在定义变量'map'后添加它吧! – user2314737

+0

@ user2314737感谢提示,不知道为什么它没有工作,但现在它确实,jami0821的答案奏效! – Gery

+1

你需要初始化地图变量_before_你为它添加标记。 – geocodezip

回答

1

渲染到#map单元工作时,做这样:

<div id="map"></div> 

<script type="text/javascript"> 

    function init() { 
    var ll = new google.maps.LatLng(12.0633419, 77.0998847); 

    var map = new google.maps.Map(document.getElementById('map'), { 
     zoom: 4, 
     center: ll 
    }); 

    // add marker to the map 
    var marker = new google.maps.Marker({ 
     position: ll, 
     map: map 
    }); 
    } 

</script> 

还记得在回调(在这种情况下init)使用正确的函数名称:

<script async defer 
src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&callback=init"> 
</script> 
+0

你是最棒的!谢谢!! – Gery