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
/
Create Pushpin Add Metadata.html
118 lines (100 loc) · 4.47 KB
/
Create Pushpin Add Metadata.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
<!DOCTYPE html>
<html lang="en">
<head>
<title>Create Pushpin and Add Metadata - 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="This sample shows how a form can be displayed to enter metadata for a pushpin after it has been added to the map." />
<meta name="keywords" content="Microsoft maps, map, gis, API, SDK, Bing, Bing Maps" />
<meta name="author" content="Microsoft Bing Maps" />
<meta name="screenshot" content="screenshot.jpg" />
<script>
var map, infobox, currentPushpin;
function GetMap()
{
map = new Microsoft.Maps.Map('#myMap', {});
//Add a click event to the map.
Microsoft.Maps.Events.addHandler(map, 'click', mapClicked);
//Create an infobox, but hide it. We can reuse it for each pushpin.
infobox = new Microsoft.Maps.Infobox(map.getCenter(), { visible: false });
infobox.setMap(map);
}
function mapClicked(e) {
//Create a pushpin.
currentPushpin = new Microsoft.Maps.Pushpin(e.location);
//Add a click event to the pushpin.
Microsoft.Maps.Events.addHandler(currentPushpin, 'click', pushpinClicked);
//Add the pushpin to the map.
map.entities.push(currentPushpin);
//Open up an input form here the user can enter in details for pushpin.
document.getElementById('inputForm').style.display = '';
}
function saveData() {
//Get the data from form and add it to the pushpin
currentPushpin.metadata = {
title: document.getElementById('titleTbx').value,
description: document.getElementById('descriptionTbx').value
};
//Optionally save this data somewhere (like a database or local storage).
//Clear the fields in the form and then hide the form.
document.getElementById('titleTbx').value = '';
document.getElementById('descriptionTbx').value = '';
document.getElementById('inputForm').style.display = 'none';
}
function pushpinClicked(e) {
if (e.target.metadata) {
infobox.setOptions({
location: e.target.getLocation(),
title: e.target.metadata.title,
description: e.target.metadata.description,
visible: true
});
}
}
</script>
<style>
#myMap {
position:relative;
width:800px;
height:600px;
}
#inputForm {
position:absolute;
top:200px;
left:250px;
padding:10px;
background-color:white;
border:1px solid #000;
border-radius:10px;
}
</style>
</head>
<body>
<div id="myMap"></div>
<div id="inputForm" style="display:none;">
<table>
<tr><td colspan="2"></td></tr>
<tr><td>Title</td><td><input id="titleTbx" type="text" /></td></tr>
<tr><td>Description</td><td><input id="descriptionTbx" type="text" /></td></tr>
<tr><td colspan="2"><input type="button" value="Save" onclick="saveData()" style="float:right;"/></td></tr>
</table>
</div>
<fieldset style="width:800px;margin-top:10px;">
<legend>Create Pushpin and Add Metadata Sample</legend>
This sample shows how a form can be displayed to enter metadata for a pushpin after it has been added to the map.
To try this out, click on the map to add a pushpin. Enter some metadata into the form that is displayed and press
save. Click on the pushpin to see the metadata that was saved for the pushpin.
</fieldset>
<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>