var map;
var markers = {};
var bounds = null;
var posi = new google.maps.LatLng(53.801279, -1.548567);
var name = 'Vince';
markers[name] = {};
markers[name].id = 1;
markers[name].lat = 53.801279;
markers[name].lng = -1.548567;
markers[name].state = 'Online';
markers[name].position = posi;
markers[name].selected = false;
$(document).ready(function() {
var mapOptions = {
zoom: 5,
center: new google.maps.LatLng(53.801279, -1.548567),
mapTypeId: google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById('map'), mapOptions);
var infowindow = new google.maps.InfoWindow();
for (var key in markers) {
var marker = new google.maps.Marker({
position: new google.maps.LatLng(markers[key].lat, markers[key].lng),
map: map
});
markers[key].marker = marker;
google.maps.event.addListener(marker, 'click', (function(marker, key) {
return function() {
infowindow.setContent(key);
infowindow.open(map, marker);
}
})(marker, key));
}
var shiftPressed = false;
$(window).keydown(function(evt) {
if (evt.which === 16) {
shiftPressed = true;
}
}).keyup(function(evt) {
if (evt.which === 16) {
shiftPressed = false;
}
});
var mouseDownPos, gribBoundingBox = null,
mouseIsDown = 0;
var themap = map;
google.maps.event.addListener(themap, 'mousemove', function(e) {
if (mouseIsDown && shiftPressed) {
if (gribBoundingBox !== null)
{
bounds.extend(e.latLng);
gribBoundingBox.setBounds(bounds);
} else
{
bounds = new google.maps.LatLngBounds();
bounds.extend(e.latLng);
gribBoundingBox = new google.maps.Rectangle({
map: themap,
bounds: bounds,
fillOpacity: 0.15,
strokeWeight: 0.9,
clickable: false
});
}
}
});
google.maps.event.addListener(themap, 'mousedown', function(e) {
if (shiftPressed) {
mouseIsDown = 1;
mouseDownPos = e.latLng;
themap.setOptions({
draggable: false
});
}
});
google.maps.event.addListener(themap, 'mouseup', function(e) {
if (mouseIsDown && shiftPressed) {
mouseIsDown = 0;
if (gribBoundingBox !== null)
{
var boundsSelectionArea = new google.maps.LatLngBounds(gribBoundingBox.getBounds().getSouthWest(), gribBoundingBox.getBounds().getNorthEast());
for (var key in markers) {
if (gribBoundingBox.getBounds().contains(markers[key].marker.getPosition())) {
markers[key].marker.setIcon("http://maps.google.com/mapfiles/ms/icons/blue.png")
document.getElementById('info').innerHTML += "key:" + key + " posn:" + markers[key].marker.getPosition() + " in bnds:" + gribBoundingBox.getBounds() + "<br>";
} else {
markers[key].marker.setIcon("http://maps.google.com/mapfiles/ms/icons/red.png")
document.getElementById('info').innerHTML += "key:" + key + " posn:" + markers[key].marker.getPosition() + " out of bnds:" + gribBoundingBox.getBounds() + "<br>";
}
}
gribBoundingBox.setMap(null);
}
gribBoundingBox = null;
}
themap.setOptions({
draggable: true
});
});
});
#map {
height: 500px;
width: 500px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCkUOdZ5y7hMm0yrcCQoCvLwzdM6M8s5qk"></script>
<div id="map"></div>
<div id="info"></div>
if (this.gripBoundingBox) this.gripBoundingBox.setMap(null);
- Aryo