forked from microsoft/Bing-Maps-V8-TypeScript-Definitions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExample.ts
148 lines (116 loc) · 4.78 KB
/
Example.ts
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
/// <reference path="scripts/MicrosoftMaps/Microsoft.Maps.All.d.ts" />
var map = new Microsoft.Maps.Map('#MyMap', {
credentials: 'Your Bing Maps Key'
});
Microsoft.Maps.Events.addHandler(map, 'click', (e: Microsoft.Maps.IMouseEventArgs) => {
//The location on the map that the user clicked.
var loc = e.location;
});
var layer = new Microsoft.Maps.Layer();
var pin = new Microsoft.Maps.Pushpin(map.getCenter(), {
color: new Microsoft.Maps.Color(150, 150, 0, 0)
});
Microsoft.Maps.Events.addOne(pin, 'changed', (e: Microsoft.Maps.IPrimitiveChangedEventArgs) => {
//e.name => name of property that changed.
//e.sender => reference to the pushpin object that changed.
});
layer.add(pin);
map.layers.insert(layer);
Microsoft.Maps.loadModule('Microsoft.Maps.GeoJSON', () => {
Microsoft.Maps.GeoJSON.readFromUrl('URL to file', (data) => {
if (data instanceof Array) {
//Data is an array of shapes.
} else {
//Data is a single shape.
}
map.entities.push(data);
});
});
Microsoft.Maps.loadModule('Microsoft.Maps.Autosuggest', {
credentials: 'Your Bing Maps Key',
callback: () => {
var asManager = new Microsoft.Maps.AutosuggestManager();
asManager.attachAutosuggest('mySuggestionTextbox', 'suggestionsContainer', (result) => {
//Do something with the selected result.
});
}
});
Microsoft.Maps.loadModule('Microsoft.Maps.DrawingTools', {
credentials: 'Your Bing Maps Key',
callback: () => {
var drawingTools = new Microsoft.Maps.DrawingTools(map);
drawingTools.create(Microsoft.Maps.DrawingTools.ShapeType.polyline);
}
});
Microsoft.Maps.loadModule('Microsoft.Maps.Search', () => {
var searchManager = new Microsoft.Maps.Search.SearchManager(map);
searchManager.geocode({
bounds: map.getBounds(),
where: 'somewhere',
callback: (answer, userData) => {
map.setView({ bounds: answer.results[0].bestView });
map.entities.push(new Microsoft.Maps.Pushpin(answer.results[0].location));
}
});
});
Microsoft.Maps.loadModule('Microsoft.Maps.Directions', () => {
var directionsManager = new Microsoft.Maps.Directions.DirectionsManager(map);
directionsManager.addWaypoint(new Microsoft.Maps.Directions.Waypoint({
address: 'Redmond, WA'
}));
directionsManager.addWaypoint(new Microsoft.Maps.Directions.Waypoint({
address: 'Seatle, WA'
}));
Microsoft.Maps.Events.addHandler(directionsManager, 'directionsUpdated', (args) => {
});
directionsManager.calculateDirections();
});
/***********************
* Custom Overlay Example
************************/
class TopographicOverlay extends Microsoft.Maps.CustomOverlay {
bounds: Microsoft.Maps.LocationRect;
image: string;
private img: HTMLImageElement;
constructor(bounds: Microsoft.Maps.LocationRect, image: string) {
super({ beneathLabels: true });
this.bounds = bounds;
this.image = image;
}
onAdd() {
//Create an image element that will be used as the overlay.
this.img = document.createElement('img');
this.img.src = this.image;
this.img.style.width = '100%';
this.img.style.height = '100%';
this.img.style.position = 'absolute';
this.setHtmlElement(this.img);
}
onLoad() {
this.repositionOverlay();
//Update the position of the image when the view changes.
Microsoft.Maps.Events.addHandler(map, 'viewchange', function () {
this.repositionOverlay();
});
}
onRemove() {
//Logic to perform when overlay has been removed from the map.
}
private repositionOverlay() {
//Streach and position the image based on the bounding box pixel coordinates.
var topLeft = <Microsoft.Maps.Point>map.tryLocationToPixel(this.bounds.getNorthwest(), Microsoft.Maps.PixelReference.control);
var bottomRight = <Microsoft.Maps.Point>map.tryLocationToPixel(this.bounds.getSoutheast(), Microsoft.Maps.PixelReference.control);
this.img.style.left = topLeft.x + 'px';
this.img.style.top = topLeft.y + 'px';
this.img.style.width = (bottomRight.x - topLeft.x) + 'px';
this.img.style.width = (bottomRight.x - topLeft.x) + 'px';
this.img.style.height = (bottomRight.y - topLeft.y) + 'px';
}
}
//The bounding box and url for the image to overlay.
var bounds = Microsoft.Maps.LocationRect.fromCorners(new Microsoft.Maps.Location(40.5, -123.5), new Microsoft.Maps.Location(40, -123));
var imageSrc = 'https://bingmapsisdk.blob.core.windows.net/isdksamples/topographicMap.gif';
//Implement the new custom overlay class.
var overlay = new TopographicOverlay(bounds, imageSrc);
//Add the custom overlay to the map.
map.layers.insert(overlay);