当前位置: 首页 > 工具软件 > GMaps.js > 使用案例 >

Js 对 Google Maps API 实际应用

相俊迈
2023-12-01

谷歌地图api的实际应用地址:

https://developers.google.com/maps/documentation/javascript/tutorial?hl=zh-cn

具体代码如下:

方法一:

<!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.googleapis.com/maps/api/js?key=YOUR_API_KEY&sensor=SET_TO_TRUE_OR_FALSE">
    </script>
    <script type="text/javascript">
      function initialize() {
        var mapOptions = {
          center: new google.maps.LatLng(-34.397, 150.644),
          zoom: 8,
          mapTypeId: google.maps.MapTypeId.ROADMAP
        };
        var map = new google.maps.Map(document.getElementById("map_canvas"),
            mapOptions);
      }
    </script>
  </head>
  <body onload="initialize()">
    <div id="map_canvas" style="width:100%; height:100%"></div>
  </body>
</html>
方法二如下:
<!DOCTYPE html>
<html>
<head>
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
    <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
    <title>搜索地图</title>
    <link href="../../Content/map/default.css" rel="stylesheet" />
    <script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=false"></script>
    <script src="../../Content/map/map.js"></script>
</head>
<body οnlοad="initialize()">
    <div id="show">
        <input id="address" type="textbox">
        <input id="btnSearch" type="button" value="搜索" οnclick="codeAddress()">
    </div>
    <div id="map_canvas" style="height: 90%; top: 30px"></div>
    <div id="foot"></div>
</body>
</html>
map.js代码如下:
var geocoder;
var map;
function initialize() {
    geocoder = new google.maps.Geocoder();
    var latlng = new google.maps.LatLng(31.23, 120.57);//昆山(北纬31.23 东经120.57)
    var myOptions = {
        zoom: 6,
        center: latlng,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    }
    map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
}


function codeAddress() {
    var address = document.getElementById("address").value;
    geocoder.geocode({ 'address': address }, function (results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
            map.setCenter(results[0].geometry.location);
            var marker = new google.maps.Marker({
                map: map,
                position: results[0].geometry.location
            });
        } else {
            alert("搜索失败,原因如下: " + status);
        }
    });
}
default.css代码如下:
html, body {
  height: 100%;
  margin: 0;
  padding: 0;
}


#map-canvas, #map_canvas {
  height: 100%;
}


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


  #map_canvas {
    height: 650px;
  }
}


#show {
    text-align: center;
    padding-top: 25px;
}


#Textbox1 {
    width: 350px;
    height: 35px;
}


#btnSearch {
    color: white;
    background-color: blue;
    width: 120px;
    height: 40px;
}


#foot {
    position: fixed;
    bottom: 0px;
    width: 100%;
    height: 30px;
    background: #DDD;
}

相关网址:http://map.sogou.com/api/documentation/javascript/api2.5/reference.html


 类似资料: