谷歌地图集成到网站

时间:2011-03-04 05:59:56

标签: google-maps

我想创建一个Web应用程序,它将获得2个位置及其邮政编码,并在谷歌地图上显示结果。例如,我选择2个城市或国家/地区,并根据我的要点显示带有彩色线条的路线图。

2 个答案:

答案 0 :(得分:3)

最佳观看地点是Google Maps API V3文档 - 我推荐V3,因为他们不再支持V2了

主要文件:http://code.google.com/apis/maps/documentation/javascript/

样品:http://code.google.com/apis/maps/documentation/javascript/examples/index.html

简单指示示例:http://code.google.com/apis/maps/documentation/javascript/examples/directions-simple.html

基本上你需要有两个坐标,虽然你可以使用街道地址,并将其传递给API,然后谷歌获得结果,然后绘制它。很容易就是馅饼!

答案 1 :(得分:2)

将此代码另存为location.html

<!DOCTYPE html> 
<html> 
  <head> 
    <title>Google Maps JavaScript API v3 Example: Map Geolocation</title> 
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no"> 
    <meta charset="UTF-8"> 
    <link href="map.css" rel="stylesheet" type="text/css"> 
<!--
Include the maps javascript with sensor=true because this code is using a
sensor (a GPS locator) to determine the user's location.
See: http://code.google.com/apis/maps/documentation/javascript/basics.html#SpecifyingSensor
--> 
<script type="text/javascript"
    src="http://maps.googleapis.com/maps/api/js?sensor=false"></script> 

<script type="text/javascript"> 
var siberia = new google.maps.LatLng(37.625364,-122.423905);

function initialize() {
  var myOptions = {
    zoom:19,
    mapTypeId: google.maps.MapTypeId.ROADMAP
  };
  var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
  var infowindow = new google.maps.InfoWindow({
          map: map,
          position: siberia,
          content: 'Location found using HTML5.'
        });

  var marker =  new google.maps.Marker({
    position: siberia,
    map: map,
    title: "omt"
  });
  map.setCenter(siberia);

}
  google.maps.event.addDomListener(window, 'load', initialize);
    </script> 
  </head> 
  <body> 
    <div id="map_canvas"></div> 
  </body> 
</html> 

将此代码另存为map.css

html, body {
  height: 100%;
  margin: 0;
  padding: 0;
}

#map_canvas {
  height: 100%;
}

@media print {
  html, body {
    height: auto;
  }

#map_canvas {
height: 650px;
}
}