This repository has been archived by the owner on May 24, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 128
/
User Input Geocode.html
105 lines (89 loc) · 3.95 KB
/
User Input Geocode.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
<!DOCTYPE html>
<html lang="en">
<head>
<title>User Input Geocode - Bing Maps Samples</title>
<meta charset="utf-8" />
<link rel="shortcut icon" href="/favicon.ico"/>
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" />
<meta name="description" content="When the user types in a query and presses the search button a check is done to see if the Search module is loaded. If it is loaded, the users query is then geocoded." />
<meta name="keywords" content="Microsoft maps, map, gis, API, SDK, Bing, Bing Maps" />
<meta name="author" content="Microsoft Bing Maps" />
<meta name="screenshot" content="bmv8-geocodeuserinputexample.jpg" />
<script>
var map, searchManager;
function GetMap() {
map = new Microsoft.Maps.Map('#myMap', {});
}
function Search() {
if (!searchManager) {
//Create an instance of the search manager and perform the search.
Microsoft.Maps.loadModule('Microsoft.Maps.Search', function () {
searchManager = new Microsoft.Maps.Search.SearchManager(map);
Search()
});
} else {
//Remove any previous results from the map.
map.entities.clear();
//Get the users query and geocode it.
var query = document.getElementById('searchTbx').value;
geocodeQuery(query);
}
}
function geocodeQuery(query) {
var searchRequest = {
where: query,
callback: function (r) {
if (r && r.results && r.results.length > 0) {
var pin, pins = [], locs = [], output = 'Results:<br/>';
for (var i = 0; i < r.results.length; i++) {
//Create a pushpin for each result.
pin = new Microsoft.Maps.Pushpin(r.results[i].location, {
text: i + ''
});
pins.push(pin);
locs.push(r.results[i].location);
output += i + ') ' + r.results[i].name + '<br/>';
}
//Add the pins to the map
map.entities.push(pins);
//Display list of results
document.getElementById('output').innerHTML = output;
//Determine a bounding box to best view the results.
var bounds;
if (r.results.length == 1) {
bounds = r.results[0].bestView;
} else {
//Use the locations from the results to calculate a bounding box.
bounds = Microsoft.Maps.LocationRect.fromLocations(locs);
}
map.setView({ bounds: bounds });
}
},
errorCallback: function (e) {
//If there is an error, alert the user about it.
alert("No results found.");
}
};
//Make the geocode request.
searchManager.geocode(searchRequest);
}
</script>
</head>
<body>
<input id="searchTbx" type="text"/>
<input type="button" value="Search" onclick="Search()"/>
<br/>
<div id="myMap" style="position:relative;width:600px;height:400px;float:left;"></div>
<div id="output" style="margin-left:10px;float:left;"></div>
<script>
// Dynamic load the Bing Maps Key and Script
// Get your own Bing Maps key at https://www.microsoft.com/maps
(async () => {
let script = document.createElement("script");
let bingKey = await fetch("https://samples.azuremaps.com/api/GetBingMapsKey").then(r => r.text()).then(key => { return key });
script.setAttribute("src", `https://www.bing.com/api/maps/mapcontrol?callback=GetMap&key=${bingKey}`);
document.body.appendChild(script);
})();
</script>
</body>
</html>