geolocate.html 7.73 KB
Newer Older
1
{% load i18n %}
2 3 4 5 6 7 8 9 10 11 12 13
<!DOCTYPE html>
<html lang="en" style="height: 100%">
  <head>
    <meta charset="utf-8">
    <title>Eduroam</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="description" content="">
    <meta name="author" content="">

    <script type="text/javascript" src="/static/js/jquery.min.js"></script>
    <link href="/static/css/bootstrap.min.css" rel="stylesheet">
    <script src="/static/js/bootstrap.min.js"></script>
14
    		<script type="text/javascript" src="https://maps.google.com/maps/api/js?v=3.exp&sensor=true&language=en&libraries=places"></script>
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
    
    <style>
   
       html, body {
  height: 100%;
  margin: 0;
  overflow: hidden;
}
#map_canvas {
  /*
  width: 100%;
  height: 100%;
  */
  position: relative;
  height: 100%;
  width: 100%;
  bottom: 0;
  left: 0;
  right: 0;
  top: 40px;
}


.navbar-fixed-top{
margin-bottom: 0px;
}



    </style>
    <!--[if lt IE 9]>
      <script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
    <![endif]-->
	

<script type="text/javascript">
	var lat = 36.97;
	var lng = 23.71;
	var zoomLevel = 6;
	var latlng = new google.maps.LatLng(lat, lng);
	var map = '';
	var bounds = '';
	var image = '';
	var marker = '';
	var eduMarker = false;
	var infoWindow;
	var getOnce = false;
62
	var geocoder = null;
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
	var infoWindow;
	var directionDisplay;
    var directionsService = new google.maps.DirectionsService();
	
	var styles = [{
		url : '/static/img/edugroup.png',
		height : 54,
		width : 63,
		textColor : '#ffffff',
		textSize : 11
	}, {
		url : '/static/img/edugroup.png',
		height : 54,
		width : 63,
		textColor : '#ffffff',
		textSize : 11
	}, {
		url : '/static/img/edugroup.png',
		height : 54,
		width : 63,
		textColor : '#ffffff',
		textSize : 11
	}];

	function initialize() {
		
		image = new google.maps.MarkerImage('/static/img/edupin.png',
			new google.maps.Size(29, 40),
			new google.maps.Point(0, 0),
			new google.maps.Point(14, 40)
		);
		var styleArray = [{
			featureType : "all",
			stylers : [{
				saturation : -60
			}, {
				gamma : 1.00
			}]
		}, {
			featureType : "poi.business",
			elementType : "labels",
			stylers : [{
				visibility : "off"
			}]
		}, {
			"featureType" : "transit.line",
			"elementType" : "geometry",
			"stylers" : [{
				"visibility" : "off"
			}]
		}, {
			"featureType" : "poi",
			"elementType" : "all",
			"stylers" : [{
				"visibility" : "off"
			}]
		}, {
			'featureType' : "administrative.country",
			'elementType' : "labels",
			'stylers' : [{
				'visibility' : "off"
			}]
		}];
		var mapOptions = {
			center : latlng,
			zoom : zoomLevel,
			mapTypeId : google.maps.MapTypeId.ROADMAP,
			styles : styleArray,
			mapTypeId : google.maps.MapTypeId.ROADMAP,
			mapTypeControlOptions : {
				style : google.maps.MapTypeControlStyle.DEFAULT
			},
			navigationControl : true,
			mapTypeControl : false,
		};
		map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions);
139
		
140 141
		var input = document.getElementById('searchTextField');
	    var autocomplete = new google.maps.places.Autocomplete(input);
142

143
	    autocomplete.bindTo('bounds', map);
144
		
145 146 147
		infoWindow = new google.maps.InfoWindow();
		bounds = new google.maps.LatLngBounds();
		directionsDisplay = new google.maps.DirectionsRenderer();
148
		geocoder = new google.maps.Geocoder();
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
		directionsDisplay.setMap(map);
		
		if (getOnce == false) {
				marker = new google.maps.Marker({
					position : latlng,
					draggable : true,
					animation : google.maps.Animation.DROP,

				});
				marker.setMap(map);
			}
		google.maps.event.addListener(map, 'idle', function() {
			if(navigator.geolocation && getOnce == false) {
				navigator.geolocation.getCurrentPosition(getPosition);
			}

		});
		google.maps.event.addListener(map, 'click', function(event) {
			moveMarker(event.latLng);
		});
