显示地图Google Maps JavaScript API v3

时间:2012-09-20 12:42:35

标签: javascript google-maps joomla

大家好我尝试使用joomla

制作的网站使用地理定位

我获得了纬度和经度并且很好......但是没有给我看任何地图 在我的代码中,我在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>

我已经阅读了同样问题的其他帖子,但我在代码中找不到错误。

由于

2 个答案:

答案 0 :(得分:2)

您的代码中存在一些小错误 - 提示:尝试使用Chrome中的Javascript控制台查看这些错误:

  • 此处未定义'pos':marcador.setPosition(pos);
  • ...并且您想要使用它创建标记,因此您必须使用LatLng对象
  • 给你的div一个宽度和高度(如果你还没有在你的CSS文件中完成)

Here is a JSFiddle with all there errors fixed

答案 1 :(得分:1)

给你的div一个大小(宽度和高度):

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