google maps: search in specific countries - php

I have a form in my PHP application which searches for locations in europe: Germany, France, Poland, Czech Republic, Spain, Austria, Romania, Italy.
This is my base query:
$address = "https://maps.googleapis.com/maps/api/geocode/json?address={$input}";
If the users tries to find "mannheim" for example, it should return Mannheim, Germany.
Instead it returns a point in Pennsylvania, US. I tried adding this address component restriction: &components=administrative_area:EU, but this is not reliable because instead of finding Mannheim Germany, it points to a place 300 km away:
https://www.google.ro/maps/dir/50.5320001,6.6364339/Mannheim/#50.0388399,8.4546225,7.25z/data=!4m8!4m7!1m0!1m5!1m1!1s0x4797cc24518e3f45:0xb1e4fe7aa406e687!2m2!1d8.4660395!2d49.4874592?hl=en
If I append , Germany:
$address = "https://maps.googleapis.com/maps/api/geocode/json?address={$input}, Germany";
then the response is correct.
Is there a way to specify some countries for which the search should happen?
A last resort is to make a separate search for each country, but this would be really slow given the ~10-12 countries I search in.

restrict google search result via country restriction like this below:
var options = {
componentRestrictions: {country: 'DE'}
};
and place options parameter in Autocomplete or other function you are using.
var autocomplete = new google.maps.places.Autocomplete(input,options);

Looks like it's possible now: https://developers.google.com/maps/documentation/javascript/examples/places-autocomplete-multiple-countries#maps_places_autocomplete_multiple_countries-javascript
// Sets a listener on a given radio button. The radio buttons specify
// the countries used to restrict the autocomplete search.
function setupClickListener(id, countries) {
const radioButton = document.getElementById(id);
radioButton.addEventListener("click", () => {
autocomplete.setComponentRestrictions({ country: countries });
});
}
setupClickListener("changecountry-usa", "us");
setupClickListener("changecountry-usa-and-uot", [
"us",
"pr",
"vi",
"gu",
"mp",
]);

According to google docs, we can add up to 5 countries in component filter
https://developers.google.com/maps/documentation/places/web-service/autocomplete
A grouping of places to which you would like to restrict your results. Currently, you can use components to filter by up to 5 countries. Countries must be passed as a two character, ISO 3166-1 Alpha-2 compatible country code. For example: components=country:fr would restrict your results to places within France. Multiple countries must be passed as multiple country:XX filters, with the pipe character | as a separator. For example: components=country:us|country:pr|country:vi|country:gu|country:mp would restrict your results to places within the United States and its unincorporated organized territories.

Related

Sphinx Search : Is there a way to find results within few miles of a route i.e a Polyline and not polygon

I want to search records in Sphinx Search, which are within "X" miles from a "Polyline", records can be on either side of Polyline. I have set of geocode which forms a route.
$polylineLatLon = array("48.1390965,11.580255","48.2617271,11.6472244","48.3885849,11.5972817","48.5191226,11.5829265","48.6465704,11.516","48.773632,11.4611113","48.9037728,11.4703166","49.0096128,11.3544774","49.1298187,11.272155","49.2569017,11.2146807","49.3872571,11.2021279","49.5046949,11.2919927","49.6166182,11.3960946","49.7263312,11.5053892","49.8565686,11.51806","49.9767852,11.606766","50.0952959,11.6940558","50.2140534,11.7813241","50.3446984,11.7877614","50.4762554,11.8022454","50.6064391,11.791023","50.7318807,11.8482506","50.8638132,11.8523812","50.9891474,11.9091797","51.1166811,11.9561183","51.2372088,12.0594156","51.3430381,12.1846855","51.4732111,12.2154129","51.6025257,12.183516","51.7333531,12.2172689","51.8452442,12.3273146","51.9514704,12.4555349","52.03493,12.6191926","52.1380019,12.7586353","52.227931,12.913034","52.3074102,13.0885363","52.4209106,13.1959963","52.5143051,13.3479595","52.5234738,13.4115041");
I tried 2 approach and failed.
1st Approach:
$cl = new SphinxClient;
$cl->SetLimits(0, 20);
$cl->SetMatchMode(SPH_MATCH_EXTENDED);
foreach($polylineLatLon as $singPoly){
$singPolyExplode = explode(",",$singPoly);
$latRad = Deg2Rad($singPolyExplode[0]);
$LonRad = Deg2Rad($singPolyExplode[1]);
$cl->SetGeoAnchor('latitude', 'longitude', $latRad,$LonRad);
$cl->AddQuery("", $indexerName);
}
$result =$cl->runQueries();
The problem with this approach is it will fetch records nearby each Geocode. Not the complete route path. Also, calculating distance for each records with polyline Geocode will take a lot of time. Thus its not an optimized approach.
2nd Approach:
$cl = new SphinxClient;
$cl->SetLimits(0, 2000);
$cl->SetMatchMode(SPH_MATCH_EXTENDED);
$cl->SetSelect("*, CONTAINS(GEOPOLY2D(48.1390965,11.580255,48.2617271,11.6472244,48.3885849,11.5972817,48.5191226,11.5829265,48.6465704,11.516,48.773632,11.4611113,48.9037728,11.4703166,49.0096128,11.3544774,49.1298187,11.272155,49.2569017,11.2146807,49.3872571,11.2021279,49.5046949,11.2919927,49.6166182,11.3960946,49.7263312,11.5053892,49.8565686,11.51806,49.9767852,11.606766,50.0952959,11.6940558,50.2140534,11.7813241,50.3446984,11.7877614,50.4762554,11.8022454,50.6064391,11.791023,50.7318807,11.8482506,50.8638132,11.8523812,50.9891474,11.9091797,51.1166811,11.9561183,51.2372088,12.0594156,51.3430381,12.1846855,51.4732111,12.2154129,51.6025257,12.183516,51.7333531,12.2172689,51.8452442,12.3273146,51.9514704,12.4555349,52.03493,12.6191926,52.1380019,12.7586353,52.227931,12.913034,52.3074102,13.0885363,52.4209106,13.1959963,52.5143051,13.3479595,52.5234738,13.4115041), latitude_deg,longitude_deg) as inside");
$cl->setFilter("inside",array(1));
$result = $cl->Query("", $indexer);
The problem with this approach is that the records a fetched only within the polygon. However, I want records on both side of polyline within few miles away from route.
In the most general sense, need to 'buffer' the polyline, to turn it into a polygon :) Ie create a polygon that encompasses the area within X distance of the polyline.
Buffer is a well known GIS operation. ... I dont actully know of a any simple PHP libraries that will do it (Sphinx doesnt have one!)
Google also created a 'Route Boxer' algorithm, which is taking a polyline and turning into a series of non-overlapping rectangles. Sort of a simplified (and more approximate!) system. Looking up results for a series of boxes is simpler than full polygone queries, even though sphinx can do them.
There is PHP library: https://github.com/bazo/route-boxer

City name, County name returning in other languages not in English

When i search using below FQ API :
https://api.foursquare.com/v2/venues/search?ll=19.346523,-99.191292&query=gimn&oauth_token=Z1NASAZ5IEUSDJAJG1VMQO5YX410DJXUKBKAHXN0FYIB15BQ&v=20150401
In browser we got the City name as "Mexico City".
But, when i used this API call in file_get_contents or curl, city name is returning as "Ciudad de México" in spanish language.
I need City name in English, Pls provide me the steps to fix this issue.
Thanks,
Suhanya.M
It's defaulting to Spanish, as it's the most popular for that locale. You can specify locale=en for English:
https://developer.foursquare.com/overview/versioning
Here's what the new link would look like; note the additional parameter at the very end:
https://api.foursquare.com/v2/venues/search?ll=19.346523,-99.191292&query=gimn&oauth_token=Z1NASAZ5IEUSDJAJG1VMQO5YX410DJXUKBKAHXN0FYIB15BQ&v=20150401&locale=en

Calculate a boundary from set of geo locations

I have list of latitudes and longitudes.
These are acquired from reverse zip look up database and it's under a specific city (Dayton, OH).
39.721286 -84.133892
39.760000 -84.195900
39.757800 -84.177700
39.845339 -84.123287
...
I have a property database in mongodb and, each property has the location defined (lat/lng), when user enters "Dayton, OH", I'm grabbing Dayton's all the zip codes with relevant geo coordinates and what I want to do is to create a boundary and add 5 miles to it and search the property database which covers the lat/lng of dayton zip codes.
What's the best way to create a boundary (square/circle) to search a database within the range using this list of cordinates?
You can use the $near operator in MongoDB.
For example:
db.cities.find( { loc : { $near : { $geometry : { type : "Point" , coordinates : [ lon, lat] }, $maxDistance : 1/111.12 } } } )
The above query fiends everything near specific coordinates within a 1km radius (see $maxDistance).
For more information about the $near operator, take a look at the MongoDB Manual.
Found the solution, apparently the best approach is to calculate the border from application end using "convex hull" algorithm and then query.

Correct way to search multilingual location names i.e. Brazil or Brasil

What is the best way of returning results from a mysql database search where place names are entered in different languages? The case in question is the following:
Entries are made in a database with a location field. The location field uses google places autocomplete to attempt to ensure consistent and correctly spelled locations. However users can both enter and search the database for locations in multiple languages. An example problem case is that Sao Paulo, Brazil returns 4 results whereas Sao Paulo, Brasil returns 20 results. The correct behavior we need is that either spelling should return all 24 results.
The search query (using joomla syntax) is currently:
// Filter By buyer's destination //
$destination = $jinput->get('destination', '', 'string');
if($destination!=''){
$destination = $db->Quote('%' . $db->escape($destination, true) . '%');
$query->where('a.deliverydestination LIKE '.$destination.' AND a.status="Collecting Bids"');
$query->order('a.created DESC');
}

multi-array search query with delimited field

I have a array which store some country names
im doing a search module
and i wanna to find the db records whether contain the country names or not
but since db design problem , those record can own mutil counties and store the countries by delimiter "|"
below is a example
input: array("Cyprus","Austria") // note that the max input will be 300 country
db record country column: Albania|Andorra|Austria|Belarus|Belgium|Bosnia and Herzegovina|Bulgaria|Croatia|Cyprus|Czech Republic|Denmark|Estonia|European Union|Faroe Islands
So anyone can give a solution base on php for seaching?
First of all your DB design is not good. You should not be putting multiple delimited country names in one filed. You might wanna change it.
If you really want to work with the existing design, you can make use of explode and in_array functions of PHP as:
$db_rec = 'Albania|Andorra|Austria|Belarus|Belgium|Bosnia and Herzegovina';
$arr = explode('|',$db_rec);
$key = 'Albania';
if(in_array($key,$arr)) {
echo "$key found";
}

Categories