標籤:地圖 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版