0

I'm working on an web service in php, the service can invoke from a client who ask some latitude and longitude the service via query get the address from a database and use the google api:

$geocode=file_get_contents("https://maps.google.com/maps/api/geocode/json?address=".$row."&components=country:IT&sensor=true");

After that the service scrap the contents and gets the latitude and longitude

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

the problem is the lat and lng are wrong but if I try the address from a browser i get che correct lat and lng.

For example this is what i get from "http://maps.google.com/maps/api/geocode/json?address=Via%20Corno%20di%20Cavento,%20Milan,%20Metropolita&components=country:IT&sensor=false%22"

{
   "results" : [
      {
         "address_components" : [
            {
               "long_name" : "Via Corno di Cavento",
               "short_name" : "Via Corno di Cavento",
               "types" : [ "route" ]
            },
            {
               "long_name" : "Milano",
               "short_name" : "Milano",
               "types" : [ "locality", "political" ]
            },
            {
               "long_name" : "Milan",
               "short_name" : "Milan",
               "types" : [ "administrative_area_level_3", "political" ]
            },
            {
               "long_name" : "Città Metropolitana di Milano",
               "short_name" : "MI",
               "types" : [ "administrative_area_level_2", "political" ]
            },
            {
               "long_name" : "Lombardia",
               "short_name" : "Lombardia",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "Italy",
               "short_name" : "IT",
               "types" : [ "country", "political" ]
            },
            {
               "long_name" : "20148",
               "short_name" : "20148",
               "types" : [ "postal_code" ]
            }
         ],
         "formatted_address" : "Via Corno di Cavento, 20148 Milano, Italy",
         "geometry" : {
            "bounds" : {
               "northeast" : {
                  "lat" : 45.4731517,
                  "lng" : 9.1267511
               },
               "southwest" : {
                  "lat" : 45.47025660000001,
                  "lng" : 9.1236354
               }
            },
            "location" : {
               "lat" : 45.471653,
               "lng" : 9.125879299999999
            },
            "location_type" : "GEOMETRIC_CENTER",
            "viewport" : {
               "northeast" : {
                  "lat" : 45.4731517,
                  "lng" : 9.1267511
               },
               "southwest" : {
                  "lat" : 45.47025660000001,
                  "lng" : 9.1236354
               }
            }
         },
         "partial_match" : true,
         "place_id" : "ChIJZwCky4_BhkcRuGIAMLrYfsY",
         "types" : [ "route" ]
      }
   ],
   "status" : "OK"
}

And this is what i got if i run that query from the php service:

"{
   "results" : [
         {
                  "address_components" : [ 
                    {
                                   "long_name" : "Italy",
                                   "short_name" : "IT",               
                                   "types" : [ "country", "political" ]            
                    }         ],         
                    "formatted_address" : "Italy",         
                    "geometry" : {
                                "bounds" : {               
                                "northeast" : {
                                                  "lat" : 47.092,                  
                                                  "lng" : 18.5205015               
                                              },               
                                "southwest" : {
                                                  "lat" : 35.4929201,                  
                                                  "lng" : 6.6267201               
                                              }            
                                },            
                                "location" : {
                                               "lat" : 41.87194,               
                                               "lng" : 12.56738            
                                             },            
                                "location_type" : "APPROXIMATE",            
                                "viewport" : {
                                               "northeast" : {
                                                                 "lat" : 47.092,                  
                                                                 "lng" : 18.5205015               
                                                             },               
                                                "southwest" : {                  
                                                                "lat" : 35.4929201,                  
                                                                "lng" : 6.6267201               
                                                              }            
                                            }         
                    },         
                    "partial_match" : true,         
                    "place_id" : "ChIJA9KNRIL-1BIRb15jJFz1LOI",         
                    "types" : [ "country", "political" ]      
        }   ],   
"status" : "OK"}"

Someone can help me?

geocodezip
  • 158,664
  • 13
  • 220
  • 245
Bestbug
  • 475
  • 4
  • 13
  • Are you [URL encoding the URL as specified in the documentation](https://developers.google.com/maps/web-services/overview#BuildingURLs) when you send it from PHP? – geocodezip Sep 05 '15 at 16:32
  • Hello, thanks to interesting i check the url format using urlencode: $row = str_replace (" ", "+", urlencode($row)); Is not enough? – Bestbug Sep 05 '15 at 16:59
  • Did you read the documentation provided? You haven't provided the complete URL sent from your code. Note that there are functions to URL encode strings... – geocodezip Sep 05 '15 at 17:17
  • Hello again! Thank you so much, when i was checking the url format i notice I have set a name of address array wrong, when i fix that the code work as i aspected. Have a nice day! :) – Bestbug Sep 05 '15 at 19:27

0 Answers0