简体   繁体   中英

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..

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..

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..

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.

It's better you just this script src for google map.

<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.

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. if you wanted to keep class as it is then use blow code in 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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