Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!DOCTYPE html>
- <html>
- <head>
- <style>
- html,
- body {
- font-family: Arial, sans-serif;
- height: 100%;
- margin: 0;
- padding: 0;
- }
- .container {
- height: 100%;
- position: relative;
- }
- input {
- font-size: 12px;
- }
- h1 {
- color: #525454;
- font-size: 22px;
- margin: 0 0 10px 0;
- text-align: center;
- }
- #hide-listings,
- #show-listings {
- width: 48%;
- }
- #map {
- bottom:0px;
- height: 100%;
- left: 362px;
- position: absolute;
- right: 0px;
- }
- .options-box {
- background: #fff;
- border: 1px solid #999;
- border-radius: 3px;
- height: 100%;
- line-height: 35px;
- padding: 10px 10px 30px 10px;
- text-align: left;
- width: 340px;
- }
- #pano {
- width: 200px;
- height: 200px;
- }
- .text {
- font-size: 12px;
- }
- #zoom-to-area-text {
- position: relative;
- width: 70%;
- }
- #zoom-to-area {
- width: 24%;
- }
- </style>
- </head>
- <body>
- <div class="container">
- <div class="options-box">
- <h1>Find Your New NYC Home</h1>
- <div>
- <input id="show-listings" type="button" value="Show Listings">
- <input id="hide-listings" type="button" value="Hide Listings">
- </div>
- <div>
- <input id="zoom-to-area-text" type="text" placeholder="Enter your favourite area!">
- <input id="zoom-to-area" type="button" value="Zoom">
- </div>
- </div>
- <div id="map"></div>
- </div>
- <script>
- var map;
- // Create a new blank array for all the listing markers.
- var markers = [];
- function initMap() {
- // Create a styles array to use with the map.
- var styles = [
- {
- featureType: 'water',
- stylers: [
- { color: '#19a0d8' }
- ]
- },{
- featureType: 'administrative',
- elementType: 'labels.text.stroke',
- stylers: [
- { color: '#ffffff' },
- { weight: 6 }
- ]
- },{
- featureType: 'administrative',
- elementType: 'labels.text.fill',
- stylers: [
- { color: '#e85113' }
- ]
- },{
- featureType: 'road.highway',
- elementType: 'geometry.stroke',
- stylers: [
- { color: '#efe9e4' },
- { lightness: -40 }
- ]
- },{
- featureType: 'transit.station',
- stylers: [
- { weight: 9 },
- { hue: '#e85113' }
- ]
- },{
- featureType: 'road.highway',
- elementType: 'labels.icon',
- stylers: [
- { visibility: 'off' }
- ]
- },{
- featureType: 'water',
- elementType: 'labels.text.stroke',
- stylers: [
- { lightness: 100 }
- ]
- },{
- featureType: 'water',
- elementType: 'labels.text.fill',
- stylers: [
- { lightness: -100 }
- ]
- },{
- featureType: 'poi',
- elementType: 'geometry',
- stylers: [
- { visibility: 'on' },
- { color: '#f0e4d3' }
- ]
- },{
- featureType: 'road.highway',
- elementType: 'geometry.fill',
- stylers: [
- { color: '#efe9e4' },
- { lightness: -25 }
- ]
- }
- ];
- // Constructor creates a new map - only center and zoom are required.
- map = new google.maps.Map(document.getElementById('map'), {
- center: {lat: 40.7413549, lng: -73.9980244},
- zoom: 13,
- styles: styles,
- mapTypeControl: false
- });
- var locations = [
- {title: 'Park Ave Penthouse', location: {lat: 40.7713024, lng: -73.9632393}},
- {title: 'Chelsea Loft', location: {lat: 40.7444883, lng: -73.9949465}},
- {title: 'Union Square Open Floor Plan', location: {lat: 40.7347062, lng: -73.9895759}},
- {title: 'East Village Hip Studio', location: {lat: 40.7281777, lng: -73.984377}},
- {title: 'TriBeCa Artsy Bachelor Pad', location: {lat: 40.7195264, lng: -74.0089934}},
- {title: 'Chinatown Homey Space', location: {lat: 40.7180628, lng: -73.9961237}}
- ];
- var largeInfoWindow = new google.maps.InfoWindow();
- var defaultIcon = makeMarkerIcon('0091ff');
- var highlightedIcon = makeMarkerIcon('FFFF24');
- // The following group uses the location array to create an array of markers on initialize.
- for (var i = 0; i < locations.length; i++) {
- // Get the position from the location array.
- var position = locations[i].location;
- var title = locations[i].title;
- // Create a marker per location, and put into markers array.
- var marker = new google.maps.Marker({
- position: position,
- title: title,
- animation: google.maps.Animation.DROP,
- icon: defaultIcon,
- id: i
- });
- // Push the marker to our array of markers.
- markers.push(marker);
- // Create an onclick event to open an infowindow at each marker.
- marker.addListener('click', function() {
- populateInfoWindow(this, largeInfoWindow);
- });
- marker.addListener('mouseover', function() {
- this.setIcon(highlightedIcon);
- });
- marker.addListener('mouseout', function() {
- this.setIcon(defaultIcon);
- });
- }
- document.getElementById('show-listings').addEventListener('click', showListings);
- document.getElementById('hide-listings').addEventListener('click', hideListings);
- document.getElementById('zoom-to-area').addEventListener('click', function() {
- zoomToArea();
- });
- }
- // This function populates the infowindow when the marker is clicked. We'll only allow
- // one infowindow which will open at the marker that is clicked, and populate based
- // on that markers position.
- function populateInfoWindow(marker, infowindow) {
- // Check to make sure the infowindow is not already opened on this marker.
- if (infowindow.marker != marker) {
- infowindow.marker = marker;
- infowindow.setContent('<div>' + marker.title + '</div>');
- infowindow.open(map, marker);
- // Make sure the marker property is cleared if the infowindow is closed.
- infowindow.addListener('closeclick', function(){
- infowindow.setMarker = null;
- });
- var streetViewService = new google.maps.StreetViewService();
- var radius = 50;
- // In case the status is OK, which means the pano was found, compute the
- // position of the streetview image, then calculate the heading, then get a
- // panorama from that and set the options
- function getStreetView(data, status) {
- if (status == google.maps.StreetViewStatus.OK) {
- var nearStreetViewLocation = data.location.latLng;
- var heading = google.maps.geometry.spherical.computeHeading(
- nearStreetViewLocation, marker.position);
- infowindow.setContent('<div>' + marker.title + '</div><div id="pano"></div>');
- var panoramaOptions = {
- position: nearStreetViewLocation,
- pov: {
- heading: heading,
- pitch: 30
- }
- };
- var panorama = new google.maps.StreetViewPanorama(
- document.getElementById('pano'), panoramaOptions);
- } else {
- infowindow.setContent('<div>' + marker.title + '</div>' +
- '<div>No Street View Found</div>');
- }
- }
- // Use streetview service to get the closest streetview image within
- // 50 meters of the markers position
- streetViewService.getPanoramaByLocation(marker.position, radius, getStreetView);
- infowindow.open(map, marker);
- }
- }
- // This function will loop through the markers array and display them all.
- function showListings() {
- var bounds = new google.maps.LatLngBounds();
- // Extend the boundaries of the map for each marker and display the marker
- for (var i = 0; i < markers.length; i++) {
- markers[i].setMap(map);
- bounds.extend(markers[i].position);
- }
- map.fitBounds(bounds);
- }
- // This function will loop through the listings and hide them all.
- function hideListings() {
- for (var i =0; i < markers.length; i++) {
- markers[i].setMap(null);
- }
- }
- function makeMarkerIcon(markerColor) {
- var markerImage = new google.maps.MarkerImage(
- 'http://chart.googleapis.com/chart?chst=d_map_spin&chld=1.15|0|'+ markerColor +
- '|40|_|%E2%80%A2',
- new google.maps.Size(21, 34),
- new google.maps.Point(0, 0),
- new google.maps.Point(10, 34),
- new google.maps.Size(21, 34));
- return markerImage;
- }
- // This function takes the input value in the find nearby area text input
- // locates it, and then zooms into that area. This is so that the user can
- // show all listings, then decide to focus on one area of the map.
- function zoomToArea() {
- var geocoder = new google.maps.Geocoder();
- var address = document.getElementById('zoom-to-area-text').value;
- if (address == '') {
- window.alert('You must enter an area, or address.');
- } else {
- // Geocode the address/area entered to get the center. Then, center the map
- // on it and zoom in
- geocoder.geocode(
- { address: address,
- componentRestrictions: {locality: 'New York'}
- }, function(results, status) {
- if (status == google.maps.GeocoderStatus.OK) {
- map.setCenter(results[0].geometry.location);
- map.setZoom(15);
- } else {
- window.alert('We could not find that location - try entering a more' + ' specific place.');
- }
- });
- }
- }
- </script>
- <script async defer
- src="https://maps.googleapis.com/maps/api/js?libraries=drawing,geometry&key=MYKEY=3&callback=initMap">
- </script>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement