2016-10-04 133 views
-1

我正在使用谷歌地图API生成地图。通过使用下面的代码,我可以在谷歌浏览器上生成地图,但它不适用于Firefox。此外,在控制台上没有JS错误,并且在firefox上没有生成相应的html。谷歌地图不显示在Firefox上

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

你可以发布一些小提琴吗? – xxxmatko

+0

geocoder = new google.maps.Geocoder();和infowindow = new google.maps.InfoWindow(); - 尝试将这些行放入initialize()中。 (你可以保持声明全局)。 –

+1

如果'navigator.geolocation'为false,那么你不用'myLatlng'作为参数调用'show_map'。 – GramThanos

回答

0

只需调用像initialize();这样的初始化方法,它就可以工作。

我已经让你成为小提琴here