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
是抓住每个地址的lat和lng,然后建造这样的东西
[
["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);
$ data = shell_exec(“卷曲”。$ url);
I got
this back as a response, after decode it
这作为一种回应,在解码之后
$data = json_decode($data);
$ data = json_decode(元数据);
{#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
如您所见,现在我可以通过访问来访问lat,lng
$location = $data->results[0]->geometry->location;
位置=数据- >结果美元[0]- >几何- >位置;
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.
我从API响应中得到的lat,lng数据会产生错误的谷歌地图标记。
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.
我现在可以在谷歌地图上找到lat、lng和正确的标记。
Final Result
1 个解决方案
#1
2
You should url-encode your address before you pass it to curl.
你应该在将你的地址编码之前,将其编码为curl。
urlencode($address);
The spaces in the addresses if not encoded can cause unpredictable problems.
如果没有编码,地址中的空格会导致不可预测的问题。
Here is an example of what happens:
这里有一个发生的例子:
Without url encoding:
没有url编码:
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:
url编码:
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
}...
#1
2
You should url-encode your address before you pass it to curl.
你应该在将你的地址编码之前,将其编码为curl。
urlencode($address);
The spaces in the addresses if not encoded can cause unpredictable problems.
如果没有编码,地址中的空格会导致不可预测的问题。
Here is an example of what happens:
这里有一个发生的例子:
Without url encoding:
没有url编码:
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:
url编码:
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
}...