Bwt Developer
Bwt Developer

Reputation: 51

Latitude Longitude sorting

I want to sort latitude longitude.

I have 10 latitudes/longitudes and I want to draw a route on them, but I don't know which is origin and which is destination. For that I want to sort them and draw a route.

My points are like this:

 [{lat:23.015422, long:72.540037},{lat:23.020320, long:72.557889},{lat:23.006890, long:72.563468},{lat:23.020873,long: 72.534372}]

Is there any way? Does Google provide this functionality?

Upvotes: 0

Views: 2224

Answers (2)

Teyam
Teyam

Reputation: 8092

Since you don't want to specify which of the given coordinates is the origin (a required parameter in sending Directions Request), an option that you can use is to connect each LatLng locations such as described in Polylines.

A Polyline object consists of an array of LatLng locations, and creates a series of line segments that connect those locations in an ordered sequence.

Here is a sample code to add a Polyline as shown in the documentation:

// This example creates a 2-pixel-wide red polyline showing the path of William
// Kingsford Smith's first trans-Pacific flight between Oakland, CA, and
// Brisbane, Australia.

function initMap() {
  var map = new google.maps.Map(document.getElementById('map'), {
    zoom: 3,
    center: {lat: 0, lng: -180},
    mapTypeId: 'terrain'
  });

  var flightPlanCoordinates = [
    {lat: 37.772, lng: -122.214},
    {lat: 21.291, lng: -157.821},
    {lat: -18.142, lng: 178.431},
    {lat: -27.467, lng: 153.027}
  ];
  var flightPath = new google.maps.Polyline({
    path: flightPlanCoordinates,
    geodesic: true,
    strokeColor: '#FF0000',
    strokeOpacity: 1.0,
    strokeWeight: 2
  });

  flightPath.setMap(map);
}

After being able to connect all given coordinates, you may also have the option to configure the shapes using polyline's editable and draggable properties with Polyline class.

To help you further with the implementation, you may also refer to suggested solution in this related SO post wherein working code snippets were also provided. Hope it helps!

Upvotes: 0

Javier Moreno
Javier Moreno

Reputation: 13

Actually the route that you want to create represents the correct order of the points to follow that path and unless you compare them against real routes (to try to find one that matches the subset of points that you have) I don't think you will be able to determinate which of them is the starting or the finishing point.

In other words, if you have the points for Albacete, Barcelona and Cartagena a traveller can visit all three cities starting from any of them.

Upvotes: 1

Related Questions