-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMap_final.html
More file actions
110 lines (72 loc) · 3.55 KB
/
Copy pathMap_final.html
File metadata and controls
110 lines (72 loc) · 3.55 KB
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
<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8" />
<title>Address Map</title>
<script src='https://api.mapbox.com/mapbox-gl-js/v2.7.0/mapbox-gl.js'></script>
<link href='https://api.mapbox.com/mapbox-gl-js/v2.7.0/mapbox-gl.css' rel='stylesheet' />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
</head>
<body>
<form id="login" align ="center">
<label for="Address">Address:</label>
<input type="text" name="Address" id="Address" style="width: 250px;">
<button type="button">Get Map</button><br />
<br />
<br />
</form>
<!--used template for displaying a map from mapbox site
<!--https://docs.mapbox.com/mapbox-gl-js/guides/install/#quickstart-->
<div id='map' style='width: 700px; height: 500px'></div>
<script>
mapboxgl.accessToken = 'pk.eyJ1IjoidG1iYXR0ZXkiLCJhIjoiY2t6Zno5aXVqM25iNDJwcDR4M2Qxc2E0bSJ9.oDkPCDETfm1G3dZAIuYvxA';
const map = new mapboxgl.Map({
container: 'map', // container ID
style: 'mapbox://styles/mapbox/streets-v11', // style URL
center: [-74.5, 40], // starting position [lng, lat] from mapbox example
zoom: 9 // starting zoom
});
</script>
<script>
$(document).ready(function () {
$("button").click(function () {
var FD = $('#Address').val();
// console.log(FD); :used to validate input
var queryString = encodeURIComponent(FD) + '.json';
var queryFinal = 'types=address' + '&access_token=pk.eyJ1IjoidG1iYXR0ZXkiLCJhIjoiY2t6Zno5aXVqM25iNDJwcDR4M2Qxc2E0bSJ9.oDkPCDETfm1G3dZAIuYvxA';
var jqxhr = $.getJSON("https://api.mapbox.com/geocoding/v5/mapbox.places/" + queryString, queryFinal, processResponse)
.done(function () {
alert("success");
})
.fail(function () {
var errorMsg = "Your query couldn't be processed. Please try again";
console.log(errorMsg);
alert("error");
})
function errorResponse() {
var errorMsg = "Your query couldn't be processed. Please try again";
console.log(errorMsg);
}
function processResponse(data) {
// console.log(data); displays data retrieved from mapbox
var messageContent = data.features[0]; //retrieve the most relevant result set
var centervar = messageContent.center;
// console.log(messageContent);
// console.log(centervar); display new map coordinates
longvar = centervar[0];
// console.log(longvar);
latvar = centervar[1];
// console.log(latvar);
//recenter the map
map.setCenter([longvar, latvar]);
// Create a new marker. Code template from mapbox site.
const marker = new mapboxgl.Marker()
.setLngLat([longvar, latvar])
.addTo(map);
}
return false;
}); // click function
}); // end ready
</script>
</body>
</html>