1

How to find nearest unit to specific location

Topic: How to find nearest unit to specific location

Dear partners

We want to do something like Nearest Unit tool in monitoring panel. Is there any way to do it by remote API or any trick to do such thing?

2

How to find nearest unit to specific location

Re: How to find nearest unit to specific location

hhamedk, it is not very easy to make such tool.
Do You use some maps framework (OpenLayers, Leaflet)?

Very simple realization (without taking into account geofences, without routing and without dynamic update of units' positions) can be made in a few steps:
1. Catch double click event on map
2. Add marker to cursor's position
3. Using remote api request get all units whose last message time > now - some value (1 hour, 10 minutes or smth) in seconds.
For example such request:

https://hst-api.wialon.com/wialon/ajax.html?svc=core/search_items&params={"spec":{"itemsType":"avl_unit","propName":"rel_last_msg_date","propValueMask":">1465280000","sortType":"sys_name"},"force":1,"flags":0x00400401,"from":0,"to":0}&sid=sid

4. Calculate distances between marker's position and position of each unit.
5. Sort units by distance and return first 5 of them. It's 5 closest units to marker.

Example code for Openlayers 2.13 https://jsfiddle.net/wv4rgx0j/
(replace sid to Yours)

Head of Wialon Local Department
Gurtam
3

How to find nearest unit to specific location

Re: How to find nearest unit to specific location

Deal, i discussed about this issue with Sergey in Minsk. He believed that it's not a right way. Anyone can help us in this case?

4

How to find nearest unit to specific location

Re: How to find nearest unit to specific location

hhamedk, You want to get list of units that are near some coordinates over one request without using maps?
When You say location You mean address or coordinates.
What radius should be checked? Is unit's data actuality important?

There is no such remote api request for now.

But You can calculate closest unit to some coordinates yourself.
You need to calculate distance between Your coordinates and each unit.
And take smallest distance.
There is method wialon.util.Geometry.getDistance(lat1, lon1, lat2, lon2) in SDK JS.

If You can't use SDK JS there are a lot of realizations of such algorithm:
http://stackoverflow.com/questions/3658 … oordinates
And the simplest for small distances:
Math.sqrt((lat2 - lat1)*(lat2 - lat1) + (lon2 - lon1)*(lon2 - lon1)) * 111000.0;

Head of Wialon Local Department
Gurtam
5

How to find nearest unit to specific location

(edited by hhamedk 20/11/2016 02:04:04)

Re: How to find nearest unit to specific location

deal, thanks for your clarification. But what we need is nearest unit based on routes not only nearest by straight line. May you help us in this case?

6

How to find nearest unit to specific location

Re: How to find nearest unit to specific location

hhamedk, You can use new SDK request getOneToManyRoute.
Example request:
wialon.util.Gis.getOneToManyRoute(55.59489584806276, 28.60730229579555, [{"lat":52.54072112733899,"lon":30.782595264242484}], function(){console.log(arguments);})
Example response: [{"status":"OK","distance":{"text":"426.33 km","value":426334.84},"duration":{"text":"5 h 19 min","value":19143}},{"status":"OK","distance":{"text":"21.83 km","value":21831.8},"duration":{"text":"19 min","value":1166}}]

Head of Wialon Local Department
Gurtam
7

How to find nearest unit to specific location

Re: How to find nearest unit to specific location

Hello. Any ideea how can i sort the unit list by ascending based on distance. For the moment it is not sorting correctly, for example i have distances: 100 km, 300 m, 20 km. Instead of listing them like this: 300 m, 20 km, 100 km, they are somehow listed randomly...
If i remove this part:

dist = dist > 1000 ? (dist / 1000).toFixed(3) + " km" : dist + " m";

but i don't have the right calculation.


deal wrote:

hhamedk, it is not very easy to make such tool.
Do You use some maps framework (OpenLayers, Leaflet)?

Very simple realization (without taking into account geofences, without routing and without dynamic update of units' positions) can be made in a few steps:
1. Catch double click event on map
2. Add marker to cursor's position
3. Using remote api request get all units whose last message time > now - some value (1 hour, 10 minutes or smth) in seconds.
For example such request:

https://hst-api.wialon.com/wialon/ajax.html?svc=core/search_items&params={"spec":{"itemsType":"avl_unit","propName":"rel_last_msg_date","propValueMask":">1465280000","sortType":"sys_name"},"force":1,"flags":0x00400401,"from":0,"to":0}&sid=sid

4. Calculate distances between marker's position and position of each unit.
5. Sort units by distance and return first 5 of them. It's 5 closest units to marker.

Example code for Openlayers 2.13 https://jsfiddle.net/wv4rgx0j/
(replace sid to Yours)

8

How to find nearest unit to specific location

Re: How to find nearest unit to specific location

florin1
Where did you get this values 100 km, 300 m, 20 km ? What request did you use?

Its pretty natural that you have to use raw value in meters for sorting. You can try to get raw data from response on convert it manually like this

function convert(val){
   val = val.split(' ');
   return val[0] * (val[1] == 'm' ? 1 : 1000);
}
9

How to find nearest unit to specific location

Re: How to find nearest unit to specific location

it was just an example. i fixed it, yes you wew right about raw data. The sorting was done with the "km" string included, and that is why i got wrong results. So baically  the sorting should be done like this:
Just raw data in the getClosestUnits() function:

 
var dist = line.getGeodesicLength(Mercator);
unitsDistances = unitsDistances.sort(function(a, b){return a.dist - b.dist}).slice(0, count);
return unitsDistances;

And when outputing the results:

for (var i = 0; i < units.length; i++) {
distance = units[i].dist > 1000 ? (units[i].dist / 1000).toFixed(3) + " km" : units[i].dist + " m";

So this works now, i get distance from unit to a added marker in an ascending order.
Now i have another problem, and i just can't figure it out. Because i am a newbie to OpenLayers 2, i am trying to make a autocomplete Google address field, where i can search for address and then automatically mark it on the OpenStreet maps. For now i have this:

<script type="text/javascript" src="http://maps.google.com/maps/api/js?key=zzzzzzzz&libraries=places&language=en-EN"></script>
        <script>
            var autocomplete = new google.maps.places.Autocomplete($("#address")[0], {});

            google.maps.event.addListener(autocomplete, 'place_changed', function() {
                var place = autocomplete.getPlace();
                var longi = place.geometry.location.lng();
                var lati = place.geometry.location.lat();
                 console.log (longi + " - " + lati)
                
            });
        </script>

The above code works and i get autocomplete results in a input field. But uppon selecting the address name, i don't know how to add the lat and long to OpenLayers map. So somehow create a marker from the longitude and latitude from google and put in on the openlayers map. Any advice?

10

How to find nearest unit to specific location

Re: How to find nearest unit to specific location

florin1
First of all, don't use OpenLayers 2, its deeply outdated. If you're already using Google Maps - you can consider using Google Maps JS API Another variants are Leaflet or Openlayers 3

So somehow create a marker from the longitude and latitude from google and put in on the openlayers map.

Most of map libraries have similar logic for it
- somehow get LatLng (in your case from Google place)
- create Marker with LatLng and add it to Map
- change map center with Map.setCenter(LatLng)

Here's useful link for you

PS please remember that we're discussing Wialon problems on this forum. All other problems can be solved with http://stackoverflow.com wink

11

How to find nearest unit to specific location

Re: How to find nearest unit to specific location

Yes, you are right, it is outdated but i was too lazy to change to google maps. But considering the advantages of google maps api, i will probably give it a go.

Thanks smile

12

How to find nearest unit to specific location

Re: How to find nearest unit to specific location

deal wrote:

hhamedk, You can use new SDK request getOneToManyRoute.
Example request:
wialon.util.Gis.getOneToManyRoute(55.59489584806276, 28.60730229579555, [{"lat":52.54072112733899,"lon":30.782595264242484}], function(){console.log(arguments);})
Example response: [{"status":"OK","distance":{"text":"426.33 km","value":426334.84},"duration":{"text":"5 h 19 min","value":19143}},{"status":"OK","distance":{"text":"21.83 km","value":21831.8},"duration":{"text":"19 min","value":1166}}]

Would you please share document link of this API?

13

How to find nearest unit to specific location

Re: How to find nearest unit to specific location

Hi,

I have found to get the Nearest vehicles list from a Location from Javascript : https://sdk.wialon.com/playground/demo/nearest_units

But client want to call a url from AJAX or API call and return the vehicle list in JSON format.

So, can you suggest Remote API sample or example to call from ajax or CURL method


Thanks,
Venu

14

How to find nearest unit to specific location

Re: How to find nearest unit to specific location

hhamedk wrote:

deal wrote:

hhamedk, You can use new SDK request getOneToManyRoute.
Example request:
wialon.util.Gis.getOneToManyRoute(55.59489584806276, 28.60730229579555, [{"lat":52.54072112733899,"lon":30.782595264242484}], function(){console.log(arguments);})
Example response: [{"status":"OK","distance":{"text":"426.33 km","value":426334.84},"duration":{"text":"5 h 19 min","value":19143}},{"status":"OK","distance":{"text":"21.83 km","value":21831.8},"duration":{"text":"19 min","value":1166}}]

Would you please share document link of this API?


Hi,

I have found to get the Nearest vehicles list from a Location from Javascript : https://sdk.wialon.com/playground/demo/nearest_units

But client want to call a url from AJAX or API call and return the vehicle list in JSON format.

So, can you suggest Remote API sample or example to call from ajax or CURL method

15

How to find nearest unit to specific location

Re: How to find nearest unit to specific location

venujakku wrote:
But client want to call a url from AJAX or API call and return the vehicle list in JSON format.

Gurtam Team,
We have same problem. We mostly looking for Remote API call and while we are talking about integration of Wialon with other systems there is no user and JS won't work. Please offer us a way.

16

How to find nearest unit to specific location

Re: How to find nearest unit to specific location

hhamedk wrote:

venujakku wrote:
But client want to call a url from AJAX or API call and return the vehicle list in JSON format.

Gurtam Team,
We have same problem. We mostly looking for Remote API call and while we are talking about integration of Wialon with other systems there is no user and JS won't work. Please offer us a way.


Yes, We can not use JS sdk in all the cases.

Can you suggest how can I us existing Remote API methods to find the nearest vehicles list.
Requirement :
Step 1: Enter the Location/Lat or Langitude

Step 2 : Display all vehicles list with Distance from above location in KMs

17

How to find nearest unit to specific location

Re: How to find nearest unit to specific location

venujakku, there is no such one remote api request, which can return the list of nearest units to specified location.
But we'll try to find suitable solution.

Head of Wialon Local Department
Gurtam
18

How to find nearest unit to specific location

Re: How to find nearest unit to specific location

deal, thanks a lot. We are wiating...do your best, please!

19

How to find nearest unit to specific location

Re: How to find nearest unit to specific location

I am using gurtam api for finding distance between one location to many locations i.e. wialon.util.Gis.getOneToManyRoute
But as i am checking the response and compare it with google the distance coming is so much different as

From location: 23.2599333, 77.41261499999996

To location                                         Distance according to gurtam            Distance according to google
23.21849072, 75.90737847                206.68 km                                         190km
23.22619722, 74.84113659                331.61 km                                          316km
23.21747726, 75.96546789               201.16 km                                          182km
23.27065634, 76.44775161                185.84 km                                         127km

Please check and clarify why their is lots of difference between wialon response and google response.

20

How to find nearest unit to specific location

Re: How to find nearest unit to specific location

deal wrote:

venujakku, there is no such one remote api request, which can return the list of nearest units to specified location.
But we'll try to find suitable solution.

It's like one year that we are waiting for a solution from your side! Please inform us about the result.

21

How to find nearest unit to specific location

Re: How to find nearest unit to specific location

deal wrote:

hhamedk, You can use new SDK request getOneToManyRoute.
Example request:
wialon.util.Gis.getOneToManyRoute(55.59489584806276, 28.60730229579555, [{"lat":52.54072112733899,"lon":30.782595264242484}], function(){console.log(arguments);})
Example response: [{"status":"OK","distance":{"text":"426.33 km","value":426334.84},"duration":{"text":"5 h 19 min","value":19143}},{"status":"OK","distance":{"text":"21.83 km","value":21831.8},"duration":{"text":"19 min","value":1166}}]

How we can use it in WL?

22

How to find nearest unit to specific location

Re: How to find nearest unit to specific location

Hi GT

Please anyone help us to get nearest units list to one location via remote API!

23

How to find nearest unit to specific location

Re: How to find nearest unit to specific location

Hello Hamed!

there is not exact request, you need to proceed some data
for example, you can try to execute request presence in geozones

Diana Cheley
Wialon Hosting Expert
Gurtam