From d26e426d1896b92251ed59ee391b5690706c604f Mon Sep 17 00:00:00 2001 From: loetie Date: Mon, 6 Mar 2017 11:25:56 +0100 Subject: [PATCH 1/3] Country only set address on map "Set to my entered address" does not work when only country is selected. The country field uses country codes (ex: nl,uk,de etc). The google maps api does not know how to convert those. My suggestion would be to use the text of the selected option and send that to the google maps api. This seems to work better. --- lib/js/admin-geo.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/js/admin-geo.js b/lib/js/admin-geo.js index 5213b18..2ee6dcb 100644 --- a/lib/js/admin-geo.js +++ b/lib/js/admin-geo.js @@ -62,7 +62,7 @@ city: $('#address_city').val(), postcode: $('#address_postcode').val(), state: $('#address_state').val(), - country: $('#address_country').val(), + country: $('#address_country option:selected').text() z_delegate: 'mod_geo' }; z_notify("address_lookup", args); From b1bd685f96af9f1a2b6263b81f2b170533915d63 Mon Sep 17 00:00:00 2001 From: Loetie Kruger Date: Tue, 2 May 2017 17:06:33 +0200 Subject: [PATCH 2/3] Different query string for googlemaps and openstreetmap so country field works for both services --- lib/js/admin-geo.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/js/admin-geo.js b/lib/js/admin-geo.js index 2ee6dcb..059d46c 100644 --- a/lib/js/admin-geo.js +++ b/lib/js/admin-geo.js @@ -62,7 +62,8 @@ city: $('#address_city').val(), postcode: $('#address_postcode').val(), state: $('#address_state').val(), - country: $('#address_country option:selected').text() + country: $('#address_country option:selected').text(), + country_name: $('#address_country option:selected').text(), z_delegate: 'mod_geo' }; z_notify("address_lookup", args); From 70514c695261117442afd99d547455ffba375acf Mon Sep 17 00:00:00 2001 From: Loetie Kruger Date: Tue, 2 May 2017 17:07:26 +0200 Subject: [PATCH 3/3] Different query string for googlemaps and openstreetmap so country field works for both services --- mod_geo.erl | 57 ++++++++++++++++++++++++++++++++--------------------- 1 file changed, 35 insertions(+), 22 deletions(-) diff --git a/mod_geo.erl b/mod_geo.erl index 0f2cca7..446f7af 100644 --- a/mod_geo.erl +++ b/mod_geo.erl @@ -30,6 +30,7 @@ event/2, observe_search_query/2, observe_pivot_fields/3, + find_geocode_api/4, find_geocode_api/3 ]). @@ -39,14 +40,19 @@ %% @doc Handle an address lookup from the admin. %% @todo Maybe add check if the user is allowed to use the admin. event(#postback_notify{message="address_lookup"}, Context) -> - {ok, Type, Q} = q([ - {address_street_1, z_context:get_q("street", Context)}, - {address_city, z_context:get_q("city", Context)}, - {address_state, z_context:get_q("state", Context)}, - {address_postcode, z_context:get_q("postcode", Context)}, - {address_country, z_context:get_q("country", Context)} - ], Context), - case find_geocode_api(Q, Type, Context) of + Fields = [ + {address_street_1, z_context:get_q("street", Context)}, + {address_city, z_context:get_q("city", Context)}, + {address_state, z_context:get_q("state", Context)}, + {address_postcode, z_context:get_q("postcode", Context)}, + {address_country, z_context:get_q("country", Context)}, + {address_country_name, z_context:get_q("country_name", Context)} + ], + + {ok, Type, Qmaps} = q(Fields, address_country, Context), + {ok, _Type2, Qosm} = q(Fields, address_country_name, Context), + + case find_geocode_api(Qmaps, Qosm, Type, Context) of {error, _} -> z_script:add_script("map_mark_location_error();", Context); {ok, {Lat, Long}} -> @@ -104,7 +110,8 @@ optional_geocode(R, Context) -> ok; _ -> % Changed, and we are doing automatic lookups - case find_geocode_api(Q, Type, Context) of + Qosm = q(R, address_country_name, Context), + case find_geocode_api(Q, Qosm, Type, Context) of {error, _} -> reset; {ok, {NewLat,NewLong}} -> @@ -114,16 +121,19 @@ optional_geocode(R, Context) -> end end. +find_geocode_api(Q, Type, Context) -> + find_geocode_api(Q, Q, Type, Context). %% @doc Check with Google and OpenStreetMap if they know the address -find_geocode_api(Q, country, _Context) -> - Qq = mochiweb_util:quote_plus(Q), +find_geocode_api(_Qmaps, Qosm, country, _Context) -> + Qq = mochiweb_util:quote_plus(Qosm), openstreetmap(Qq); -find_geocode_api(Q, _Type, Context) -> - Qq = mochiweb_util:quote_plus(Q), +find_geocode_api(Qmaps, Qosm, _Type, Context) -> + Qq = mochiweb_util:quote_plus(Qmaps), case googlemaps_check(Qq, Context) of {error, _} -> - openstreetmap(Qq); + Qqo = mochiweb_util:quote_plus(Qosm), + openstreetmap(Qqo); {ok, {_Lat, _Long}} = Ok-> Ok end. @@ -242,17 +252,20 @@ get_json(Url) -> q(R, Context) -> + q(R, address_country, Context). + +q(R, CountryField, Context) -> Fs = iolist_to_binary([ - p(address_street_1, $,, R), - p(address_city, $,, R), - p(address_state, $,, R), - p(address_postcode, $,, R) - ]), + p(address_street_1, $,, R), + p(address_city, $,, R), + p(address_state, $,, R), + p(address_postcode, $,, R) + ]), case Fs of <<>> -> - {ok, country, iolist_to_binary(p(address_country, <<>>, R))}; - _ -> - Country = iolist_to_binary(country_name(proplists:get_value(address_country, R), Context)), + {ok, country, iolist_to_binary(p(CountryField, <<>>, R))}; + _ -> + Country = iolist_to_binary(country_name(p(CountryField, <<>>, R), Context)), {ok, full, <>} end.