简体   繁体   English

无法使用地理位置加载Google Maps API

[英]Unable to load Google Maps API with Geolocation

I followed the documentation on Google itself regarding the Map API and other sources to try to figure out what's wrong but after a few hours of trying it still doesnt work.. 我按照Google本身关于Map API和其他来源的文档来试图弄清楚什么是错的但是经过几个小时的尝试它仍然无法工作..

Could anyone please guide me into solving it? 有人可以指导我解决它吗?

<div class="generatedMap"></div>
                <div class="generatedMap"></div>
                <script src="//maps.googleapis.com/maps/api/js?v=3&client=gme-KEYFROMGOOGLEAPI" type="text/javascript"></script>
                <script type="text/javascript">
                    function initMap() {
                        /*var latlng = new google.maps.LatLng(latitude, longitude);
                        var mapOptions = {
                            zoom: 5,
                            center: new google.maps.LatLng(latitude, longitude)
                        }
                        map = new google.maps.Map(document.getElementById('generatedMap'), mapOptions);*/

                        var latitudeHF = document.getElementById("<%=HF_Latitude.ClientID %>");
                        var longitudeHF = document.getElementById("<%=HF_Longitude.ClientID %>");

                        var latitude = 0.0;
                        var longitude = 0.0;

                        if (latitudeHF)
                        {
                            latitude = latitudeHF.value;
                        }

                        if (longitudeHF) {
                            longitude = longitudeHF.value;
                        }

                        var map = new google.maps.Map(document.getElementById('generatedMap'), {
                            zoom: 8,
                            center: new google.maps.LatLng(latitude, longitude),
                            mapTypeId: google.maps.MapTypeId.SATELLITE
                        });

                        var marker = new google.maps.Marker({
                            position: new google.maps.LatLng(latitude, longitude),
                            map: map,
                            title: "<div style = 'height:60px;width:150px'><b>Consumer's location:</b><br />Latitude: " + latitude + "<br />Longitude: " + longitude
                        });
                    }

                    google.maps.event.addDomListener(window, 'load', initMap);
                </script>

I'm actually retrieving the saved latitude and longitude from my database, then storing it into a HiddenField and retrieving it from there.. 我实际上是从我的数据库中检索保存的纬度和经度,然后将其存储到HiddenField中并从那里检索它。

foreach (DataRow r in ds.Tables[0].Rows)
        {
            accountDB = r["AccountStatus"].ToString();
            latitudeDB = Convert.ToDouble(r["Latitude"]);
            longitudeDB = Convert.ToDouble(r["Longitude"]);
            usernameDB = r["Username"].ToString();
            ipDB = r["IPAddressOfCreation"].ToString();
        }

HF_Latitude.Value = latitudeDB.ToString();
HF_Longitude.Value = longitudeDB.ToString();

I removed the Google Map API key for illustration purposes.. 我删除了Google Map API密钥以用于说明目的。

I got both the latitude and longitude displayed as shown in the picture but i cant seem to display the map.. 我得到的纬度和经度显示如图所示,但我似乎无法显示地图..

显示

Appreciate any help please! 请欣赏任何帮助!

Mistake into your code in Div tag instead of class you need to define id as attributes. 错误地将您的代码定义为Div标签而不是class您需要将id定义为属性。

It's better you just this script src for google map. 这个脚本src对谷歌地图来说更好。

<script src="https://maps.googleapis.com/maps/api/js?key=Google-api-key&callback=initMap" />

Once you are using above script src then remove this line from your code. 一旦你使用上面的脚本src然后从你的代码中删除此行。

google.maps.event.addDomListener(window, 'load', initMap);

Instead of : 代替 :

<div class="generatedMap"></div>

Should be : 应该 :

<div id="generatedMap"></div>

Javascript code will remain as it is. Javascript代码将保持不变。 if you wanted to keep class as it is then use blow code in javascript 如果你想保持课程,那么在javascript中使用打击代码

Instead of : 代替 :

var map = new google.maps.Map(document.getElementById('generatedMap')

Should be : 应该 :

var map = new google.maps.Map(document.getElementsByClassName('generatedMap')

Hope this helps. 希望这可以帮助。

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

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