Google Maps JavaScript API 工具|专案实作

串接地图 JavaScript API 中虽然相较起来难度较高,不过官方文件写的也很简单易懂。

使用方式

1.宣告 HTML5 文件,将 JS CDN 引入档案(要先申请金钥)

<!DOCTYPE html><html lang="en"><head>    <meta charset="UTF-8">    <meta http-equiv="X-UA-Compatible" content="IE=edge">    <meta name="viewport" content="width=device-width, initial-scale=1.0">    <title>google maps api</title>    <script async    src="https://maps.googleapis.com/maps/api/js?key=你的金钥&callback=initMap">    </script></head><body>    <div id="map"></div></body></html>

2.设定 map 容器的高度为100%,否则会看不到内容

<style>    #map {        height: 100%;    }    /* Optional: Makes the sample page fill the window. */    html,    body {        height: 100%;        margin: 0;        padding: 0;    }</style>
最后,设定地图呈现的参数
<script>  let map;  function initMap() {      map = new google.maps.Map(document.getElementById("map"), {        // 中心点位置        center: { lat: 23.197250366210938, lng: 119.42967987060547 },                zoom: 18, // 地图缩放比例 (0-20)        maxZoom: 20, // 使用者能缩放地图的最大比例         minZoom: 3, // 使用者能缩放地图的最小比例                streetViewControl: false, // 是否显示右下角街景小人        mapTypeControl: false // 使用者能否切换地图样式:一般、卫星图等      });  }</script>

参考来源:https://developers.google.com/maps/documentation/javascript/overview#maps_map_simple-html


关于作者: 网站小编

码农网专注IT技术教程资源分享平台,学习资源下载网站,58码农网包含计算机技术、网站程序源码下载、编程技术论坛、互联网资源下载等产品服务,提供原创、优质、完整内容的专业码农交流分享平台。

热门文章