Comments (2)
Hi,
It depends on what api you are using?
In my example, nominatim is used so I have to build the query properly first, take a look documentation and then change them in this line and also this line.
I warn you, nominatim returns, more than we would like ;)
Even this working example of mine does not only return cities, but also the names of villages, neighborhoods, etc. ;)
See how geojson is returned https://nominatim.openstreetmap.org/search?format=geojson&limit=5&addressdetails=1&city=sydney
As you can see, these are not just returned cities. It may happen that two identical results will appear in the search results, e.g .:
- Sydney, Australia
- Sydney, Australia
And the question is, what will you do then :)
You can set everything up in line 69 and allow only those results that are suitable for you.
It is not without reason that the website https://www.openstreetmap.org/search?query=sydney#map=10/-33.7697/150.8024 displays not only the city and country but the entire name.
In summary, you can only display the city and country, but you have to correctly ask api nominatim how to properly use the conditions to display the records.
from leaflet.autocomplete.
My code for somebody with same request as @shykhamza
function googleGeocoder(string $address, string $lang): array {
$json['features'] = [];
$url = 'https://maps.googleapis.com/maps/api/geocode/json?' . http_build_query(['address' => $address, 'language' => $lang, 'sensor' => 'false', 'key' => MAP_GOOGLE_APIKEY]);
$request = json_decode(file_get_contents($url));
if ($request->status === 'OK') {
foreach ($request->results as $result) {
$point = null;
if (!empty($result->address_components)) {
$address_components = [];
foreach ($result->address_components as $address_component) {
if (!in_array('postal_code', $address_component->types)) {
$address_components[] = $address_component->long_name;
}
}
$locality = htmlspecialchars(implode(', ', array_unique($address_components)), ENT_QUOTES, 'UTF-8');
$point['properties']['display_name'] = $locality;
}
if (!empty($result->geometry->location)) {
$point['geometry']['coordinates'] = [$result->geometry->location->lng, $result->geometry->location->lat];
}
if (!empty($point)) {
$json['features'][] = $point;
}
}
} elseif ($request->status === 'ZERO_RESULTS') {
//$json['features'] = [];
} else {
$log = sprintf('Error within Google Geocoder API with status %1$s for request %2$s.', $request->status, str_replace(MAP_GOOGLE_APIKEY, 'key', $url));
if (!empty($request->error_message)) {
$log .= sprintf(' Error message: %s', $request->error_message);
}
Log::watchdog('error', $log);
}
return $json;
}
from leaflet.autocomplete.
Related Issues (8)
- An issue that's not really an issue... HOT 2
- There is a typo "maxZomm" HOT 1
- How to inicialize clearButton (x) when the field comes with default value HOT 1
- Not reset settings
- Violating Nominatim Usage Policy
- Search string pulls back all items regardless of whether there is a character match when using flat file HOT 2
- Search for a polygon and insert a bar in the center of the map HOT 10
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from leaflet.autocomplete.