使用Google地圖 Javascript版

來源:互聯網
上載者:User

標籤:地圖   Google   

Google稱Map JavaScript V3版是同時為PC和行動裝置開發的,使用Html5。

首先需要在 Google Console 申請KEY,建立 一個 Browser key ,簡單demo就可以使用:

<!DOCTYPE html><html>  <head>    <title>Asynchronous Loading</title>    <meta name="viewport" content="initial-scale=1.0, user-scalable=no">    <meta charset="utf-8">    <style>      html, body, #map-canvas {        height: 100%;        margin: 0px;        padding: 0px      }    </style>    <script>function initialize() {  var mapOptions = {    zoom: 8,    center: new google.maps.LatLng(-34.397, 150.644)  };  var map = new google.maps.Map(document.getElementById(‘map-canvas‘),      mapOptions);}function loadScript() {  var script = document.createElement(‘script‘);  script.type = ‘text/javascript‘;  script.src = ‘https://maps.googleapis.com/maps/api/js?key=AIzaSyCR69jLZcjIGSw03n9_Q_ouR8tvLCHq59g&sensor=TRUE&callback=initialize‘;  document.body.appendChild(script);}window.onload = loadScript;    </script>  </head>  <body>    <div id="map-canvas"></div>  </body></html>

上傳到伺服器,直接點擊html檔案貌似沒法顯示的,,,
結果是這樣的,,,

好吧,https://mts0.googleapis.com/…被牆了。。
只好添加層了。。。

<!DOCTYPE html><html>  <head>    <title>Image map types</title>    <style>      html, body, #map-canvas {        height: 100%;        margin: 0px;        padding: 0px      }    </style>  </head>  <body>    <div id="map-canvas"></div>    <script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCR69jLZcjIGSw03n9_Q_ouR8tvLCHq59g&sensor=TRUE&callback=initialize"/></script>    <script>        var moonTypeOptions = {          getTileUrl: function(coord, zoom) {              var normalizedCoord = getNormalizedCoord(coord, zoom);              if (!normalizedCoord) {                return null;              }              var bound = Math.pow(2, zoom);              return ‘http://mt{1,2,3,0}.google.cn/vt/[email protected]&hl=zh-CN&gl=cn&x=‘+normalizedCoord.x+‘&y=‘+normalizedCoord.y+‘&z=‘+zoom+‘&s=Galil‘;          },          tileSize: new google.maps.Size(256, 256),          maxZoom: 9,          minZoom: 0,          radius: 1738000,          name: ‘Moon‘        };        var moonMapType = new google.maps.ImageMapType(moonTypeOptions);        function initialize() {          var myLatlng = new google.maps.LatLng(0, 0);          var mapOptions = {            center: myLatlng,            zoom: 1,            streetViewControl: false,            mapTypeControlOptions: {              mapTypeIds: [‘moon‘]            }          };          var map = new google.maps.Map(document.getElementById(‘map-canvas‘),              mapOptions);          map.mapTypes.set(‘moon‘, moonMapType);          map.setMapTypeId(‘moon‘);        }        // Normalizes the coords that tiles repeat across the x axis (horizontally)        // like the standard Google map tiles.        function getNormalizedCoord(coord, zoom) {          var y = coord.y;          var x = coord.x;          // tile range in one direction range is dependent on zoom level          // 0 = 1 tile, 1 = 2 tiles, 2 = 4 tiles, 3 = 8 tiles, etc          var tileRange = 1 << zoom;          // don‘t repeat across y-axis (vertically)          if (y < 0 || y >= tileRange) {            return null;          }          // repeat across x-axis          if (x < 0 || x >= tileRange) {            x = (x % tileRange + tileRange) % tileRange;          }          return {            x: x,            y: y          };        }        google.maps.event.addDomListener(window, ‘load‘, initialize);      </script>  </body></html>

好吧,暫時沒有運行起來,,在這行斷掉了,,tileSize: new google.maps.Size(256, 256),
暫時沒有解決。。。。。

換高德試試:
http://lbs.amap.com/api/javascript-api/example/d/0403-2/
我擦。。好給力。。。。。

demo:
http://www.erdian.net/mapdemo_amap.html

使用Google地圖 Javascript版

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.