-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbingmap.html
232 lines (173 loc) · 8.57 KB
/
bingmap.html
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
<!DOCTYPE html>
<html>
<head>
<title>loadmapasyncHTML</title>
<style type='text/css'>body{margin:0;padding:0;overflow:hidden;font-family:'Segoe UI',Helvetica,Arial,Sans-Serif}</style>
<meta http-equiv='Content-Type' content='text/html; charset=utf-8'/>
<script type='text/javascript' src='https://www.bing.com/api/maps/mapcontrol?key=' async defer></script>
<script type='text/javascript'>
var entities = [
{ "name":"vincent","position":{"latitude":13.107673631379782,"longitude":80.29390144098141},"color":"orange","description":"programmer" },
{ "name":"robert","position":{"latitude":13.092488900156042,"longitude":80.18744992959836},"color":"green","description":"analyst"}
]
var map;
var zoomValue=4;
/*https://blogs.bing.com/maps/2011/09/16/dev-tip-repositioning-an-infobox*/
function loadMapScenario()
{
var map = new Microsoft.Maps.Map(document.getElementById('myMap'), {
/* No need to set credentials if already passed in URL */
center: new Microsoft.Maps.Location(13.079454766739488, 80.27396726358273),
mapTypeId: Microsoft.Maps.MapTypeId.aerial,
zoom: zoomValue/*13*/
});
var pushpins=[];
var infoBoxes=[];
//Create an infobox at the center of the map but don't show it.
infobox = new Microsoft.Maps.Infobox(map.getCenter(), {
visible: false
});
//Assign the infobox to a map instance.
infobox.setMap(map);
var infoboxTemplate = '<div id="infoboxText" style="background-color:White; border-style:solid; border-width:medium; border-color:lightgrey; padding:5px;min-height:100px; width: 240px;margin-top:{marginTop}px;margin-left:{marginLeft}px;">' +
'<div id="infoboxTitle" style="">{title}</b>' +
'<div id="infoboxDescription" style="">{description}</a></div>';
/*Microsoft.Maps.Events.addHandler(map, 'viewchangeend', reactToMapMove);*/
//Create a ClusterLayer with options and add it to the map.
Microsoft.Maps.loadModule("Microsoft.Maps.Clustering", function () {
for ( var i=0; i<entities.length ; i++)
{
pushpins[i] = createCirclePushpin(new Microsoft.Maps.Location(entities[i].position.latitude,entities[i].position.longitude), 15, 'rgba(255, 255, 255, 0.8)', entities[i].color, 7,'black',i,entities[i].name,entities[i].description);
/*pushpins[i] = createRegularPushpin(new Microsoft.Maps.Location(entities[i].position.latitude,entities[i].position.longitude),i,entities[i].name,entities[i].description);*/
/*map.entities.push(pushpins[i]);*/
pushpins[i].metadata = {
title: entities[i].name,
description: entities[i].description
};
Microsoft.Maps.Events.addHandler(pushpins[i], 'click', zoomIn);
Microsoft.Maps.Events.addHandler(pushpins[i], 'mouseover', showInfoBox);
Microsoft.Maps.Events.addHandler(pushpins[i], 'mouseout', hideInfoBox);
}
/*var clusterLayer = new Microsoft.Maps.ClusterLayer(pushpins);*/
var clusterLayer = new Microsoft.Maps.ClusterLayer(pushpins, {
clusteredPinCallback: customizeClusterPresentation,
gridSize: 80
});
/* customizeClusterPresentation();*/
map.layers.insert(clusterLayer);
});
function zoomIn(){
var pushpinLocation = e.target.getLocation();
var newCenter = pushpinLocation;
var zoomValue=map.getZoom()+4;
map.setView({ zoom:zoomValue,center:newCenter });
}
function showInfoBox(e) {
//Make sure the infobox has metadata to display.
if (e.target.metadata) {
var margins=checkInfoBoxPosition(e.target);
//Set the infobox options with the metadata of the pushpin.
infobox.setOptions({
location: e.target.getLocation(),
title: e.target.metadata.title,
description: e.target.metadata.description,
htmlContent: infoboxTemplate.replace('{title}', e.target.metadata.title).replace('{description}', e.target.metadata.description).replace('{marginTop}',margins.marginTop).replace('{marginLeft}',margins.marginLeft),
visible: true
});
}
}
function customizeClusterPresentation(cluster){
var minimumRadius=0;
var outlineWidth=0;
minimumRadius=12;
outlineWidth=7;
var numberOfPinsInCluster = cluster.containedPushpins.length;
var radius = Math.log(numberOfPinsInCluster) / Math.log(10) * 5 + minimumRadius;
var fillColor = 'orange';
var svg = ['<svg xmlns="http://www.w3.org/2000/svg" width="', (radius * 2), '" height="', (radius * 2), '">',
'<circle cx="', radius, '" cy="', radius, '" r="', radius, '" fill="', fillColor, '"/>',
'</svg>'];
cluster.setOptions({
icon: svg.join(''),
anchor: new Microsoft.Maps.Point(radius, radius),
textOffset: new Microsoft.Maps.Point(0, radius - 8)
});
Microsoft.Maps.Events.addHandler(cluster, 'click', clusterClicked);
}
function clusterClicked(e) {
if (e.target.containedPushpins) {
var locs = [];
for (var i = 0, len = e.target.containedPushpins.length; i < len; i++) {
//Get the location of each pushpin.
locs.push(e.target.containedPushpins[i].getLocation());
}
//Create a bounding box for the pushpins.
var bounds = Microsoft.Maps.LocationRect.fromLocations(locs);
//Zoom into the bounding box of the cluster.
//Add a padding to compensate for the pixel area of the pushpins.
map.setView({ bounds: bounds, padding: 100 });
}
}
function hideInfoBox(e) {
//Make sure the infobox has metadata to display.
if (e.target.metadata) {
//Set the infobox options with the metadata of the pushpin.
infobox.setOptions({
visible: false
});
}
}
function createCirclePushpin(location, radius, fillColor, strokeColor, strokeWidth, textColor,id,name,description)
{
if (strokeColor === void 0) { strokeColor = ""; }
if (strokeWidth === void 0) { strokeWidth = 0; }
//Create an SVG string of a circle with the specified radius and color.
var svg = "<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"" + 2 * radius + "\" height=\"" + 2 * radius + "\">\n <circle cx=\"" + radius + "\" cy=\"" + radius + "\" r=\"" + (radius - strokeWidth) + "\" stroke=\"" + strokeColor + "\" stroke-width=\"" + strokeWidth + "\" fill=\"" + fillColor + "\"/><text x='50%' y='50%' text-anchor='middle' stroke='"+textColor+"' stroke-width='2px' dy='.3em'></text></svg> ";
//Create a pushpin from the SVG and anchor it to the center of the circle.
return new Microsoft.Maps.Pushpin(location, { icon: svg, anchor: new Microsoft.Maps.Point(radius, radius), title: name,
subTitle: description, });
}
function createRegularPushpin(location,id,name,description)
{
return new Microsoft.Maps.Pushpin(location, {
title: name,
subTitle: description,
text: '1'
});
}
function checkInfoBoxPosition(pushpin){
var margins={marginTop:"150",marginLeft:"-110"};
var mapWidth = map.getWidth();
var mapHeight = map.getHeight();
var ymargin = 147;
var xmargin = 120;
/* 0,0 is top left */
var pushpinLocation = map.tryLocationToPixel(pushpin.getLocation(), Microsoft.Maps.PixelReference.control);
var infoBoxBottom = pushpinLocation.y + ymargin;
var infoBoxRight = pushpinLocation.x + xmargin;
var infoBoxLeft = pushpinLocation.x - xmargin;
document.getElementById("test").innerHTML = "map width:"+mapWidth + " Map Height" +mapHeight+" Pin location:"+ pushpinLocation + "<br>Infobox bottom:"+infoBoxBottom+" Info box left"+infoBoxLeft+" Info box right"+infoBoxRight;
if ( infoBoxBottom > mapHeight )
{
margins.marginTop = "-30";
}
if ( infoBoxLeft < 0 )
{
margins.marginLeft = "-10";
}
if ( infoBoxRight > mapWidth )
{
margins.marginLeft = "-220";
}
return margins;
}
}
</script>
</head>
<body onload='loadMapScenario();'>
<div id='printoutPanel'></div>
<div id='myMap' style='width: 100vw; height: 80vh;'></div>
<div id='test' style='width: 100%; height: 200px; background:white'>test
</div>
</body>
</html>