/
google_maps_api.html
88 lines (66 loc) · 2.46 KB
/
google_maps_api.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Google Maps API Practice</title>
<style type="text/css">
#map-canvas {
width: 770px;
height: 481px;
}
</style>
</head>
<body>
<!-- div to hold map -->
<div id="map-canvas"></div>
<input type="text" id="address">
<button type="submit" id="searchButton">Search</button>
<!-- Load the Google Maps API [DON'T FORGET TO USE A KEY] -->
<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyDMVcZOzzha3vjCoaxlSmos_rF-e4CBn-4"></script>
<!-- Script to show address on map -->
<script type="text/javascript">
(function() {
"use strict";
// Set our map options
var mapOptions = {
// Set the zoom level
zoom: 19,
// This sets the center of the map at our location
center: {
lat: 29.418675,
lng: -98.488021
}
};
// Init geocoder object
var geocoder = new google.maps.Geocoder();
var addressButton = document.getElementById("searchButton");
addressButton.addEventListener("click", function() {
var myAddress = document.getElementById("address").value;
geocoder.geocode({ "address": myAddress }, function (results, status) {
// Check for a successful result
if (status == google.maps.GeocoderStatus.OK) {
// Recenter the map over the address
console.log(results);
map.setCenter(results[0].geometry.location);
var tempMarker = new google.maps.Marker({
position: results[0].geometry.location,
map: map
});
// create a new infoWindow object with content
var infowindow = new google.maps.InfoWindow({
content: "Here's what you're looking for!"
});
//Open the window using our map and marker
infowindow.open(map, tempMarker);
} else {
// Show an error message with the status if our request fails
alert("Geocoding was not successful - STATUS: " + status);
}
});
});
// Render the map
var map = new google.maps.Map(document.getElementById("map-canvas"), mapOptions);
})();
</script>
</body>
</html>