I have
an 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
is to grab the lat
, and lng
of each address, and construct 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 I can plot them in Google Map.
I tried:
making a curl to
https://maps.googleapis.com/maps/api/geocode/json?address='.$address.'&key=***
$data = shell_exec('curl '.$url);
I got
this back as a response, after decode 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 the lat,lng by accessing
$location = $data->results[0]->geometry->location;
You might say, if you can access to it - why are you still asking this question ?
Well, that lat,lng data that I'm getting back from the API response produce wrong Google Map Marker.
I'm not sure why/how it is wrong, but I am sure that all my user addresses are in the US, and here is my Google Map Marker Result. None of them showing inside the US.
Google Map Result
I'm running out of ideas now.
Any hints / helps / suggestions will mean a lot to me.
Update
Thanks to this answer
I be able to get the lat,lng and plot correct markers on my Google Map now.