Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SnaptoRoads - A path with 58 locations only returns 5 snappedPoints back #62

Closed
ericbrunner opened this issue Nov 21, 2017 · 14 comments
Closed
Assignees

Comments

@ericbrunner
Copy link

Hi,

I have that map with 58 markers.
map_with_pins

I put that 58 locations as path to the SnapToRoadsRequest instance but I only get 5 SnappedPoints back.

Any idea?

I put the JSON response (my data where I put the snappedPoints inside) as gist

Here my code:

public sealed class GoogleMapsApiHelper
    {
        public static string ApiKey { get; set; }

        public static IEnumerable<TruckFahrerGeoDataWithSnapToRoad> GetSnapToRoadPoints(TruckFahrerGeoData[] truckFahrerGeoData)
        {
            //TODO make method ASYNC  !!!

            // TODO That current only worksd with a max. of 100 waypoints !. Adjust when prototyp is working.
            List<TruckFahrerGeoDataWithSnapToRoad> truckFahrerGeoDataWithSnapToRoadPoints = new List<TruckFahrerGeoDataWithSnapToRoad>();

            int currentIndex = 0;
            foreach (TruckFahrerGeoData fahrerGeoData in truckFahrerGeoData)
            {
                truckFahrerGeoDataWithSnapToRoadPoints.Add(new TruckFahrerGeoDataWithSnapToRoad
                {
                    Id = fahrerGeoData.Id,
                    FahrerId = fahrerGeoData.FahrerId,
                    Imei = fahrerGeoData.Imei,
                    Latitude = fahrerGeoData.Latitude,
                    Longitude = fahrerGeoData.Longitude,
                    TimeStamp = fahrerGeoData.TimeStamp,
                    SnappedPoint = default(SnappedPoint),
                    Name = fahrerGeoData.Name,
                    TelemetryData = fahrerGeoData.TelemetryData,
                    SnappedPointIndex = currentIndex
                });

                currentIndex++;
            }
            
            Location[] requestPath = truckFahrerGeoDataWithSnapToRoadPoints
                .Select(geo => new GoogleApi.Entities.Common.Location((double) geo.Latitude, (double) geo.Longitude))
                .ToArray();

            /*
             * TODO
             * 
             * 1. get the lat/lon points out of the truckFahrerGeoData array as an array of Location instances
             * 2. build the SnapToRoadsRequest with each of the lat/lon points
             * 3. pass the request to the SnapToRoad.Query method and invoke
             * 4. get the result.SnappedPoints?.ToArray();
             * 5. add each SnappedPoint by its INDEX to the truckFahrerGeoDataWithSnapToRoadPoints.IndexOf(INDEX)
             * 
             * */


            SnapToRoadsRequest request = new SnapToRoadsRequest
            {
                Key = GoogleMapsApiHelper.ApiKey,
                Path = requestPath
            };


            SnapToRoadsResponse response = GoogleMaps.SnapToRoad.Query(request);
            
            if (response.SnappedPoints == null)
                return truckFahrerGeoDataWithSnapToRoadPoints;

            SnappedPoint[] snappedPoints = response.SnappedPoints.ToArray();
            foreach (SnappedPoint snappedPoint in snappedPoints)
            {
                TruckFahrerGeoDataWithSnapToRoad truckFahrerGeoDataWithSnapToRoad =
                    truckFahrerGeoDataWithSnapToRoadPoints.FirstOrDefault(tfg => tfg.SnappedPointIndex == snappedPoint.OriginalIndex);

                if (truckFahrerGeoDataWithSnapToRoad != null)
                {
                    truckFahrerGeoDataWithSnapToRoad.SnappedPoint = snappedPoint;
                }
            }
            return truckFahrerGeoDataWithSnapToRoadPoints;
        }
    }

Thanks in advance.

best

Eric

@vivet
Copy link
Owner

vivet commented Nov 21, 2017

Hi again
Sorry i haven't worked that much with Roads api
Try check the Response.RawJson. That shows what google returns.

@ericbrunner
Copy link
Author

response.RawJson

"{\n  \"snappedPoints\": [\n    {\n      \"location\": {\n        \"latitude\": 48.160701920080641,\n        \"longitude\": 15.615951312026901\n      },\n      \"originalIndex\": 0,\n      \"placeId\": \"ChIJdb0ylo6HbUcRhty45O33OIc\"\n    },\n    {\n      \"location\": {\n        \"latitude\": 48.163834926702592,\n        \"longitude\": 15.615102852000513\n      },\n      \"originalIndex\": 1,\n      \"placeId\": \"ChIJY-KcuIiHbUcRcLXeMxGzJlw\"\n    },\n    {\n      \"location\": {\n        \"latitude\": 48.162118789614105,\n        \"longitude\": 16.483688015927697\n      },\n      \"originalIndex\": 45,\n      \"placeId\": \"ChIJg46bP5CqbUcRK07KzaXoyRs\"\n    },\n    {\n      \"location\": {\n        \"latitude\": 48.16213290222106,\n        \"longitude\": 16.483567306372223\n      },\n      \"originalIndex\": 46,\n      \"placeId\": \"ChIJg46bP5CqbUcRK07KzaXoyRs\"\n    },\n    {\n      \"location\": {\n        \"latitude\": 48.162141252774212,\n        \"longitude\": 16.483495880930516\n      },\n      \"or
iginalIndex\": 47,\n      \"placeId\": \"ChIJg46bP5CqbUcRK07KzaXoyRs\"\n    }\n  ],\n  \"warningMessage\": \"Input path is too sparse. You should provide a path where consecutive points are closer to each other. Refer to the 'path' parameter in Google Roads API documentation.\"\n}\n"

I put my Path here (what I pass to the SnapToRoadsRequest.Path)

@vivet
Copy link
Owner

vivet commented Nov 21, 2017

But that is what google returns, which seems du be the same as the deserialized response

@ericbrunner
Copy link
Author

image

@vivet
Copy link
Owner

vivet commented Nov 21, 2017

Could be...
But the RawJson and the RawQueryString is good properties to check if you suspect my Api does something wrong.

@ericbrunner
Copy link
Author

Good to know. Thanks.

@ericbrunner
Copy link
Author

Would I have another option to snap the gps points to a road if I don't get as much points as google would need ? my drivers are often in areas with no or less connectivity and it occurs that I get only every few minutes gps coordinates. thanks in advance.

@ericbrunner ericbrunner reopened this Nov 21, 2017
@vivet
Copy link
Owner

vivet commented Nov 21, 2017

Not through Google.
You would need to use compass or similar to track the direction and add fake coordinates.
What is your business goal? you need the route or the distance?

@ericbrunner
Copy link
Author

My goal is a realistic route visualization along the real way my drivers drove. From above screenshot you can see that the polylines are straight connections between each point which is by design and d o n t snap on the road. That's what I want to achieve: Snao that polyline on the real road

@ericbrunner
Copy link
Author

Maybe the nearestroad API of the Roads API but as far as I read it is a very fragile stuff. For the SnapToRoads API the GPS waypoint should not exceed a distance of max. 300 meters.

@vivet
Copy link
Owner

vivet commented Nov 22, 2017

Yes you can try nearest roads.
Otherwise you can consider using directions api to "fake" the results.

@ericbrunner
Copy link
Author

regarding "directions api to 'fake' the result"

Yes that would be pretty much sufficient ...

PEASE could you give me a sample or starting point for such scenario as in my case, where I have several pre-collected waypoints ? Thanks in advance.

@vivet
Copy link
Owner

vivet commented Nov 22, 2017

Sorry i can't help much with that.
I support the GoogleApi, but not concrete business requirements.
I guess you have to experiment a bit.

@ericbrunner
Copy link
Author

Ok thanks

@vivet vivet self-assigned this May 3, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants