2017-10-20 121 views
-1

我试过不同的代码,仍然不知道为什么我的标记不显示。 我一直在寻找和阅读谷歌开发人员的文档,并尝试了所有他们解释的方式,没有任何东西。也尝试看看是否有任何sintax错误(并且必须是......),但由于我对我来说是新手,看起来没有错误。为什么不在地图上显示标记?

那么,有人知道我的代码有什么问题吗?

<?php 

get_header(); ?> 

<div id="map"></div><!-- #map--> 
<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?key=AIzaSyAp9iQ2oC3PEvy20_6aDpAPGLT8sFDqAjI&libraries=geometry,places&signed_in=true"></script> 
<script type="text/javascript"> 
    google.maps.event.addDomListener(window, 'load', gmaps_results_initialize); 

    function gmaps_results_initialize() { 
     var map = new google.maps.Map(document.getElementById('map'), { 
     zoom:   3, 
     center:   new google.maps.LatLng(28.291564, 16.62913), 
     mapTypeControl: true, 
     mapTypeControlOptions: { 
      style: google.maps.MapTypeControlStyle.HORIZONTAL_BAR, 
      position: google.maps.ControlPosition.RIGHT_BOTTOM 
     }, 
     zoomControl: true, 
     zoomControlOptions: { 
      position: google.maps.ControlPosition.LEFT_CENTER 
     }, 
     scaleControl: true, 
     streetViewControl: true, 
     streetViewControlOptions: { 
      position: google.maps.ControlPosition.LEFT_BOTTOM 
     }, 
     fullscreenControl: true 
     }); 
     var infoWindow = new google.maps.InfoWindow({map: map}); 

     // Try HTML5 geolocation. 
      if (navigator.geolocation) { 
      navigator.geolocation.getCurrentPosition(function(position) { 
      var pos = { 
      lat: position.coords.latitude, 
      lng: position.coords.longitude 
      }; 

      infoWindow.setPosition(pos); 
      infoWindow.setContent('Location found.'); 
      map.setCenter(pos); 
      }, function() { 
       handleLocationError(true, infoWindow, map.getCenter()); 
      }); 
     } else { 
      // Browser doesn't support Geolocation 
      handleLocationError(false, infoWindow, map.getCenter()); 
      } 

     };  
</script> 
<script> 
     var myLatlng = new google.maps.LatLng(-25.363882,131.044922); 
     var mapOptions = {} 
     var map = new google.maps.Map(document.getElementById("map"), mapOptions); 
     var marker = new google.maps.Marker({}); 

     // To add the marker to the map, call setMap(); 
     marker.setMap(map); 
    </script> 
<script src="/geo.js"></script> 
<?php 
get_sidebar(); 
get_footer(); 

回答

0

我认为它是因为你正在为地图创建两个对象。 使地图对象不变。不要重新初始化它。

你已经在函数外面添加了标记,并且使得它在initialize方法之前执行,当google映射脚本加载时会调用它,因此不会添加标记,因为map未初始化。创建单独的方法TestMarker并从初始化中调用它。

function addMarker(location) { 
    marker = new google.maps.Marker({ 
     position: location, 
     map: map 
    }); 
} 

function TestMarker() { 
     CentralPark = new google.maps.LatLng(37.7699298, -122.4469157); 
     addMarker(CentralPark); 
} 

呼叫TestMarker内部初始化函数

注:地图是这里的常数。

+0

您的意思是? 'const map = new google.maps.Map(document.getElementById('map'),'它仍然不起作用。无论如何谢谢你回答我。 – Caesar

+0

对不起,我编辑了我的答案。请参阅 –

+0

嗨,再次感谢。我在初始化时调用了TestMarker函数,并且改变了你写的最后一个脚本代码,但是仍然没有工作,我感到我有点迟疑... – Caesar