2016-10-04 125 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