2014-01-23 137 views
1

我正尝试使用Google Maps API设置一个简单的页面,以在位置上显示标记。但是,我没有得到这个代码,这是基于谷歌示例代码here。我改变了源从使用Google Maps API显示标记

"https://maps.googleapis.com/maps/api/js?key=API_KEY&sensor=false" 

"http://maps.google.com/maps/api/js?sensor=false" 

,因为有人建议here避免使用API​​密钥的目的。 var标记声明从谷歌here复制。什么必须改变我的标记出现?


<!DOCTYPE html> 
<html> 
    <head> 
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no" /> 
    <style type="text/css"> 
     html { height: 100% } 
     body { height: 100%; margin: 0; padding: 0 } 
     #map-canvas { height: 100% } 
    </style> 
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script> 
    </script> 
    <script type="text/javascript"> 
     function initialize() { 

     var location = new google.maps.LatLng(59.272, 10.418); 

     var mapOptions = { 
      center: location, 
      zoom: 8 
     }; 

     var marker = new google.maps.Marker({ 
      position: location, 
      map: map, 
      title: 'Hello World!' 
     }); 

     var map = new google.maps.Map(document.getElementById("map-canvas"), 
      mapOptions); 
     } 
     google.maps.event.addDomListener(window, 'load', initialize); 
    </script> 
    </head> 
    <body> 
    <div id="map-canvas"/> 
    </body> 
</html> 

回答

5

招行建立行设立标志之前的地图,作为标记使用其调用地图:

function initialize() { 
    var location = new google.maps.LatLng(59.272, 10.418); 

    var mapOptions = { 
    center: location, 
    zoom: 8 
    }; 

    var map = new google.maps.Map(document.getElementById("map-canvas"), mapOptions); 

    var marker = new google.maps.Marker({ 
    position: location, 
    map: map, 
    title: 'Hello World!' 
    }); 
} 
+0

伟大的思想:)。 – Swires

+0

谢谢@Andy。你节省了我的时间。我还有一个疑问。我想要显示标记窗口的详细信息。你能建议我吗? –

0

移动您的标记申报到地图声明之后,然后标记分配给图所示:

var map = new google.maps.Map(document.getElementById("map-canvas"), 
     mapOptions); 

var marker = new google.maps.Marker({ 
     position: location, 
     map: map, 
     title: 'Hello World!' 
    }); 

marker.setMap(map); 

注意,设置地图是可选的,可而不是在标记实例化时设置地图。