Giter VIP home page Giter VIP logo

Comments (2)

tomickigrzegorz avatar tomickigrzegorz commented on June 19, 2024

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.

jasomdotnet avatar jasomdotnet commented on June 19, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.