參考文檔:Class:服務類/LocalCityjavascript
代碼片段:php
javascriptvar myCity = new BMap.LocalCity(); myCity.get( function(result){ console.log(result.name); //城市名稱 console.log(result.code); //城市代碼 console.log(result.level); //當前地圖Zoom的Level console.log(result.center.lat); //當前城市中心點維度 console.log(result.center.lng); //當前城市中心點經度 });
一個完整的實例:css
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>Hello, World</title> <style type="text/css"> html{height:100%} body{height:100%;margin:0px;padding:0px} #container{height:100%} </style> <script type="text/javascript" src="http://api.map.baidu.com/api?v=1.5&ak=83E4862c1e77e7b01856563eb22e334b"> </script> </head> <body> <div id="container"></div> <script type="text/javascript"> var myCity = new BMap.LocalCity(); var map = new BMap.Map("container"); // 建立Map實例 myCity.get( function(result){ // 初始化地圖,設置中心點座標和地圖級別 map.centerAndZoom(new BMap.Point(result.center.lng, result.center.lat), result.level); //添加地圖類型控件 map.addControl(new BMap.MapTypeControl()); //開啓鼠標滾輪縮放 map.enableScrollWheelZoom(true); }); </script> </body> </html>