Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <html>
- <head>
- <meta name="viewport" content="initial-scale=1.0, user-scalable=no"/>
- <title>Reach Me</title>
- <script type="text/javascript" src="http://www.google.com/jsapi?key={{GOOGLE_API_KEY}}"></script>
- <script type="text/javascript">
- google.load("jquery", "1.4.2");
- google.load("maps", "3", {"other_params":"sensor=true"});
- </script>
- <script type="text/javascript" src="/static/js/gears_init.js"></script>
- <script type="text/javascript">
- $(document).ready(function() {
- var visitor_loc = new google.maps.LatLng(32.5468, -23.2031); //default location is world center
- var map;
- var directionDisplay;
- var directionsService;
- var stepDisplay;
- var markerArray = [];
- // Create a map and center on it
- var myOptions = {
- zoom: 8,
- mapTypeId: google.maps.MapTypeId.ROADMAP,
- center: visitor_loc
- }
- map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
- // Instantiate a directions service.
- directionsService = new google.maps.DirectionsService();
- // Create a renderer for directions and bind it to the map.
- var rendererOptions = {
- map: map
- }
- directionsDisplay = new google.maps.DirectionsRenderer(rendererOptions)
- // Try W3C Geolocation (Preferred)
- if(navigator.geolocation) {
- navigator.geolocation.getCurrentPosition(function(position) {
- calcRoute(position.coords.latitude,position.coords.longitude);
- },fallback);
- }
- //Fall back on Gears
- else if (window.google && window.google.gears) {
- var geo = google.gears.factory.create('beta.geolocation');
- geo.getCurrentPosition(function(position) {
- calcRoute(position.latitude,position.longitude);
- },fallback,{enableHighAccuracy: true});
- }
- else {
- fallback();
- }
- function fallback() {
- if (google.loader.ClientLocation && google.loader.ClientLocation.address.country_code == "US") {
- calcRoute(google.loader.ClientLocation.latitude, google.loader.ClientLocation.longitude);
- } else {
- calcRoute({{latitude}},{{longitude}});
- }
- }
- function calcRoute(lati,longi) {
- // Instantiate an info window to hold step text.
- stepDisplay = new google.maps.InfoWindow();
- // First, remove any existing markers from the map.
- for (i = 0; i < markerArray.length; i++) {
- markerArray[i].setMap(null);
- }
- // Now, clear the array itself.
- markerArray = [];
- // Retrieve the start and end locations and create
- // a DirectionsRequest using DRIVING directions.
- var start = new google.maps.LatLng(lati,longi);
- var end = "{{BUSINESS_LOCATION}}";
- var request = {
- origin: start,
- destination: end,
- travelMode: google.maps.DirectionsTravelMode.DRIVING
- };
- // Route the directions and pass the response to a
- // function to create markers for each step.
- directionsService.route(request, function(response, status) {
- if (status == google.maps.DirectionsStatus.OK) {
- var warnings = document.getElementById("warnings_panel");
- warnings.innerHTML = "<b>" + response.routes[0].warnings + "</b>";
- directionsDisplay.setDirections(response);
- showSteps(response);
- }
- });
- }
- function showSteps(directionResult) {
- // For each step, place a marker, and add the text to the marker's
- // info window. Also attach the marker to an array so we
- // can keep track of it and remove it when calculating new
- // routes.
- var myRoute = directionResult.routes[0].legs[0];
- for (var i = 0; i < myRoute.steps.length; i++) {
- var marker = new google.maps.Marker({
- position: myRoute.steps[i].start_point,
- map: map
- });
- attachInstructionText(marker, myRoute.steps[i].instructions);
- markerArray[i] = marker;
- }
- }
- function attachInstructionText(marker, text) {
- google.maps.event.addListener(marker, 'click', function() {
- // Open an info window when the marker is clicked on,
- // containing the text of the step.
- stepDisplay.setContent(text);
- stepDisplay.open(map, marker);
- });
- }
- });
- </script>
- </head>
- <body style="margin:0px; padding:0px;">
- <div id="map_canvas" style="width:100%;height:80%"></div>
-
- <div id="warnings_panel" style="width:100%;height:10%;text-align:center"></div>
- <div id="info"> <b>Your Approx. Location (A)</b>: LatLng = ({{latitude}},{{longitude}}) <br/> <b>My Google Latitude Location (B)</b>: {{BUSINESS_LOCATION}}</div>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement