Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var map, geocoder, icon;
- var markers = {}, polygons = {};
- var searchResults = {
- 'california': [
- {
- seller: 'Robert De Niro',
- price: 100.0,
- latitude: 36.70834,
- longitude: -119.54618
- },
- {
- seller: 'Jessica Alba',
- price: 80.0,
- latitude: 36.83567,
- longitude: -119.69193
- },
- {
- seller: 'Larry Page',
- price: 90.0,
- latitude: 35.37114,
- longitude: -119.03961
- }],
- 'washington': [
- {
- seller: 'Barack Obama',
- price: 120.0,
- latitude: 38.90787,
- longitude: -77.00360
- },
- {
- seller: 'Hillary Clinton',
- price: 200.0,
- latitude: 38.95147,
- longitude: -77.06188
- },
- {
- seller: 'Collin Powel',
- price: 50.0,
- latitude: 38.90893,
- longitude: -77.04446
- }],
- 'florida': [
- {
- seller: 'Diego de la Vega',
- price: 45.0,
- latitude: 25.78011,
- longitude: -80.68909
- },
- {
- seller: 'Versacce',
- price: 864.0,
- latitude: 29.73576,
- longitude: -83.18848
- },
- {
- seller: 'Don Fransisco',
- price: 10.0,
- latitude: 28.07683,
- longitude: -82.43042
- }],
- 'ny': [
- {
- seller: 'Madona',
- price: 9999.9,
- latitude: 40.71435,
- longitude: -74.00597
- },
- {
- seller: 'Ben Stiler',
- price: 350.0,
- latitude: 40.68044,
- longitude: -73.99541
- },
- {
- seller: 'Kofi Annan',
- price: 150.0,
- latitude: 40.77118,
- longitude: -73.98983
- }],
- 'utah': [
- {
- seller: 'Heat',
- price: 20.0,
- latitude: 39.34492,
- longitude: -110.78888
- },
- {
- seller: 'Snake',
- price: 10.0,
- latitude: 39.37465,
- longitude: -111.30249
- },
- {
- seller: 'Cowboy',
- price: 50.0,
- latitude: 39.01492,
- longitude: -111.84357
- }],
- 'la': [
- {
- seller: 'Pimp My Ride',
- price: 200.0,
- latitude: 34.04932,
- longitude: -118.25057
- },
- {
- seller: 'Surfers',
- price: 600.0,
- latitude: 34.02478,
- longitude: -118.51021
- },
- {
- seller: 'Juan Valdez',
- price: 300.0,
- latitude: 34.05380,
- longitude: -118.22954
- }],
- 'default' : []
- };
- $(document).ready(function () {
- loadScript();
- $('#search_address_button').click(function () {
- var address = $('#search_address_text').val();
- searchAddress(address);
- });
- $('#search_address_text').click(function (event) {
- var keyDown = (event.keyCode ? event.keyCode : event.which);
- if (keyDown == 13)
- searchAddress();
- });
- $('.state_address').click(function () {
- var address = $(this).attr('address');
- if (address == "all")
- searchAllAddress();
- else
- searchAddress(address, true);
- });
- });
- function loadScript()
- {
- var script = document.createElement("script");
- script.type = "text/javascript";
- script.src = "http://maps.googleapis.com/maps/api/js?key=APIKEY&sensor=false&callback=initialize";
- document.body.appendChild(script);
- }
- function initialize()
- {
- var myOptions = {
- zoom: 4,
- center: new google.maps.LatLng(38.68551, -99.22852),
- mapTypeId: google.maps.MapTypeId.ROADMAP,
- minZoom: 4,
- maxZoom: 16
- }
- geocoder = new google.maps.Geocoder();
- map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
- image = new google.maps.MarkerImage('img/beachflag.png',
- // This marker is 20 pixels wide by 32 pixels tall.
- new google.maps.Size(20, 32),
- // The origin for this image is 0,0.
- new google.maps.Point(0, 0),
- // The anchor for this image is the base of the flagpole at 0,32.
- new google.maps.Point(0, 32));
- google.maps.event.addListener(map, 'click', function (event) {
- addMarker(event.latLng);
- });
- // Bounds for North America
- var strictBounds = new google.maps.LatLngBounds(
- new google.maps.LatLng(28.70, -127.50),
- new google.maps.LatLng(48.85, -55.90)
- );
- // Listen for the dragend event
- google.maps.event.addListener(map, 'dragend', function() {
- if (strictBounds.contains(map.getCenter())) return;
- // We're out of bounds - Move the map back within the bounds
- var c = map.getCenter(),
- x = c.lng(),
- y = c.lat(),
- maxX = strictBounds.getNorthEast().lng(),
- maxY = strictBounds.getNorthEast().lat(),
- minX = strictBounds.getSouthWest().lng(),
- minY = strictBounds.getSouthWest().lat();
- if (x < minX) x = minX;
- if (x > maxX) x = maxX;
- if (y < minY) y = minY;
- if (y > maxY) y = maxY;
- map.setCenter(new google.maps.LatLng(y, x));
- });
- }
- function addMarker(location, name)
- {
- if (name == undefined) name = "default";
- if (markers[name] == undefined) markers[name] = [];
- var marker_number = markers[name].length;
- var contentString = '<div id="marker_info"><h2>Excelente localidad</h2></div>';
- var infowindow = new google.maps.InfoWindow({
- content: contentString,
- maxWidth: 150
- });
- var marker = new google.maps.Marker({
- position: location,
- map: map,
- icon: image,
- title: location.name
- });
- google.maps.event.addListener(marker, 'mouseover', function () {
- infowindow.open(map, marker);
- });
- google.maps.event.addListenerOnce(marker, 'click', function () {
- $('#marks').append('<a href="#" marker="' + marker_number + '">Delete - ' + marker_number + '</a>');
- $('[marker=' + marker_number + ']').click(function () {
- removeMarker("default", marker_number);
- $(this).hide();
- });
- });
- markers[name].push(marker);
- addSearchResult(name, '', 0.0, location.lat(), location.lng());
- }
- function addSearchResult(name, seller, price, latitude, longitude) {
- if (searchResults[name] == undefined) searchResults[name] = [];
- searchResults[name].push(
- {
- seller: seller,
- price: price,
- latitude: latitude,
- longitude: longitude
- });
- }
- function showMarker(name, number)
- {
- markers[name][number].setMap(map);
- }
- function hideMarker(name, number)
- {
- markers[name][number].setMap(null);
- }
- function removeMarker(name, number)
- {
- markers[name][number].setMap(null);
- markers[name][number] = null;
- swap(markers[name], number, markers[name].length - 1);
- markers[name].length -= 1;
- }
- function removePolygon(name)
- {
- if (polygons[name] == undefined) return;
- polygons[name].setMap(null);
- polygons[name] = null;
- }
- function removeMarkers(name)
- {
- for (marker in markers[name])
- {
- markers[name][marker].setMap(null);
- markers[name][marker] = null;
- }
- markers[name] = []
- }
- function swap(array, a, b)
- {
- var aux = array[a];
- array[a] = array[b];
- array[b] = aux;
- }
- function addHouseMarker(name, house) {
- var lat = house.latitude;
- var lng = house.longitude;
- var location = new google.maps.LatLng(lat, lng);
- var marker_number = markers.length;
- var contentString = '<div id="marker_info"><div id="house_price">' + house.price + '</div></div>';
- var infowindow = new google.maps.InfoWindow({
- content: contentString,
- maxWidth: 50,
- maxHeigth: 50
- });
- var marker = new google.maps.Marker({
- position: location,
- map: map,
- icon: image,
- title: location.name
- });
- google.maps.event.addListener(marker, 'mouseover', function () {
- infowindow.open(map, marker);
- });
- google.maps.event.addListener(marker, 'click', function () {
- $('#house_details').empty();
- $('#house_details').append('<div id="house_photo">Photo</div><br /><table><tr><td>Vendedor</td><td id="house_seller">' + house.seller + '</td></tr>' +
- '<tr><td>Precio</td><td id="house_price">' + house.price + '</td></tr></table><br />');
- });
- if (markers[name] == undefined) markers[name] = [];
- markers[name].push(marker);
- }
- function addHousesPolygon(name, coordinates)
- {
- var polygon= new google.maps.Polygon({
- paths: coordinates,
- strokeColor: "#000000",
- strokeOpacity: 0.0,
- strokeWeight: 3,
- fillColor: "#00FF00",
- fillOpacity: 0.35
- });
- polygon.setMap(map);
- var contentString = '<div id="polygon_info"><div id="polygon_name">' + name + '</div></div>';
- var infowindow = new google.maps.InfoWindow({
- content: contentString,
- maxWidth: 150
- });
- google.maps.event.addListener(polygon, 'mouseover', function (event) {
- infowindow.setPosition(event.latLng);
- infowindow.open(map);
- });
- polygons[name] = polygon;
- }
- function searchAddress(address, remove)
- {
- var houses = searchResults[address];
- if (houses == undefined) {
- geocoder.geocode({ 'address': address }, function (results, status) {
- if (status == google.maps.GeocoderStatus.OK) {
- map.setCenter(results[0].geometry.location);
- alert(results[0].geometry.location.latitude + ', ' + results[0].geometry.location.longitude);
- }
- else
- alert("Geocode was not successful for the following reason: " + status);
- });
- }
- else if (houses.length == 0) return;
- else
- {
- var maxX = -360.0,
- maxY = -360.0,
- minX = 360.0,
- minY = 360.0;
- var coordinates = []
- if (remove)
- {
- for (name in searchResults)
- {
- removeMarkers(name);
- removePolygon(name);
- }
- }
- for (house in houses) {
- if (houses[house].longitude < minX) minX = houses[house].longitude;
- if (houses[house].longitude > maxX) maxX = houses[house].longitude;
- if (houses[house].latitude < minY) minY = houses[house].latitude;
- if (houses[house].latitude > maxY) maxY = houses[house].latitude;
- addHouseMarker(address, houses[house]);
- coordinates.push(new google.maps.LatLng(houses[house].latitude, houses[house].longitude));
- }
- addHousesPolygon(address, coordinates);
- var southWest = new google.maps.LatLng(minY, minX),
- northEast = new google.maps.LatLng(maxY, maxX);
- var bounds = new google.maps.LatLngBounds(southWest, northEast);
- map.fitBounds(bounds);
- var center_location = new google.maps.LatLng((minY + maxY) / 2, (minX + maxX) / 2);
- map.setCenter(center_location);
- }
- }
- function searchAllAddress() {
- var maxX = -360.0,
- maxY = -360.0,
- minX = 360.0,
- minY = 360.0;
- for (address in searchResults) {
- var houses = searchResults[address];
- var coordinates = []
- for (house in houses) {
- if (houses[house].longitude < minX) minX = houses[house].longitude;
- if (houses[house].longitude > maxX) maxX = houses[house].longitude;
- if (houses[house].latitude < minY) minY = houses[house].latitude;
- if (houses[house].latitude > maxY) maxY = houses[house].latitude;
- addHouseMarker(address, houses[house]);
- coordinates.push(new google.maps.LatLng(houses[house].latitude, houses[house].longitude));
- }
- addHousesPolygon(address, coordinates);
- }
- var southWest = new google.maps.LatLng(minY, minX),
- northEast = new google.maps.LatLng(maxY, maxX);
- var bounds = new google.maps.LatLngBounds(southWest, northEast);
- map.fitBounds(bounds);
- var center_location = new google.maps.LatLng((minY + maxY) / 2, (minX + maxX) / 2);
- map.setCenter(center_location);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement