icon可以從網站目錄中取得
或從一個絕對的圖片路徑
但這些marker都是靜態的
也就是預先產生做好的icon
最近有個案子需求
marker必須是有意義的
例如長條圖、圓餅圖來代表該區域的特殊意義
於是就得動態產生這些marker
於是我就用ajax+泛型處理+製圖元件來達到
以下是長條圖的範例
javascript
- function createHistogramLayer(parameters) {
- $.ajax({
- url: "./Handler/HandlerHistogram.ashx" + '?' + $.param(parameters),
- type: "POST",
- dataType: "json",
- success: function (Jdata) {
- var i = 0;
- for (var numOfIndex in Jdata) {
- var latLng = new google.maps.LatLng(Jdata[numOfIndex].y, Jdata[numOfIndex].x);
- LayerMarker[i] = new google.maps.Marker({
- position: latLng,
- map: map,
- icon: Jdata[numOfIndex].icon,
- title: Jdata[numOfIndex].name + "\r\n" + Jdata[numOfIndex].desc
- });
- i++;
- }
- },
- error: function () {
- alert("讀取資料發生錯誤");
- }
- });
- }
依照所輸入的條件,產生不同的長條圖
這裡我用到的製圖元件是ChartDirector
json 格式
- [
- {
- "id": "64",
- "name": "三灣鄉",
- "icon":"./images/tmp/20151020023106047.png",
- "desc":"101:11 / 102:10 / 103:5",
- "x":"120.95375",
- "y":"24.63135"
- },
- {
- "id": "61",
- "name": "三義鄉",
- "icon":"./images/tmp/20151020023106216.png",
- "desc":"101:2 / 102:1 / 103:4",
- "x":"120.7736",
- "y":"24.3794"
- },
- {
- "id": "57",
- "name": "公館鄉",
- "icon":"./images/tmp/20151020023106368.png",
- "desc":"101:3 / 102:0 / 103:1",
- "x":"120.85935",
- "y":"24.49795"
- },
- {
- "id": "55",
- "name": "卓蘭鎮",
- "icon":"./images/tmp/20151020023106521.png",
- "desc":"101:0 / 102:0 / 103:1",
- "x":"120.8434",
- "y":"24.32675"
- },
- {
- "id": "59",
- "name": "南庄鄉",
- "icon":"./images/tmp/20151020023106677.png",
- "desc":"101:10 / 102:1 / 103:9",
- "x":"121.01565",
- "y":"24.5692"
- },
- {
- "id": "56",
- "name": "大湖鄉",
- "icon":"./images/tmp/20151020023106832.png",
- "desc":"101:6 / 102:6 / 103:3",
- "x":"120.84755",
- "y":"24.39825"
- },
- {
- "id": "54",
- "name": "後龍鎮",
- "icon":"./images/tmp/20151020023107002.png",
- "desc":"101:0 / 102:0 / 103:1",
- "x":"120.7743",
- "y":"24.6168"
- },
- {
- "id": "66",
- "name": "泰安鄉",
- "icon":"./images/tmp/20151020023107164.png",
- "desc":"101:4 / 102:0 / 103:2",
- "x":"121.08275",
- "y":"24.4047"
- },
- {
- "id": "65",
- "name": "獅潭鄉",
- "icon":"./images/tmp/20151020023107320.png",
- "desc":"101:9 / 102:1 / 103:1",
- "x":"120.9196",
- "y":"24.52285"
- },
- {
- "id": "52",
- "name": "竹南鎮",
- "icon":"./images/tmp/20151020023107504.png",
- "desc":"101:1 / 102:0 / 103:0",
- "x":"120.87935",
- "y":"24.6943"
- },
- {
- "id": "50",
- "name": "苑裡鎮",
- "icon":"./images/tmp/20151020023107654.png",
- "desc":"101:1 / 102:1 / 103:1",
- "x":"120.6851",
- "y":"24.4092"
- },
- {
- "id": "49",
- "name": "苗栗市",
- "icon":"./images/tmp/20151020023108122.png",
- "desc":"101:2 / 102:0 / 103:0",
- "x":"120.8117",
- "y":"24.5589"
- },
- {
- "id": "62",
- "name": "西湖鄉",
- "icon":"./images/tmp/20151020023108280.png",
- "desc":"101:2 / 102:1 / 103:0",
- "x":"120.7583",
- "y":"24.53435"
- },
- {
- "id": "51",
- "name": "通霄鎮",
- "icon":"./images/tmp/20151020023108439.png",
- "desc":"101:4 / 102:2 / 103:3",
- "x":"120.70445",
- "y":"24.48915"
- },
- {
- "id": "63",
- "name": "造橋鄉",
- "icon":"./images/tmp/20151020023108565.png",
- "desc":"101:14 / 102:10 / 103:3",
- "x":"120.8814",
- "y":"24.6279"
- },
- {
- "id": "58",
- "name": "銅鑼鄉",
- "icon":"./images/tmp/20151020023108665.png",
- "desc":"101:1 / 102:0 / 103:8",
- "x":"120.7981",
- "y":"24.4609"
- },
- {
- "id": "53",
- "name": "頭份鎮",
- "icon":"./images/tmp/20151020023108768.png",
- "desc":"101:8 / 102:7 / 103:4",
- "x":"120.92655",
- "y":"24.6692"
- },
- {
- "id": "60",
- "name": "頭屋鄉",
- "icon":"./images/tmp/20151020023108876.png",
- "desc":"101:9 / 102:2 / 103:2",
- "x":"120.88515",
- "y":"24.57145"
- }
- ]
以下是圓餅圖的範例
javascript
- function createViolationLayer(parameters) {
- $.ajax({
- url: "./Handler/HandlerViolation.ashx" + '?' + $.param(parameters),
- type: "POST",
- dataType: "json",
- success: function (Jdata) {
- var i = 0;
- for (var numOfIndex in Jdata) {
- var latLng = new google.maps.LatLng(Jdata[numOfIndex].y, Jdata[numOfIndex].x);
- LayerMarker[i] = new google.maps.Marker({
- position: latLng,
- map: map,
- icon: Jdata[numOfIndex].icon,
- title: Jdata[numOfIndex].name + "\r\n違規:" + Jdata[numOfIndex].yes + " / 非違規:" + Jdata[numOfIndex].no
- });
- i++;
- }
- },
- error: function () {
- alert("讀取資料發生錯誤");
- }
- });
- }
- [
- {
- "id": "64",
- "name": "三灣鄉",
- "icon":"./images/tmp/20151020025403819.png",
- "yes":"26",
- "no":"97",
- "x":"120.95375",
- "y":"24.63135"
- },
- {
- "id": "61",
- "name": "三義鄉",
- "icon":"./images/tmp/20151020025403968.png",
- "yes":"7",
- "no":"47",
- "x":"120.7736",
- "y":"24.3794"
- },
- {
- "id": "57",
- "name": "公館鄉",
- "icon":"./images/tmp/20151020025404099.png",
- "yes":"4",
- "no":"74",
- "x":"120.85935",
- "y":"24.49795"
- },
- {
- "id": "55",
- "name": "卓蘭鎮",
- "icon":"./images/tmp/20151020025404237.png",
- "yes":"1",
- "no":"75",
- "x":"120.8434",
- "y":"24.32675"
- },
- {
- "id": "59",
- "name": "南庄鄉",
- "icon":"./images/tmp/20151020025404375.png",
- "yes":"20",
- "no":"81",
- "x":"121.01565",
- "y":"24.5692"
- },
- {
- "id": "56",
- "name": "大湖鄉",
- "icon":"./images/tmp/20151020025404511.png",
- "yes":"15",
- "no":"127",
- "x":"120.84755",
- "y":"24.39825"
- },
- {
- "id": "54",
- "name": "後龍鎮",
- "icon":"./images/tmp/20151020025404652.png",
- "yes":"1",
- "no":"39",
- "x":"120.7743",
- "y":"24.6168"
- },
- {
- "id": "66",
- "name": "泰安鄉",
- "icon":"./images/tmp/20151020025404790.png",
- "yes":"6",
- "no":"120",
- "x":"121.08275",
- "y":"24.4047"
- },
- {
- "id": "65",
- "name": "獅潭鄉",
- "icon":"./images/tmp/20151020025404930.png",
- "yes":"11",
- "no":"43",
- "x":"120.9196",
- "y":"24.52285"
- },
- {
- "id": "52",
- "name": "竹南鎮",
- "icon":"./images/tmp/20151020025405068.png",
- "yes":"1",
- "no":"32",
- "x":"120.87935",
- "y":"24.6943"
- },
- {
- "id": "50",
- "name": "苑裡鎮",
- "icon":"./images/tmp/20151020025405207.png",
- "yes":"3",
- "no":"20",
- "x":"120.6851",
- "y":"24.4092"
- },
- {
- "id": "49",
- "name": "苗栗市",
- "icon":"./images/tmp/20151020025405343.png",
- "yes":"2",
- "no":"38",
- "x":"120.8117",
- "y":"24.5589"
- },
- {
- "id": "62",
- "name": "西湖鄉",
- "icon":"./images/tmp/20151020025405488.png",
- "yes":"3",
- "no":"13",
- "x":"120.7583",
- "y":"24.53435"
- },
- {
- "id": "51",
- "name": "通霄鎮",
- "icon":"./images/tmp/20151020025405634.png",
- "yes":"9",
- "no":"42",
- "x":"120.70445",
- "y":"24.48915"
- },
- {
- "id": "63",
- "name": "造橋鄉",
- "icon":"./images/tmp/20151020025405774.png",
- "yes":"27",
- "no":"60",
- "x":"120.8814",
- "y":"24.6279"
- },
- {
- "id": "58",
- "name": "銅鑼鄉",
- "icon":"./images/tmp/20151020025405913.png",
- "yes":"9",
- "no":"99",
- "x":"120.7981",
- "y":"24.4609"
- },
- {
- "id": "53",
- "name": "頭份鎮",
- "icon":"./images/tmp/20151020025406053.png",
- "yes":"19",
- "no":"62",
- "x":"120.92655",
- "y":"24.6692"
- },
- {
- "id": "60",
- "name": "頭屋鄉",
- "icon":"./images/tmp/20151020025406195.png",
- "yes":"13",
- "no":"76",
- "x":"120.88515",
- "y":"24.57145"
- }
- ]
希望提供有這需求的人參考
沒有留言:
張貼留言