从纬度和经度生成谷歌地图

时间:2015-11-07 21:01:09

标签: javascript google-maps

我需要使用纬度和经度在网页中生成谷歌地图。但是没有显示地图。我到目前为止尝试的内容如下 -

<script src="plugins/jQuery/jQuery-2.1.4.min.js"></script>
<script language=javascript src='http://maps.google.com/maps/api/js?sensor=false'></script>
<div id="map"></div>
<script>
   function initialize(){
        var myLatlng = new google.maps.LatLng(-25.363882,131.044922);
        var myOptions = {
            zoom: 4,
            center: myLatlng,
            mapTypeId: google.maps.MapTypeId.ROADMAP
            }
         map = new google.maps.Map(document.getElementById("map"), myOptions);
         var marker = new google.maps.Marker({
             position: myLatlng, 
             map: map,
         title:"Fast marker"
        });
   } 

   google.maps.event.addDomListener(window,'load', initialize); 
   $(document).ready(function (){
        var myLatlng = new google.maps.LatLng(-25.363882,131.044922);
        var myOptions = {
            zoom: 4,
            center: myLatlng,
            mapTypeId: google.maps.MapTypeId.ROADMAP
            }
         map = new google.maps.Map($('#map'), myOptions);
         var marker = new google.maps.Marker({
             position: myLatlng, 
             map: map,
             title:"Fast marker"
        });
   }                                
</script>

我如何生成地图?找不到任何错误。请帮我。

1 个答案:

答案 0 :(得分:1)

您要多次添加代码

这有效:

<!DOCTYPE html>
<html>
<head>
<script
src="http://maps.googleapis.com/maps/api/js">
</script>

<script>
 function initialize(){
        var myLatlng = new google.maps.LatLng(-25.363882,131.044922);
        var myOptions = {
            zoom: 4,
            center: myLatlng,
            mapTypeId: google.maps.MapTypeId.ROADMAP
            }
         map = new google.maps.Map(document.getElementById("map"), myOptions);
         var marker = new google.maps.Marker({
             position: myLatlng, 
             map: map,
         title:"Fast marker"
        });
   } 

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

</script>
</head>

<body>
<div id="map" style="width:500px;height:380px;"></div>

</body>
</html>

小提琴示例:https://jsfiddle.net/eugensunic/wexd3spp/1/

CodePen示例:http://codepen.io/anon/pen/QjZgpw