169
		
170
		google.maps.event.addListener(autocomplete, 'place_changed', function() {
171 172 173 174 175 176 177 178 179
			var place = autocomplete.getPlace();
			console.log(place);
			if (place.geometry.viewport) {
	            map.fitBounds(place.geometry.viewport);
	          } else {
	            map.setCenter(place.geometry.location);
	            map.setZoom(17);  // Why 17? Because it looks good.
	          }
			moveMarker(place.geometry.location);
180
		})
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
	}
	
	function calcRoute(start, end) {
		$("#distanceText").html();
        var request = {
            origin:start,
            destination:end,
            travelMode: google.maps.DirectionsTravelMode.WALKING
        };
        directionsService.route(request, function(response, status) {
          if (status == google.maps.DirectionsStatus.OK) {
            directionsDisplay.setDirections(response);
            var route = response.routes[0];
            var distText = route.legs[0].distance.text;
            $("#distanceText").html(distText);
          }
        });
      }

	function moveMarker(position) {
		marker.setPosition(position);
		getClosest(position);
	}

	function getPosition(position) {
		latlng = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
		getOnce = true;
		map.setCenter(latlng);
		map.setZoom(12);
		marker.setPosition(latlng);
		getClosest(latlng);
	}

	function getClosest(coords){
		$.ajax({
			url:"{% url closest %}", 
			data: {"lat": coords.lat(), "lng": coords.lng()},
			type: "GET",
			cache: false,
			success:function(data){
				bounds = new google.maps.LatLngBounds();
				if (eduMarker){
					eduMarker.setMap(null);
				}
				bounds.extend(coords);
				bounds.extend(new google.maps.LatLng(data.lat, data.lng));
				eduMarker = new google.maps.Marker({
					position : new google.maps.LatLng(data.lat, data.lng),
					draggable : true,
					'icon': image,
					animation : google.maps.Animation.DROP,

				});
				eduMarker.setMap(map);
				map.fitBounds(bounds);
				calcRoute(coords, new google.maps.LatLng(data.lat, data.lng));
				google.maps.event
															.addListener(
																	eduMarker,
																	'click',
																	function() {
																		infoWindow.content = "<div>"
																				+ data.text 
																				+ "</div>";
																		infoWindow
																				.open(
																						map,
																						eduMarker);
																	});
			}
		});

	}

255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
function autocomplete() {

        var input = $('#searchTextField');
        var autocomplete = new google.maps.places.Autocomplete(input);

        autocomplete.bindTo('bounds', map);

        var marker = new google.maps.Marker({
          map: map
        });

        google.maps.event.addListener(autocomplete, 'place_changed', function() {
          console.log(autocomplete)
          


        });

        // Sets a listener on a radio button to change the filter type on Places
        // Autocomplete.

      }

278 279

	$(document).ready(function() {
280
		alert("Beware that the accuracy of your geolocation depends highly on your connection type (wired, wireless, mobile data plan)");
281
		initialize();
282
	
283 284 285 286 287 288 289 290 291 292 293

	});

		</script>
  </head>

  <body>

<div class="navbar navbar-fixed-top">
      <div class="navbar-inner">
        <div class="container">
294
          <a class="brand" href="#" style="font-size: 14px;"><img src="/static/img/eduroam_trans_80pix.png" style="height:20px"> near me</a>
295 296 297 298
          <div class="nav-collapse collapse">
          	
            <ul class="nav">
              <li></li>
299 300
              <li id="distanceText" style="line-height:40px;" style="font-size: 14px;"></li>
              <li></li>
301
              <div class="navbar-search pull-left" style="margin-left: 5px;">
302
                  <input type="text" class="search-query" placeholder="Search" id="searchTextField" style="width: 150px;">
303
                </div>
304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326
            </ul>
            
          </div><!--/.nav-collapse -->
        </div>
      </div>
    </div>

<div id="map_canvas"></div>
	

  </body>
</html>