谷歌地图没有在Firefox上显示

时间:2016-10-04 09:39:54

标签: javascript jquery google-maps firefox

我使用谷歌地图api生成地图。通过使用以下代码,我能够在谷歌浏览器上生成地图,但它在Firefox中无效。此外,在firefox上没有生成控制台和相应的html上没有JS错误。

var map;
var marker;
var lat;
var lng;
var myLatlng;
var geocoder = new google.maps.Geocoder();
var infowindow = new google.maps.InfoWindow();

function initialize(){

    if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(showPosition);
    } else {
        myLatlng = new google.maps.LatLng(-23.69748,133.88362);
    }

    function showPosition(position) {
        lat = position.coords.latitude;
        lng = position.coords.longitude;
        show_map(lat,lng);
    }

    function show_map(lat,lng){

        var mapOptions = {
            zoom: 18,
            mapTypeId: google.maps.MapTypeId.ROADMAP
        };

        map = new google.maps.Map(document.getElementById("myMap"), mapOptions);
        map.setCenter(new google.maps.LatLng(lat,lng));
        map.setZoom(8);

        marker = new google.maps.Marker({
            map: map,
            position: new google.maps.LatLng(lat,lng),
            draggable: true
        });

        geocoder.geocode({'latLng': new google.maps.LatLng(lat,lng) }, function(results, status) {

            if (status == google.maps.GeocoderStatus.OK) {
                if (results[0]) {
                    infowindow.setContent(results[0].formatted_address);
                    infowindow.open(map, marker);
                }
            }

        });

    }

}

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

1 个答案:

答案 0 :(得分:0)

只需调用initialize();之类的初始化方法即可。

我让你成了一个工作小提琴here