-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
117 lines (101 loc) · 4.5 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
define(function (require, exports, module) {
var $ = require("$"),
_ = require("underscore");
if (!window.initData) return;
var map = new google.maps.Map($("#map")[0], {
zoom: window.initData.zoom,
center: new google.maps.LatLng(+window.initData.lat, +window.initData.lng),
mapTypeId: google.maps.MapTypeId.ROADMAP
});
var markerPool = [];
var coordCache = {};
var infoWindow = new google.maps.InfoWindow({
"zIndex": 120
});
function clearMarker() {
_.each(markerPool, function (v, k) {
v.setMap(null);
});
markerPool = [];
coordCache = {};
}
function latlng2tilecoordinate(lon, lat, nZoom) {
var x = (Math.floor((Number(lon) + 180) / 360 * Math.pow(2, nZoom))),
y = (Math.floor((1 - Math.log(Math.tan(lat * Math.PI / 180) + 1 / Math.cos(lat * Math.PI / 180)) / Math.PI) / 2 * Math.pow(2, nZoom)));
return [x, y, nZoom].join(",");
}
function fetchPlace() {
clearMarker();
var bounds = map.getBounds();
$.ajax({
url: window.initData.dataurl + "/bounds-" + [bounds.getNorthEast().toUrlValue(10), bounds.getSouthWest().toUrlValue(10)].join(",") + "/",
cache: false,
dataType: "json",
success: function (data) {
clearMarker();
var currentZoom = map.getZoom(),
render = function (dt) {
_.each(dt, function (v, k) {
// show original image, like: ./public/attachment/201303/513d42fbed690.jpg
if (v.image) {
v.image = v.image.replace("./", "http://www.suzhoutong.com/");
}
// show count
else if (v.count) {
v.title = "此处有" + v.count + "条优惠信息";
v.image = "http://chart.apis.google.com/chart?chst=d_map_pin_letter_withshadow&chld=" + v.count + "|FE6256|000000";
v.desc = "此处有" + v.count + "条优惠信息";
}
var marker = new google.maps.Marker({
title: v.title,
position: new google.maps.LatLng(+v.lat, +v.lng),
map: map,
icon: {
url: v.image,
size: new google.maps.Size(32, 32)
}
//new google.maps.MarkerImage(v.image)
});
google.maps.event.addListener(marker, "click", function () {
infoWindow.open(map, this);
infoWindow.setContent(v.desc || '<a href="' + v.link + '" target="_blank" style="text-decoration: none;"><img src="' + v.image + '" style="height: 260px;" /><span>' + v.title + '</span></a>');
});
markerPool.push(marker);
});
};
if (currentZoom >= 16) {
render(data.place);
} else {
_.each(data.place, function (v, k) {
var coord = latlng2tilecoordinate(v.lng, v.lat, currentZoom);
if (coordCache[coord]) {
coordCache[coord].push(v);
} else {
coordCache[coord] = [v];
}
});
var tmpData = [];
_.each(coordCache, function (v, k) {
if (v.length > 1) {
tmpData.push({
"lat": v[0].lat,
"lng": v[0].lng,
"count": v.length,
"zoom": currentZoom
});
} else {
tmpData.push(v[0]);
}
});
render(tmpData);
}
},
error: function (e) {
seajs.log(e);
}
});
}
google.maps.event.addListener(map, "tilesloaded", _.throttle(function () {
fetchPlace();
}, 500));
});