简体   繁体   English

Google Maps API JavaScript-无显示,无错误

[英]Google Maps API JavaScript - no display, no errors

I could use some guidance. 我可以使用一些指导。 I have built a map using Google Maps Javascript API, and I receive no errors on load - but the map isn't displaying. 我已经使用Google Maps Javascript API构建了地图,并且在加载时未收到任何错误-但该地图未显示。 The DOM element is active; DOM元素处于活动状态; the javascript file loads; javascript文件加载; html and css styling are all in place. html和css样式均已就绪。 API key is active, and restricted to my domain. API密钥处于活动状态,并且仅限于我的域。 So I'm really just not sure what I'm doing wrong. 所以我真的只是不确定自己在做什么错。 You can see a live demo of this issue by going to https://www.copelandcompany.com/visit-us/ , and scrolling just below the fold. 您可以转到https://www.copelandcompany.com/visit-us/并滚动到折叠下方,以观看此问题的实时演示。 That white space should be the map. 那个空白应该是地图。

While there are no errors now, what DOES throw errors is when I add "async defer" to the Google Maps javascript, or when I add "callback:initMap" to the API url. 尽管现在没有错误,但是当我向Google Maps javascript中添加“异步延迟”或向API网址中添加“ callback:initMap”时,引发的错误是什么。 So I removed those things, and there's no scripting errors. 因此,我删除了这些内容,并且没有脚本错误。

But - no map. 但是-没有地图。

Here's my code. 这是我的代码。 Any help is appreciated, thanks! 任何帮助表示赞赏,谢谢!

Loaded at the bottom of the header: 加载到标题的底部:

<script src="https://www.copelandcompany.com/wp-content/themes/pinnacle_child/cco-map.js"></script>
    <script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?key=YESILOADEDMYAPIKEYHERE"></script>

Here's my cco-map.js file code: 这是我的cco-map.js文件代码:

// Google Maps Code
function initMap () {
  var cco_office = {lat: 26.687469, lng: -80.054225};
  var cco_map = new google.maps.Map(document.getElementById('cco-map'), {
    zoom: 15,
    center: cco_office
  });

  var contentString = 
      '<div id="cco-marker">'+
      '<img src="/wp-content/uploads/2015/04/DSC_0195.jpg" />'+
      '<h3 style="font-family: Raleway;">Copeland & Co. Real Estate</h3>'+
      '<div id="bodyContent">'+
      '<b>Address:</b> 2822B S Dixie Hwy, West Palm Beach, FL 33405' +
      '<p><b>Phone:</b> 561-500-LIST (5478)<br>'+
      '<b>Email:</b> hello@cpld.co</br>'+
      '<b>Hours:</b> Monday-Friday, 9:00 a.m. to 5:00 p.m.<br>'+
      '<div class="links"><a href="https://www.google.com/maps/dir//Copeland+%26+Co.+Real+Estate,+2822+S+Dixie+Hwy,+West+Palm+Beach,+FL+33405/@26.687469,-80.054225,15z/data=!4m8!4m7!1m0!1m5!1m1!1s0x88d8d7aa6a149623:0xde07abb7d8b409f4!2m2!1d-80.054225!2d26.687469" target="_blank" title="Directions" />Get Directions</a> | <a href="https://www.google.com/maps/place/Copeland+%26+Co.+Real+Estate/@26.6874738,-80.0564137,17z/data=!4m7!3m6!1s0x0:0xde07abb7d8b409f4!8m2!3d26.687469!4d-80.054225!9m1!1b1" target="_blank" title="Reviews" style="color: #EB6E00;" />Read Our 5-Star Reviews</a></div>'+
      '</div>'+
      '</div>';

  var infowindow = new google.maps.InfoWindow({
    content: contentString
  });

  var cco_marker = new google.maps.Marker({
    position: cco_office,
    map: cco_map,
    title: 'Copeland & Co. Real Estate'
  });
  cco_marker.addListener('click', function() {
    InfoWindow.open(cco_map, cco_marker);
  });
}

Loaded by style.css: 由style.css加载:

#cco-map {
    width: 100%;
    height: 400px;
}

And my HTML: 而我的HTML:

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

add style in html 在html中添加样式

<style>
 #cco-map{
   width: 100%;
   height: 400px;
   background-color: grey;
 }
</style>

it will display map 它会显示地图

I ever used google map in my project.. I hope this can help you 我曾经在我的项目中使用过Google地图。希望能对您有所帮助

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

<div>
   <input id="lat" type="text" size="30" class="form-control" name="Latitude" />
   <input id="lng" type="text" size="30" class="form-control" name="Longitude" />
</div>

<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?key=AIzaSyCiH9aX_KI44VpanKHYDE28s-4jTv9B8EM&sensor=true"></script>

<script type="text/javascript">
    function loadmap() {
        var medan = new google.maps.LatLng(3.58727, 98.67491);
        var marker;
        var map;

        var mapOptions = {
            zoom : 13,
            mapTypeId : google.maps.MapTypeId.ROADMAP,
            center : medan
        };
        map = new google.maps.Map(document.getElementById("map"), mapOptions);
        marker = new google.maps.Marker({
            map : map,
            draggable : true,
            animation : google.maps.Animation.DROP,
            position : medan
        });
        google.maps.event.addListener(marker, 'click', toggleBounce);
        google.maps.event.addListener(marker, 'dragend', function() {
            var markerlocation = marker.getPosition();
            $('#lat').val(markerlocation.lat().toString());
            $('#lng').val(markerlocation.lng().toString());
        });
    }

    function toggleBounce() {
        if (marker.getAnimation() != null) {
            marker.setAnimation(null);
        } else {
            marker.setAnimation(google.maps.Animation.BOUNCE);
        }
    }

    loadmap();
</script>

To troubleshoot your issue I would suggest installing the Google Maps API Checker extension for Chrome browser: 要解决您的问题,建议您安装适用于Chrome浏览器的Google Maps API Checker扩展程序:

https://chrome.google.com/webstore/detail/google-maps-api-checker/mlikepnkghhlnkgeejmlkfeheihlehne https://chrome.google.com/webstore/detail/google-maps-api-checker/mlikepnkghhlnkgeejmlkfeheihlehne

Open your page https://www.copelandcompany.com/visit-us/ and check what says the Google Maps API checker 打开您的页面https://www.copelandcompany.com/visit-us/并检查显示的内容是Google Maps API检查器

在此处输入图片说明

As you can see you try to load Maps JavaScript API twice on the same page. 如您所见,您尝试在同一页面上两次加载Maps JavaScript API。 Even more, the first URL 还有,第一个网址

https://maps.googleapis.com/maps/api/js?v=3&libraries=places&key=AIzaSyBN...

loads the release version of the API, while the second URL 加载API的发行版本,而第二个URL

https://maps.googleapis.com/maps/api/js?key=AIzaSyCBe8iI....&callback=initMap

loads experimental version of the API. 加载API的实验版本。 You mix release and experimental version on the same page and use different API keys in both calls. 您在同一页面上混合了发行版和实验版,并在两个调用中使用了不同的API密钥。 I don't think this mix of versions might work correctly. 我认为这种混合版本可能无法正常工作。

Now check the browser console, you say there is no errors, but I can see two of them 现在检查浏览器控制台,您说没有错误,但是我可以看到其中两个

在此处输入图片说明

You have to load Google Maps JavaScript API only once, have a look at versioning document 您只需加载一次Google Maps JavaScript API,请查看版本控制文档

https://developers.google.com/maps/documentation/javascript/versions https://developers.google.com/maps/documentation/javascript/versions

I hope this helps! 我希望这有帮助!

I fixed the issue. 我解决了这个问题。 This was unbeknownst to me, but apparently you have to insert the div tag and the API call together, like back-to-back, in the HTML. 这对我来说并不为人所知,但是显然您必须像HTML一样,将div标签和API调用一起插入。 The issue was that I was loading the API call and my coo-map.js file in the header, instead of loading the API call with the map div. 问题是我在标题中加载了API调用和我的coo-map.js文件,而不是在map div中加载了API调用。

Here's how it should be, for anyone who has the same misunderstanding I had: 对于任何与我有相同误解的人,这应该是这样:

IN THE HEADER: 在标题中:

<script src="https://www.copelandcompany.com/wp-content/themes/pinnacle_child/cco-map.js"></script>

IN THE HTML: 在HTML中:

<div id="cco-map"></div>
<script async defer src="https://maps.googleapis.com/maps/api/js?key=APIKEY?callback=initMap"></script>

IN THE CSS: 在CSS中:

<style>
 #cco-map{
   width: 100%;
   height: 400px; */THIS REQUIRES A DESIGNATED HEIGHT TO LOAD/*
   background-color: grey;
 }
</style>

AND, IN A .JS FILE: 并且,在.JS文件中:

// Google Maps Code
function initMap () {
  var cco_office = {lat: 26.687469, lng: -80.054225};
  var cco_map = new google.maps.Map(document.getElementById('cco-map'), {
    zoom: 15,
    center: cco_office
  });

  var contentString = 
      '<div id="cco-marker">'+
      '<img src="/wp-content/uploads/2015/04/DSC_0195.jpg" />'+
      '<h3 style="font-family: Raleway;">Copeland & Co. Real Estate</h3>'+
      '<div id="bodyContent">'+
      '<b>Address:</b> 2822B S Dixie Hwy, West Palm Beach, FL 33405' +
      '<p><b>Phone:</b> 561-500-LIST (5478)<br>'+
      '<b>Email:</b> hello@cpld.co</br>'+
      '<b>Hours:</b> Monday-Friday, 9:00 a.m. to 5:00 p.m.<br>'+
      '<div class="links"><a href="https://www.google.com/maps/dir//Copeland+%26+Co.+Real+Estate,+2822+S+Dixie+Hwy,+West+Palm+Beach,+FL+33405/@26.687469,-80.054225,15z/data=!4m8!4m7!1m0!1m5!1m1!1s0x88d8d7aa6a149623:0xde07abb7d8b409f4!2m2!1d-80.054225!2d26.687469" target="_blank" title="Directions" />Get Directions</a> | <a href="https://www.google.com/maps/place/Copeland+%26+Co.+Real+Estate/@26.6874738,-80.0564137,17z/data=!4m7!3m6!1s0x0:0xde07abb7d8b409f4!8m2!3d26.687469!4d-80.054225!9m1!1b1" target="_blank" title="Reviews" style="color: #EB6E00;" />Read Our 5-Star Reviews</a></div>'+
      '</div>'+
      '</div>';

  var infowindow = new google.maps.InfoWindow({
    content: contentString
  });

  var cco_marker = new google.maps.Marker({
    position: cco_office,
    map: cco_map,
    title: 'Copeland & Co. Real Estate'
  });
  cco_marker.addListener('click', function() {
    InfoWindow.open(cco_map, cco_marker);
  });
}

All good. 都好。 Thanks everyone. 感谢大家。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM