google maps geojson & autocomplete
by beaver71
HTML
<script src="https://maps.google.com/maps/api/js?sensor=false&libraries=places&.js"></script>
<input id="pac-input" class="controls" type="text" placeholder="Search Box">
<div id="googleMap"></div>
CSS
html,
body,
#googleMap {
width: 100%;
height: 100%;
}
#pac-input {
background-color: #fff;
font-family: Roboto;
font-size: 15px;
font-weight: 300;
margin-left: 12px;
padding: 0 11px 0 13px;
text-overflow: ellipsis;
width: 400px;
}
#pac-input:focus {
border-color: #4d90fe;
}
JavaScript
var x = new google.maps.LatLng(40.75597, -73.974228);
var feats = [], infoWindow;
function initialize() {
var data = {
type: "Feature",
geometry: {
"type": "Polygon",
"coordinates": [
[
[-73.974228, 40.75597],
[-73.983841, 40.742931],
[-74.008133, 40.75307500000001],
[-73.998131, 40.765915],
[-73.974228, 40.75597]
]
]
},
properties: {
"name": "My square area"
}
};
var mapProp = {
center: x,
zoom: 12,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById("googleMap"), mapProp);
map.data.addGeoJson(data);
// not useful if use loadGeoJson
setTimeout(function() {
getFeats(map);
}, 100)
/*loadGeoJson("URL", {}, function(features){
log("loadGeoJson finished, number of features:" + feature.length);
});*/
infoWindow = new google.maps.InfoWindow({
content: ""
});
initAutoCompl(map);
}
google.maps.event.addDomListener(window, 'load', initialize);
initAutoCompl = function(map) {
var input = document.getElementById('pac-input');
var searchBox = new google.maps.places.SearchBox(input);
map.controls[google.maps.ControlPosition.TOP_LEFT].push(input);
map.addListener('bounds_changed', function() {
searchBox.setBounds(map.getBounds());
});
searchBox.addListener('places_changed', function() {
var places = searchBox.getPlaces();
if (places.length == 0) {
return;
}
var bounds = new google.maps.LatLngBounds();
places.forEach(function(place) {
if (!place.geometry) {
console.log("Returned place contains no geometry");
return;
}
/* if (place.geometry.viewport) {
// Only geocodes have viewport.
bounds.union(place.geometry.viewport);
} else {
bounds.extend(place.geometry.location);
}*/
var area = inWhichArea(map,...