DIV CSS 佈局教程網

用 SOSO 地圖 JS 畫出標註與中心點的 HTML 範例
編輯:關於JavaScript     

本文直接提供一份可在本地建立 HTML 檔後執行的完整程式碼,示範如何用 SOSO 地圖的 JavaScript API 畫出地圖、設定中心點,並放置多個標註(marker)。

完整程式碼

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>Map</title>
<script charset="utf-8" src="https://map.soso.com/api/v2/main.js"></script>
<script>
function init() {
  var center = new soso.maps.LatLng(31.15953, 121.516035);
  var map = new soso.maps.Map(
    document.getElementById("container"),
    {
      center: center, // 地圖中心位置
      zoom: 15         // 縮放級別
    }
  );

  // 第一個標註的座標
  var position1 = new soso.maps.LatLng(31.15751, 121.514061);
  var marker = new soso.maps.Marker({
    position: position1,
    map: map
  });

  // 第二個標註的座標
  var position2 = new soso.maps.LatLng(31.160705, 121.524017);
  var marker = new soso.maps.Marker({
    position: position2,
    map: map
  });
}

function loadScript() {
  var script = document.createElement("script");
  script.type = "text/javascript";
  script.src = "https://map.soso.com/api/v2/main.js?callback=init";
  document.body.appendChild(script);
}
window.onload = loadScript;
</script>
</head>
<body onload="init()">
<div id="container" style="width:1000px; height:800px"></div>
</body>
</html>

程式說明

整體流程是這樣的:先用 soso.maps.LatLng 建立一個經緯度座標作為地圖中心,接著以 soso.maps.Map 把地圖渲染到 id 為 container 的 div 上,並透過 zoom 參數控制縮放級別。要加上標註時,只要用另一組 LatLng 座標建立 soso.maps.Marker 並關聯到同一個 map 物件即可,重複這個步驟就能放上一個又一個的標記點。這種「中心點 + 多標註」的組合,是各種地圖應用最基礎也最常用的起手式。

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved