Get lat, lng base at
I have
array of user addresses
array:5 [▼ 0 => array:2 [▼ "name" => " Admin" "address" => "111 Park AveFloor 4 New York, NY" ] 1 => array:2 [▼ "name" => "User A" "address" => "12 Main Street Cambridge, MA" ] 2 => array:2 [▼ "name" => "Apple HQ" "address" => "1 Infinite Loop Cupertino, California" ] 3 => array:2 [▼ "name" => "Google MA" "address" => "355 Main St Cambridge, MA" ] 4 => array:2 [▼ "name" => "site HQ" "address" => "300 Concord Road Billerica, MA " ] ] My goal
- grab lat and lng each address and build something like this
[ ["Admin", 18.3114513, -66.9219513, 0], ["User A", 25.3253982, 44.5503772, 1], ["Apple HQ", 33.0241101, 39.5865834, 2], ["Google MA", 43.9315743, 20.2366877, 3], ["site HQ", 32.683063, 35.27481, 4] ] so that I can display them on a Google Map.
I tried:
makes curl up
https://maps.googleapis.com/maps/api/geocode/json?address='.$address.'&key=*** $data = shell_exec('curl '.$url);
I got
this is the answer in response, after decoding it
$data = json_decode($data);
{#278 ▼ +"results": array:1 [▼ 0 => {#298 ▼ +"address_components": array:2 [▶] +"formatted_address": "PR-111, Puerto Rico" +"geometry": {#300 ▼ +"bounds": {#301 ▶} +"location": {#304 ▼ +"lat": 18.3114513 +"lng": -66.9219513 } +"location_type": "GEOMETRIC_CENTER" +"viewport": {#305 ▶} } +"place_id": "ChIJrdMXucS4AowRF4jHu2ji58U" +"types": array:1 [▶] } ] +"status": "OK" } As you can see, now I can access lat, lng by accessing
$location = $data->results[0]->geometry->location;
You could say if you can access it - why are you still asking this question?
It’s good that the lat, lng data that I get from the API response causes an incorrect Google map marker.
I'm not sure why / how this is wrong, but I 'm sure all my user addresses are in the USA, and here is my Google Map marker result. None of them are shown in the USA.
Google Map Result
Now I'm running out of ideas.
Any hints / hints / tips will mean a lot to me.
Update
Thanks to this answer
I can get lat, lng and map the correct markers on my google map now.
Final result
You must url your address before passing it in order to curl.
urlencode($address); Spaces in addresses, if not encoded, can cause unpredictable problems.
Here is an example of what is happening:
Without url encoding:
curl https://maps.googleapis.com/maps/api/geocode/json?address=111 Park AveFloor 4 New York, NY { "results" : [ { "address_components" : [ { "long_name" : "111", "short_name" : "PR-111", "types" : [ "route" ] }, { "long_name" : "Puerto Rico", "short_name" : "PR", "types" : [ "country", "political" ] } ], "formatted_address" : "PR-111, Puerto Rico", "geometry" : { "bounds" : { "northeast" : { "lat" : 18.4485405, "lng" : -66.6643718 }, "southwest" : { "lat" : 18.2420721, "lng" : -67.15701799999999 } }, "location" : { "lat" : 18.3114513, "lng" : -66.9219513 }, "location_type" : "GEOMETRIC_CENTER", "viewport" : { "northeast" : { "lat" : 18.4485405, "lng" : -66.6643718 }, "southwest" : { "lat" : 18.2420721, "lng" : -67.15701799999999 } } }, "place_id" : "ChIJrdMXucS4AowRF4jHu2ji58U", "types" : [ "route" ] } ], "status" : "OK" } With url encoding:
curl https://maps.googleapis.com/maps/api/geocode/json?address=111+Park+AveFloor+4+New+York%2C+NY { "results" : [ { "address_components" : [ { "long_name" : "111", "short_name" : "111", "types" : [ "street_number" ] }, { "long_name" : "Park Avenue", "short_name" : "Park Ave", "types" : [ "route" ] }, { "long_name" : "Midtown East", "short_name" : "Midtown East", "types" : [ "neighborhood", "political" ] }, { "long_name" : "Manhattan", "short_name" : "Manhattan", "types" : [ "sublocality_level_1", "sublocality", "political" ] }, { "long_name" : "New York", "short_name" : "New York", "types" : [ "locality", "political" ] }, { "long_name" : "New York County", "short_name" : "New York County", "types" : [ "administrative_area_level_2", "political" ] }, { "long_name" : "New York", "short_name" : "NY", "types" : [ "administrative_area_level_1", "political" ] }, { "long_name" : "United States", "short_name" : "US", "types" : [ "country", "political" ] }, { "long_name" : "10170", "short_name" : "10170", "types" : [ "postal_code" ] } ], "formatted_address" : "111 Park Ave, New York, NY 10170, USA", "geometry" : { "bounds" : { "northeast" : { "lat" : 40.7522096, "lng" : -73.9773591 }, "southwest" : { "lat" : 40.7521971, "lng" : -73.97736809999999 } }, "location" : { "lat" : 40.7521971, "lng" : -73.97736809999999 }... 
