2012-09-20 48 views
1

大家好我嘗試使用geolocalization我webside與Joomla取得顯示地圖谷歌地圖JavaScript API第3

我所獲得的經度和緯度和精細...但是不要告訴我任何地圖 在我的代碼,我繪製地圖一個div內

這是我的代碼

<script type="text/javascript"> 
    function init() { 
    if (navigator.geolocation) { 
    navigator.geolocation.getCurrentPosition(function(position) { 
    var myLatlng = new google.maps.LatLng(position.coords.latitude,position.coords.longitude); 

    var mapOptions= { 
    zoom: 14, 
    center: myLatlng, 
    mapTypeId: google.maps.MapTypeId.ROADMAP 
    }; 

    var map = new google.maps.Map(document.getElementById('map'), mapOptions); 
    var marcador = new google.maps.Marker(); 
    marcador.setPosition(pos); 
    marcador.setMap(map); 

    }, function() { 
    alert("su navegador no acepta geolocalización"); 
    }); 
    } 
    } 
    google.maps.event.addDomListener(window, 'load', init); 
    </script> 

在我的腦子裏,我有這個

<script src="http://maps.googleapis.com/maps/api/js?sensor=false"></script> 
    <script type='text/javascript' src='http://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js?ver=3.1.2'></script> 

和他在我的身上

<div id="map"></div> 

我一直在閱讀其他職位同樣的問題,但我不覺得我的代碼錯誤。

謝謝

回答

1

pos在哪裏定義?這很可能會導致JS錯誤,從而阻止地圖顯示。

marcador.setPosition(pos); 
1

給你的DIV大小(寬度和高度):

<div id="map" style="width:400px;height:300px"></div> 
2

您的代碼中存在一些小錯誤 - 提示:嘗試在Chrome中使用Javascript控制檯查看以下錯誤:

  • 您在這裏沒有'pos'定義:marcador.setPosition(pos);
  • ...你想使用它創建一個標記,所以你必須使用經緯度對象
  • 給你的DIV的寬度和高度(如果你還沒有在你的CSS文件中完成它)

Here is a JSFiddle with all there errors fixed