54984

Google Map directions service

Question:

You could see View example (directions-simple.html).

In the above example from and to is given by the user but i'm trying to get <strong>From</strong> automatically by detecting the user location through browser and <strong>To</strong> is been inputted by user.

I tried this dpaste.com/695949 but could not get it working.

Thanks in advance!!

Answer1:

You've got a number of JavaScript errors in your example at <a href="http://dpaste.com/695949" rel="nofollow">http://dpaste.com/695949</a>. I cleaned up the core of it, getting the geolocation and setting the map center there. I think you can then go from there to figure out the directions piece, but basically you'll need to have an input box for your user to enter the location they want to go to, and you can use the current map center as the starting point for your directions.

var directionDisplay; var directionsService = new google.maps.DirectionsService(); var map; function mapinit(position){ directionsDisplay = new google.maps.DirectionsRenderer(); var chicago = new google.maps.LatLng(41.850033, -87.6500523); var myOptions = { zoom:7, mapTypeId: google.maps.MapTypeId.ROADMAP, center: chicago } map = new google.maps.Map(document.getElementById("mapcont"), myOptions); directionsDisplay.setMap(map); initiate_geolocation() } function initiate_geolocation() { navigator.geolocation.getCurrentPosition(handle_position_results); } function handle_position_results(position){ newCenter = new google.maps.LatLng(position.coords.latitude,position.coords.longitude); map.setCenter(newCenter); }

Answer2:

Sure, just ask the browser where the user is, through the <a href="http://dev.w3.org/geo/api/spec-source.html" rel="nofollow">W3 Geolocation API Specification</a>. Since this may changes across different browser and devices, I'd try with a library like <a href="http://code.google.com/p/geo-location-javascript/" rel="nofollow">geo-location-javascript</a>.

Recommend

  • ng-map show the best way between two points (waypoints) [closed]
  • How to serialize Django GeoPt for Elasticsearch
  • Pass javascript array to another page
  • PHP code to check country from IP
  • Ruby and class variables in inherit class
  • Insertion large number of Entities into SQL Server 2012 [duplicate]
  • Efficient algorithm to find additions and removals from 2 collections
  • How to repeat sections of a SQL query across UNIONs? (DRY in SQL)
  • C++ cout and enum representations
  • cell spacing in div table
  • Most efficient way to move table rows from one table to another
  • Plotting densities in R
  • XSLT foreach repeating nodes to flat
  • How to create a 2D image by rotating 1D vector of numbers around its center element?
  • How do I get the list of bad records that didn't load in Bigquery?
  • Update Google Maps traffic layer without page reloading
  • Thread 1: EXC_BAD_ACCESS (code =1 address = 0x0)
  • Ember.js model to be organised as a tree structure
  • Angular2 component view does not update on value change via method
  • CakePHP ACL tutorial initDB function warnings
  • Thread safety of a fluent like class using clone() and non final fields
  • WPF - CanExecute dosn't fire when raising Commands from a UserControl
  • Installing iPhone App to iPhone
  • How to delay loading a property with linq to sql external mapping?
  • Java Scanner input dilemma. Automatically inputs without allowing user to type
  • How to add a column to a Pandas dataframe made of arrays of the n-preceding values of another column
  • script to move all files from one location to another location
  • 'TypeError' while using NSGA2 to solve Multi-objective prob. from pyopt-sparse in OpenMDAO
  • what is the difference between the asp.net mvc application and asp.net web application
  • Matplotlib draw Spline from multiple points
  • SetUp method failed while running tests from teamcity
  • Matrix multiplication with MKL
  • Proper folder structure for lots of source files
  • How get height of the a view with gone visibility and height defined as wrap_content in xml?
  • JTable with a ScrollPane misbehaving
  • Understanding cpu registers
  • Linking SubReports Without LinkChild/LinkMaster
  • Running Map reduces the dimensions of the matrices
  • Binding checkboxes to object values in AngularJs
  • Android Heatmap on canvas or ImageView