【问题标题】:Google Map container shows with no map谷歌地图容器显示没有地图
【发布时间】:2017-11-20 04:42:12
【问题描述】:

我知道这已经被问过很多次了,但是我见过的其他答案似乎都没有帮助我。基本上,地图的容器会显示(在页面上显示为 500x300 像素的空白区域),但没有地图。无论我做什么,我都无法显示地图。

您可能已经猜到了,我对 Google 地图不熟悉,因此不胜感激。我设置了缩放并包括“溢出:可见”,这是另一个建议。

希望这个小提琴有效:https://jsfiddle.net/7cvj565j/

findfitter.php - 显示地图的位置

<div class="container text-center">
    <div class="row">
        <div class="col-sm-8">
            <div id="map-container">
                <div id="googleMap" style="height:300px; width:500px;">
                    <script src="https://maps.googleapis.com/maps/api/js?key=MY_KEY&callback=initMap" async defer></script>
                    <script src="includes/findafitter2.js"></script>
                </div>
            </div>
        </div>
        <div class="col-sm-4">
            Enter your postcode to find your nearest fitter. Do you have any questions? <a href="contact.php">Let us know!</a>
            <input id="address" type="textbox" value="">
            <input type="button" value="Find My Nearest Fitter" onclick="checkZip(getElementById('address').value)">
        </div>
    </div>
</div>

findfitter2.js - 地图的代码

var geocoder;
var map;
var markers = [];

function initialize() 
{
    geocoder = new google.maps.Geocoder();
    var latlng = new google.maps.LatLng(40.768505, -111.853244);
    var myOptions = 
    {
        zoom: 8,
        center: latlng,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    }
    map = new google.maps.Map(document.getElementById("googleMap"), myOptions);
    addPostCode('84101');
    addPostCode('84102');
    addPostCode('84103');
    addPostCode('84104');
    addPostCode('84105');
}

function addPostCode(zip) 
{
    geocoder.geocode( { 'address': zip}, function(results, status) 
    {
        if (status == google.maps.GeocoderStatus.OK)
        {
            map.setCenter(results[0].geometry.location);
            var marker = new google.maps.Marker({
                map: map,
                position: results[0].geometry.location,
                name: zip
            });
            markers.push(marker);
        } 
        else 
        {
            alert("Geocode was not successful for the following reason: " + status);
        }
    });
}

function checkZip(zip)
{
    var distance = Number.MAX_VALUE;
    var index = 0;
    geocoder.geocode( { 'address': zip}, function(results, status)
    {
        if (status == google.maps.GeocoderStatus.OK)
        {
            for(ix=0; ix< markers.length; ix++)
            {
                var tmp = getDistance(results[0].geometry.location, markers[ix].position);
                if (tmp < distance)
                {
                    distance = tmp;
                    index = ix;
                }
            }
            alert('nearest zipcode is :' + markers[index].name);
        }
    });
}

function getDistance(latlng1, latlng2)
{
    var R = 6371; // Radius of the earth in km
    var dLat = (latlng2.lat()-latlng1.lat()) * Math.PI / 180;  // Javascript functions in radians
    var dLon = (latlng2.lng()-latlng1.lng()) * Math.PI / 180;
    var a = Math.sin(dLat/2) * Math.sin(dLat/2) +
            Math.cos(latlng1.lat()  * Math.PI / 180) * Math.cos(latlng2.lat()  * Math.PI / 180) *
            Math.sin(dLon/2) * Math.sin(dLon/2);
    var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
    var d = R * c; // Distance in km
    d = d * 0.621371192; // convert to miles
    return d;
}

styles.css - 相关位

#map-container
{
    min-height: 300px;
    color: #000000;
    overflow: visible;
}

Google 地图代码取自另一个 SO 帖子 (Google maps - Postcode plotting),以查看我的地图代码是否破坏了它 - 这与我将要做的非常相似,并且该代码适用于许多人。

希望这已经足够清楚了。提前致谢!

【问题讨论】:

  • 您是否尝试过使用谷歌地图自动生成的 iframe 代替?

标签: javascript html css google-maps google-maps-api-3


【解决方案1】:
  1. 在 Google 地图中搜索您想要的位置。
  2. 点击“分享”
  3. 点击“嵌入地图”
  4. Google 地图会自动生成一个 iframe 供您使用

告诉我你对这种方法的看法!

似乎比从另一个 StackOverflow 帖子中获取谷歌地图代码并尝试使其工作更好。

&lt;iframe src="https://www.google.com/maps/embed?pb=!1m16!1m12!1m3!1d2965.0824050173574!2d-93.63905729999999!3d41.998507000000004!2m3!1f0!2f0!3f0!3m2!1i1024!2i768!4f13.1!2m1!1sWebFilings%2C+University+Boulevard%2C+Ames%2C+IA!5e0!3m2!1sen!2sus!4v1390839289319" width="100%" height="300px" frameborder="0" style="border:0"&gt;&lt;/iframe&gt;

【讨论】:

  • 我需要能够找到与用户给出的邮政编码最近的标记。希望我提供的代码可以做到这一点,但我无法测试它,因为地图不会显示。我确实喜欢 iframe 解决方案,但它允许我这样做吗?
【解决方案2】:

iframe 选择是最简单的,但我认为您需要 JS 版本。 https://jsfiddle.net/scorpio777/af2nzqh1/

这可能对您不起作用,因为 API 密钥不正确,或者与您正在使用的 div 和样式有关。 将地图放在网页上最简单的方法是在谷歌地图上搜索位置,根据需要自定义大小并将嵌入代码作为 iframe。

1. Search the wanted location on Google maps
2. Choose "Share or embed map" from the top-right "hamburger" menu
3. Choose "Embed map" tab > "Custom size"
and you have the iframe code.

【讨论】:

  • 您的 javascript 解决方案有效,谢谢!我需要能够找到与给定邮政编码最近的标记 - iframe 方法会影响我这样做吗? (我的问题中的代码希望已经这样做了)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-12-27
  • 1970-01-01
  • 2020-04-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多