Fix for place search by area when no keyword and category is specified and area is... 03/92203/1
authorVarun <tallytalwar@gmail.com>
Thu, 13 Oct 2016 23:04:18 +0000 (19:04 -0400)
committerVarun <tallytalwar@gmail.com>
Fri, 14 Oct 2016 01:05:08 +0000 (21:05 -0400)
- Uses an approximation to calculate radius and center of the circle from the bounding rectangle and does then uses the
nearby pelias endpoint

Change-Id: I2e3f57abcc869d12911b5b748057009d87fc23c2

src/mapzen_plugin.c

index 6e874ab..9a95186 100644 (file)
@@ -16,6 +16,7 @@
 
 #include <stdio.h>
 #include <stdlib.h>
+#include <math.h>
 #include "mapzen_plugin.h"
 #include "mapzen_plugin_internal.h"
 #include "mapzen_api.hpp"
@@ -31,6 +32,7 @@
 
 #define DEFAULT_NUM_RESULTS    10
 #define _PROVIDER_KEY_MAX_SIZE 1024
+#define R_EARTH 6378137.0
 
 static const double LATITUDE_RANGE = 85.05113;
 static const double LONGITUDE_RANGE = 180.0;
@@ -1564,16 +1566,6 @@ EXPORT_API int maps_plugin_search_place_by_area(const maps_area_h boundary, cons
        if (searchKeyword)
                place_req->search_string = g_strdup_printf("%s", searchKeyword);
 
-       if (!searchKeyword && !categoryId) {
-               g_free(calldata_place);
-               calldata_place = NULL;
-               g_free(place_req->maps_key);
-               place_req->maps_key = NULL;
-               g_free(place_req);
-               place_req = NULL;
-               return MAPS_ERROR_INVALID_PARAMETER;
-       }
-
        if (searchKeyword) {
                g_free(searchKeyword);
                searchKeyword = NULL;
@@ -1599,11 +1591,17 @@ EXPORT_API int maps_plugin_search_place_by_area(const maps_area_h boundary, cons
                                place_req->boundary->circle.center.longitude = bound->circle.center.longitude;
                                place_req->boundary->circle.radius = bound->circle.radius;
                        } else if (bound->type == MAPS_AREA_RECTANGLE) {
-                               place_req->boundary->type = MAPZEN_BOUNDARY_RECT;
-                               place_req->boundary->rect.top_left.latitude = bound->rect.top_left.latitude;
-                               place_req->boundary->rect.top_left.longitude = bound->rect.top_left.longitude;
-                               place_req->boundary->rect.bottom_right.latitude = bound->rect.bottom_right.latitude;
-                               place_req->boundary->rect.bottom_right.longitude = bound->rect.bottom_right.longitude;
+                               place_req->boundary->type = MAPZEN_BOUNDARY_CIRCLE;
+                               gdouble centerLat = (bound->rect.top_left.latitude + bound->rect.bottom_right.latitude) * 0.5;
+                               gdouble centerLon = (bound->rect.top_left.longitude + bound->rect.bottom_right.longitude) * 0.5;
+                               //aprox radius calculation (Equirectangular approximation) - works great for small distances
+                               gdouble dLat = degrees_to_radians(bound->rect.top_left.latitude - bound->rect.bottom_right.latitude);
+                               gdouble dLon = degrees_to_radians(bound->rect.top_left.longitude - bound->rect.bottom_right.longitude);
+                               gdouble mLat = degrees_to_radians(bound->rect.top_left.latitude + bound->rect.bottom_right.latitude) * 0.5;
+                               gdouble radius = R_EARTH * 0.5 * sqrt( pow((dLon * cos(mLat)), 2.0) + pow(dLat, 2.0) );
+                               place_req->boundary->circle.center.latitude = centerLat;
+                               place_req->boundary->circle.center.longitude = centerLon;
+                               place_req->boundary->circle.radius = radius;
                        }
                }
        }