This repository was archived by the owner on Sep 29, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgoogle-maps-example.html
More file actions
140 lines (118 loc) · 4.73 KB
/
google-maps-example.html
File metadata and controls
140 lines (118 loc) · 4.73 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
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
<html>
<head>
<title>PIN API + Google Map</title>
<style type="text/css">
.error { color: red }
#stats { margin: 1em }
body { text-align: center; font-family: verdana,arial,helvetica,'sans-serif'; }
</style>
<script type="text/javascript" src="https://www.publicinsightnetwork.org/src/sprintf.min.js"></script>
<script type="text/javascript" src="https://www.publicinsightnetwork.org/source/js/jquery-1.8.1.min.js"></script>
<script type="text/javascript" src="https://www.publicinsightnetwork.org/source/js/insights.js"></script>
<!-- based on http://www.geocodezip.com/v3_example_click2add_infowindow.html -->
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
<script type="text/javascript">
//<![CDATA[
// global variables
var map = null;
var marker = null;
var infowindow = new google.maps.InfoWindow({
size: new google.maps.Size(150,50)
});
// A function to create the marker and set up the event window function
function createMarker(latlng, name, html) {
var contentString = html;
var marker = new google.maps.Marker({
position: latlng,
map: map,
draggable: true,
zIndex: Math.round(latlng.lat()*-100000)<<5
});
google.maps.event.addListener(marker, 'click', function() {
infowindow.setContent(contentString);
infowindow.open(map,marker);
});
google.maps.event.trigger(marker, 'click');
return marker;
}
function initialize() {
// create the map
var myOptions = {
zoom: 8,
center: new google.maps.LatLng(44.8947, -93.3597),
mapTypeControl: true,
mapTypeControlOptions: {style: google.maps.MapTypeControlStyle.DROPDOWN_MENU},
navigationControl: true,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
google.maps.event.addListener(map, 'click', function() {
infowindow.close();
});
google.maps.event.addListener(map, 'click', function(event) {
//call function to create marker
if (marker) {
marker.setMap(null);
marker = null;
}
marker = createMarker(event.latLng, "name", "<b>Location</b><br>"+event.latLng);
});
}
function normalize_location(latlong) {
var lat = sprintf("%010.6f", latlong.lat() + 90.0);
var long = sprintf("%010.6f", latlong.lng() + 180.0);
return { lat: parseFloat(lat), long: parseFloat(long) };
}
function populate_map() {
var query = jQuery('#q').val();
if (!query || !query.length) {
jQuery('#search').append('<div class="error">Enter a search term</div>');
return;
}
if (!marker || !marker.getPosition()) {
jQuery('#search').append('<div class="error">Click on the map</div>');
return;
}
// no errors, so clear.
jQuery('.error').remove();
var position = normalize_location(marker.getPosition());
//console.log(position);
// copy original
var query_orig = query;
// append range to query string
// this makes a search radius of about 40 miles from the marker
query += ' ' + sprintf("latitude:(%03d.0000..%03d.0000)", position.lat, position.lat + 1.0);
query += ' ' + sprintf("longitude:(%03d.0000..%03d.0000)", position.long, position.long + 1.0);
var uri = 'https://www.publicinsightnetwork.org/air2/api/public/search?callback=?&';
var params = {
a: '840ffd8f30eabbe3294adce9d9b97c8d',
q: query
};
uri += jQuery.param(params);
//console.log(uri);
jQuery('#stats').html('<img src="//www.publicinsightnetwork.org/source/img/loading.gif"/>');
jQuery.getJSON(uri, function(resp) {
//console.log(resp);
jQuery('#stats').html(resp.total + ' results for ' + query_orig);
jQuery.each(resp.results, function(idx, r) {
console.log(r);
var latlong = new google.maps.LatLng(r.primary_lat, r.primary_long);
var m = createMarker(latlong, r.uri, r.src_first_name+' '+r.src_last_name+'<br/>'+query_orig);
});
});
}
jQuery(document).ready(function() {
initialize();
});
//]]>
</script>
</head>
<body>
<div id="map_canvas" style="margin: auto; width: 550px; height: 450px"></div>
<div id="search">
<input type="text" name="q" id="q" />
<button onclick="populate_map()">Map it!</button>
</div>
<div id="stats"></div>
</body>
</html>