【问题标题】:Get the lat,lng base on address根据地址获取 lat,lng
【发布时间】:2016-05-10 11:08:35
【问题描述】:

我有

用户地址数组

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 "
  ]
]

我的目标

就是抓取每个地址的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]
]

所以我可以在谷歌地图中绘制它们。


我试过了:

卷曲到

https://maps.googleapis.com/maps/api/geocode/json?address='.$address.'&key=***

$data = shell_exec('curl '.$url);


我明白了

解码后作为响应返回

$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"
}

如你所见,现在我可以通过访问来访问 lat,lng

$location = $data->results[0]->geometry->location;

你可能会说,如果你可以访问它——你为什么还要问这个问题?

嗯,我从 API 响应返回的 lat,lng 数据产生了错误的 Google Map Marker。

我不确定为什么/如何出错,但我确定我所有的用户地址都在美国,这是我的 Google 地图标记结果。他们都没有出现在美国境内。

谷歌地图结果


我的想法已经不多了。

任何提示/帮助/建议对我来说意义重大。


更新

感谢answer

我现在可以获取 lat,lng 并在我的 Google 地图上绘制正确的标记。


最终结果

【问题讨论】:

  • 您正在搜索一些通用地址。如果您在它们的末尾标记“美国”会发生什么?
  • 嗯,好电话,让我试试。
  • 不管实际问题是什么,this 都是一个结构良好的问题 (+1)。
  • 很高兴你在美国各地为那些不知道它在哪里的人放了一个盒子:)
  • 您也可以尝试添加邮政编码,并确保 $address 是 URL 编码的。在您的示例中,地理编码器 API 将格式化地址返回为波多黎各,因此它可能无法正确读取它。这是关于格式化地址的常见问题解答:developers.google.com/maps/faq#geocoder_queryformat

标签: javascript php jquery google-maps-api-3 google-maps-markers


【解决方案1】:

在将地址传递给 curl 之前,您应该对地址进行 url 编码。

urlencode($address);

地址中的空格如果不编码可能会导致不可预知的问题。

下面是一个例子:

不带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"
}

带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
            }...

【讨论】:

  • 你是对的!在我urlencode($address) 之后,它解决了我的问题。但是我可以问我的响应数组中有 26 个元素吗?我怎么知道哪个元素会给我准确的 lat,lng ?请指教。
  • 它返回与给定查询匹配的位置,可以是多个地址。如果您只想要给定查询的最佳建议,请使用第一个结果中的 lat/lng。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-07-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多