Welcome Guest, Not a member yet? Register   Sign In
Get coordinates from address
#1

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 ?
Reply
#2

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.
Reply
#3

(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.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB