CodeIgniter Forums
Get coordinates from address - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forumdisplay.php?fid=30)
+--- Thread: Get coordinates from address (/showthread.php?tid=87550)



Get coordinates from address - pippuccio76 - 05-02-2023

hi i try to get coordinates from openstreetmap
this is my code :
Code:
    public function getCoordinateFromAddress()
    {
     
   
        $address = 'piazza della signoria  ,5, firenze ';

        $geocode = file_get_contents('https://nominatim.openstreetmap.org/search?q='.$address.'&format=json&polygon=1&addressdetails=1');

        $output= json_decode($geocode);

        //print_r($output);
        //die();

        $lat = $output->results[0]->geometry->location->lat;
        $long = $output->results[0]->geometry->location->lng;

   
   }
but with this code i have :

Code:
file_get_contents(https://nominatim.openstreetmap.org/search?q=piazza della signoria ,5, firenze &format=json&polygon=1&addressdetails=1): Failed to open stream: HTTP request failed! HTTP/1.1 400 Bad Request
How can i solve ?


RE: Get coordinates from address - JustJohnQ - 05-03-2023

In Codeigniter 3 I made a helper (which I basically copied from the internet):
PHP Code:
<?php
function geocode($address){
    $address urlencode($address);

    $url "https://nominatim.openstreetmap.org/?addressdetails=1&q=$address&format=json&limit=1";

    $ch curl_init();
    curl_setopt($chCURLOPT_RETURNTRANSFERtrue);
    curl_setopt($chCURLOPT_URL,$url);
    curl_setopt($chCURLOPT_SSL_VERIFYPEERfalse);
    curl_setopt($chCURLOPT_HEADERfalse);
    curl_setopt($chCURLOPT_REFERER$url);
    curl_setopt($chCURLOPT_USERAGENT"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.86 Safari/537.36");

    $result curl_exec($ch);

    curl_close($ch);

    return json_decode($resulttrue);


Then I call it from a Controller:
PHP Code:
function testMe(){
    $this->load->helper('address');
    echo "<pre>";
    print_r(geocode("piazza della signoria  ,5, firenze"));
    

It is using CURL, so it may not be what you want, but it works for me.


RE: Get coordinates from address - pippuccio76 - 05-04-2023

(05-03-2023, 02:23 AM)JustJohnQ Wrote: In Codeigniter 3 I made a helper (which I basically copied from the internet):
PHP Code:
<?php
function geocode($address){
    $address urlencode($address);

    $url "https://nominatim.openstreetmap.org/?addressdetails=1&q=$address&format=json&limit=1";

    $ch curl_init();
    curl_setopt($chCURLOPT_RETURNTRANSFERtrue);
    curl_setopt($chCURLOPT_URL,$url);
    curl_setopt($chCURLOPT_SSL_VERIFYPEERfalse);
    curl_setopt($chCURLOPT_HEADERfalse);
    curl_setopt($chCURLOPT_REFERER$url);
    curl_setopt($chCURLOPT_USERAGENT"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.86 Safari/537.36");

    $result curl_exec($ch);

    curl_close($ch);

    return json_decode($resulttrue);


Then I call it from a Controller:
PHP Code:
function testMe(){
    $this->load->helper('address');
    echo "<pre>";
    print_r(geocode("piazza della signoria  ,5, firenze"));
    

It is using CURL, so it may not be what you want, but it works for me.


It works fine thank's.