我想使用地理编码从给定的纬度和经度检索地理地址,
使用网站上的给定示例
http://maps.googleapis.com/maps/api/geocode/json?latlng=40.714224,-73.961452&sensor=true
在这个例子中我只需要城市名称纽约和国家名称即美国。但问题是不止一次出现
这些词为long_name。
我的问题是如何在 php 中解析此 json/xml 以获取城市名称和国家/地区。
$url= file_get_contents('http://maps.googleapis.com/maps/api/geocode/xml?latlng=40.714224,-73.961452&sensor=true');
$xml = simplexml_load_string($url);
$tracks = $xml->result;
$temp; $city; $country;
foreach($tracks as $key)
{
foreach($key->address_component as $val)
{
$temp= $val->long_name;
if($val->type=="locality")
{
$city=$temp;
}else if($val->type=="country")
{
$country=$temp;
}
}
}
echo $country."<br />"; echo $city;
<?xml version="1.0" encoding="UTF-8"?>
<GeocodeResponse>
<status>OK</status>
<result>
<type>street_address</type>
<formatted_address>277 Bedford Avenue, Brooklyn, New York 11211, USA</formatted_address>
<address_component>
<long_name>277</long_name>
<short_name>277</short_name>
<type>street_number</type>
</address_component>
<address_component>
<long_name>Bedford Avenue</long_name>
<short_name>Bedford Ave</short_name>
<type>route</type>
</address_component>
<address_component>
<long_name>Williamsburg</long_name>
<short_name>Williamsburg</short_name>
<type>neighborhood</type>
<type>political</type>
</address_component>
<address_component>
<long_name>Brooklyn</long_name>
<short_name>Brooklyn</short_name>
<type>sublocality</type>
<type>political</type>
</address_component>
<address_component>
<long_name>Kings</long_name>
<short_name>Kings</short_name>
<type>administrative_area_level_2</type>
<type>political</type>
</address_component>
<address_component>
<long_name>New York</long_name>
<short_name>NY</short_name>
<type>administrative_area_level_1</type>
<type>political</type>
</address_component>
<address_component>
<long_name>USA</long_name>
<short_name>US</short_name>
<type>country</type>
<type>political</type>
</address_component>
<address_component>
<long_name>11211</long_name>
<short_name>11211</short_name>
<type>postal_code</type>
</address_component>
<geometry>
<location>
<lat>40.7142320</lat>
<lng>-73.9612889</lng>
</location>
<location_type>ROOFTOP</location_type>
<viewport>
<southwest>
<lat>40.7128830</lat>
<lng>-73.9626379</lng>
</southwest>
<northeast>
<lat>40.7155810</lat>
<lng>-73.9599399</lng>
</northeast>
</viewport>
</geometry>
</result>
<result>
<type>bus_station</type>
<type>transit_station</type>
<type>establishment</type>
<formatted_address>Grand St/Bedford Av, Brooklyn, New York 11211, USA</formatted_address>
<address_component>
<long_name>Grand St/Bedford Av</long_name>
<short_name>Grand St/Bedford Av</short_name>
<type>bus_station</type>
<type>transit_station</type>
<type>establishment</type>
</address_component>
<address_component>
<long_name>Williamsburg</long_name>
<short_name>Williamsburg</short_name>
<type>neighborhood</type>
<type>political</type>
</address_component>
<address_component>
<long_name>Brooklyn</long_name>
<short_name>Brooklyn</short_name>
<type>sublocality</type>
<type>political</type>
</address_component>
<address_component>
<long_name>Kings</long_name>
<short_name>Kings</short_name>
<type>administrative_area_level_2</type>
<type>political</type>
</address_component>
<address_component>
<long_name>New York</long_name>
<short_name>NY</short_name>
<type>administrative_area_level_1</type>
<type>political</type>
</address_component>
<address_component>
<long_name>USA</long_name>
<short_name>US</short_name>
<type>country</type>
<type>political</type>
</address_component>
<address_component>
<long_name>11211</long_name>
<short_name>11211</short_name>
<type>postal_code</type>
</address_component>
<geometry>
<location>
<lat>40.7143210</lat>
<lng>-73.9611510</lng>
</location>
<location_type>APPROXIMATE</location_type>
<viewport>
<southwest>
<lat>40.7129720</lat>
<lng>-73.9625000</lng>
</southwest>
<northeast>
<lat>40.7156700</lat>
<lng>-73.9598020</lng>
</northeast>
</viewport>
</geometry>
</result>
<result>
<type>bus_station</type>
<type>transit_station</type>
<type>establishment</type>
<formatted_address>Grand St/Bedford Av, Brooklyn, New York 11249, USA</formatted_address>
<address_component>
<long_name>Grand St/Bedford Av</long_name>
<short_name>Grand St/Bedford Av</short_name>
<type>bus_station</type>
<type>transit_station</type>
<type>establishment</type>
</address_component>
<address_component>
<long_name>Williamsburg</long_name>
<short_name>Williamsburg</short_name>
<type>neighborhood</type>
<type>political</type>
</address_component>
<address_component>
<long_name>Brooklyn</long_name>
<short_name>Brooklyn</short_name>
<type>sublocality</type>
<type>political</type>
</address_component>
<address_component>
<long_name>Kings</long_name>
<short_name>Kings</short_name>
<type>administrative_area_level_2</type>
<type>political</type>
</address_component>
<address_component>
<long_name>New York</long_name>
<short_name>NY</short_name>
<type>administrative_area_level_1</type>
<type>political</type>
</address_component>
<address_component>
<long_name>USA</long_name>
<short_name>US</short_name>
<type>country</type>
<type>political</type>
</address_component>
<address_component>
<long_name>11249</long_name>
<short_name>11249</short_name>
<type>postal_code</type>
</address_component>
<geometry>
<location>
<lat>40.7146840</lat>
<lng>-73.9615630</lng>
</location>
<location_type>APPROXIMATE</location_type>
<viewport>
<southwest>
<lat>40.7133350</lat>
<lng>-73.9629120</lng>
</southwest>
<northeast>
<lat>40.7160330</lat>
<lng>-73.9602140</lng>
</northeast>
</viewport>
</geometry>
</result>
<result>
<type>bus_station</type>
<type>transit_station</type>
<type>establishment</type>
<formatted_address>Bedford Av/Grand St, Brooklyn, New York 11211, USA</formatted_address>
<address_component>
<long_name>Bedford Av/Grand St</long_name>
<short_name>Bedford Av/Grand St</short_name>
<type>bus_station</type>
<type>transit_station</type>
<type>establishment</type>
</address_component>
<address_component>
<long_name>Williamsburg</long_name>
<short_name>Williamsburg</short_name>
<type>neighborhood</type>
<type>political</type>
</address_component>
<address_component>
<long_name>Brooklyn</long_name>
<short_name>Brooklyn</short_name>
<type>sublocality</type>
<type>political</type>
</address_component>
<address_component>
<long_name>Kings</long_name>
<short_name>Kings</short_name>
<type>administrative_area_level_2</type>
<type>political</type>
</address_component>
<address_component>
<long_name>New York</long_name>
<short_name>NY</short_name>
<type>administrative_area_level_1</type>
<type>political</type>
</address_component>
<address_component>
<long_name>USA</long_name>
<short_name>US</short_name>
<type>country</type>
<type>political</type>
</address_component>
<address_component>
<long_name>11211</long_name>
<short_name>11211</short_name>
<type>postal_code</type>
</address_component>
<geometry>
<location>
<lat>40.7147100</lat>
<lng>-73.9609990</lng>
</location>
<location_type>APPROXIMATE</location_type>
<viewport>
<southwest>
<lat>40.7133610</lat>
<lng>-73.9623480</lng>
</southwest>
<northeast>
<lat>40.7160590</lat>
<lng>-73.9596500</lng>
</northeast>
</viewport>
</geometry>
</result>
<result>
<type>postal_code</type>
<formatted_address>Brooklyn, New York 11211, USA</formatted_address>
<address_component>
<long_name>11211</long_name>
<short_name>11211</short_name>
<type>postal_code</type>
</address_component>
<address_component>
<long_name>Brooklyn</long_name>
<short_name>Brooklyn</short_name>
<type>sublocality</type>
<type>political</type>
</address_component>
<address_component>
<long_name>New York City</long_name>
<short_name>New York City</short_name>
<type>locality</type>
<type>political</type>
</address_component>
<address_component>
<long_name>New York</long_name>
<short_name>NY</short_name>
<type>administrative_area_level_1</type>
<type>political</type>
</address_component>
<address_component>
<long_name>USA</long_name>
<short_name>US</short_name>
<type>country</type>
<type>political</type>
</address_component>
<geometry>
<location>
<lat>40.7093358</lat>
<lng>-73.9565551</lng>
</location>
<location_type>APPROXIMATE</location_type>
<viewport>
<southwest>
<lat>40.7008331</lat>
<lng>-73.9644697</lng>
</southwest>
<northeast>
<lat>40.7280089</lat>
<lng>-73.9207299</lng>
</northeast>
</viewport>
<bounds>
<southwest>
<lat>40.7008331</lat>
<lng>-73.9644697</lng>
</southwest>
<northeast>
<lat>40.7280089</lat>
<lng>-73.9207299</lng>
</northeast>
</bounds>
</geometry>
</result>
<result>
<type>neighborhood</type>
<type>political</type>
<formatted_address>Williamsburg, Brooklyn, New York, USA</formatted_address>
<address_component>
<long_name>Williamsburg</long_name>
<short_name>Williamsburg</short_name>
<type>neighborhood</type>
<type>political</type>
</address_component>
<address_component>
<long_name>Brooklyn</long_name>
<short_name>Brooklyn</short_name>
<type>sublocality</type>
<type>political</type>
</address_component>
<address_component>
<long_name>New York City</long_name>
<short_name>New York City</short_name>
<type>locality</type>
<type>political</type>
</address_component>
<address_component>
<long_name>Kings</long_name>
<short_name>Kings</short_name>
<type>administrative_area_level_2</type>
<type>political</type>
</address_component>
<address_component>
<long_name>New York</long_name>
<short_name>NY</short_name>
<type>administrative_area_level_1</type>
<type>political</type>
</address_component>
<address_component>
<long_name>USA</long_name>
<short_name>US</short_name>
<type>country</type>
<type>political</type>
</address_component>
<geometry>
<location>
<lat>40.7064461</lat>
<lng>-73.9536163</lng>
</location>
<location_type>APPROXIMATE</location_type>
<viewport>
<southwest>
<lat>40.6979329</lat>
<lng>-73.9698450</lng>
</southwest>
<northeast>
<lat>40.7251773</lat>
<lng>-73.9364980</lng>
</northeast>
</viewport>
<bounds>
<southwest>
<lat>40.6979329</lat>
<lng>-73.9698450</lng>
</southwest>
<northeast>
<lat>40.7251773</lat>
<lng>-73.9364980</lng>
</northeast>
</bounds>
</geometry>
</result>
<result>
<type>administrative_area_level_2</type>
<type>political</type>
<formatted_address>Kings, New York, USA</formatted_address>
<address_component>
<long_name>Kings</long_name>
<short_name>Kings</short_name>
<type>administrative_area_level_2</type>
<type>political</type>
</address_component>
<address_component>
<long_name>New York</long_name>
<short_name>NY</short_name>
<type>administrative_area_level_1</type>
<type>political</type>
</address_component>
<address_component>
<long_name>USA</long_name>
<short_name>US</short_name>
<type>country</type>
<type>political</type>
</address_component>
<geometry>
<location>
<lat>40.6528762</lat>
<lng>-73.9594940</lng>
</location>
<location_type>APPROXIMATE</location_type>
<viewport>
<southwest>
<lat>40.5695637</lat>
<lng>-74.0429503</lng>
</southwest>
<northeast>
<lat>40.7394460</lat>
<lng>-73.8333651</lng>
</northeast>
</viewport>
<bounds>
<southwest>
<lat>40.5695637</lat>
<lng>-74.0429503</lng>
</southwest>
<northeast>
<lat>40.7394460</lat>
<lng>-73.8333651</lng>
</northeast>
</bounds>
</geometry>
</result>
<result>
<type>sublocality</type>
<type>political</type>
<formatted_address>Brooklyn, New York, USA</formatted_address>
<address_component>
<long_name>Brooklyn</long_name>
<short_name>Brooklyn</short_name>
<type>sublocality</type>
<type>political</type>
</address_component>
<address_component>
<long_name>New York City</long_name>
<short_name>New York City</short_name>
<type>locality</type>
<type>political</type>
</address_component>
<address_component>
<long_name>Kings</long_name>
<short_name>Kings</short_name>
<type>administrative_area_level_2</type>
<type>political</type>
</address_component>
<address_component>
<long_name>New York</long_name>
<short_name>NY</short_name>
<type>administrative_area_level_1</type>
<type>political</type>
</address_component>
<address_component>
<long_name>USA</long_name>
<short_name>US</short_name>
<type>country</type>
<type>political</type>
</address_component>
<geometry>
<location>
<lat>40.6500000</lat>
<lng>-73.9500000</lng>
</location>
<location_type>APPROXIMATE</location_type>
<viewport>
<southwest>
<lat>40.5510420</lat>
<lng>-74.0566300</lng>
</southwest>
<northeast>
<lat>40.7394460</lat>
<lng>-73.8333651</lng>
</northeast>
</viewport>
<bounds>
<southwest>
<lat>40.5510420</lat>
<lng>-74.0566300</lng>
</southwest>
<northeast>
<lat>40.7394460</lat>
<lng>-73.8333651</lng>
</northeast>
</bounds>
</geometry>
</result>
<result>
<type>locality</type>
<type>political</type>
<formatted_address>New York City, New York, USA</formatted_address>
<address_component>
<long_name>New York City</long_name>
<short_name>New York City</short_name>
<type>locality</type>
<type>political</type>
</address_component>
<address_component>
<long_name>New York</long_name>
<short_name>NY</short_name>
<type>administrative_area_level_1</type>
<type>political</type>
</address_component>
<address_component>
<long_name>USA</long_name>
<short_name>US</short_name>
<type>country</type>
<type>political</type>
</address_component>
<geometry>
<location>
<lat>40.7143528</lat>
<lng>-74.0059731</lng>
</location>
<location_type>APPROXIMATE</location_type>
<viewport>
<southwest>
<lat>40.4960060</lat>
<lng>-74.2557349</lng>
</southwest>
<northeast>
<lat>40.9152556</lat>
<lng>-73.7002721</lng>
</northeast>
</viewport>
<bounds>
<southwest>
<lat>40.4959961</lat>
<lng>-74.2590879</lng>
</southwest>
<northeast>
<lat>40.9152556</lat>
<lng>-73.7002721</lng>
</northeast>
</bounds>
</geometry>
</result>
<result>
<type>political</type>
<formatted_address>USA, ニュー・ヨーク=ノーザン・ニュー・ジャージー=ロング・アイランド</formatted_address>
<address_component>
<long_name>ニュー・ヨーク=ノーザン・ニュー・ジャージー=ロング・アイランド</long_name>
<short_name>ニュー・ヨーク=ノーザン・ニュー・ジャージー=ロング・アイランド</short_name>
<type>political</type>
</address_component>
<address_component>
<long_name>USA</long_name>
<short_name>US</short_name>
<type>country</type>
<type>political</type>
</address_component>
<geometry>
<location>
<lat>40.9590293</lat>
<lng>-74.0300122</lng>
</location>
<location_type>APPROXIMATE</location_type>
<viewport>
<southwest>
<lat>39.4985330</lat>
<lng>-75.3585939</lng>
</southwest>
<northeast>
<lat>41.6018065</lat>
<lng>-71.8562140</lng>
</northeast>
</viewport>
<bounds>
<southwest>
<lat>39.4985330</lat>
<lng>-75.3585939</lng>
</southwest>
<northeast>
<lat>41.6018065</lat>
<lng>-71.8562140</lng>
</northeast>
</bounds>
</geometry>
</result>
<result>
<type>administrative_area_level_1</type>
<type>political</type>
<formatted_address>New York, USA</formatted_address>
<address_component>
<long_name>New York</long_name>
<short_name>NY</short_name>
<type>administrative_area_level_1</type>
<type>political</type>
</address_component>
<address_component>
<long_name>USA</long_name>
<short_name>US</short_name>
<type>country</type>
<type>political</type>
</address_component>
<geometry>
<location>
<lat>43.2994285</lat>
<lng>-74.2179326</lng>
</location>
<location_type>APPROXIMATE</location_type>
<viewport>
<southwest>
<lat>40.4960191</lat>
<lng>-79.7621438</lng>
</southwest>
<northeast>
<lat>45.0156269</lat>
<lng>-71.8562643</lng>
</northeast>
</viewport>
<bounds>
<southwest>
<lat>40.4959961</lat>
<lng>-79.7621438</lng>
</southwest>
<northeast>
<lat>45.0158650</lat>
<lng>-71.8562643</lng>
</northeast>
</bounds>
</geometry>
</result>
<result>
<type>country</type>
<type>political</type>
<formatted_address>USA</formatted_address>
<address_component>
<long_name>USA</long_name>
<short_name>US</short_name>
<type>country</type>
<type>political</type>
</address_component>
<geometry>
<location>
<lat>37.0902400</lat>
<lng>-95.7128910</lng>
</location>
<location_type>APPROXIMATE</location_type>
<viewport>
<southwest>
<lat>25.8200000</lat>
<lng>-124.3900000</lng>
</southwest>
<northeast>
<lat>49.3800000</lat>
<lng>-66.9400000</lng>
</northeast>
</viewport>
<bounds>
<southwest>
<lat>-90.0000000</lat>
<lng>-180.0000000</lng>
</southwest>
<northeast>
<lat>90.0000000</lat>
<lng>180.0000000</lng>
</northeast>
</bounds>
</geometry>
</result>
</GeocodeResponse>
最佳答案
您可以检查 google 响应 (json) 中的 3 个变量以找到当前的城市名称。因此有 3 种不同的名称解释:
Country
|
AdministrativeArea
|
|_AdministrativeAreaName (cityname)
|
|_Locality_LocalityName (cityname)
|
|_SubAdministrativeArea
|
|_Locality_LocalityName (cityname)
if ($this->json->Placemark[0]->AddressDetails->Country->AdministrativeArea->Locality->LocalityName != NULL) {
$code['cityname'] = $this->json->Placemark[0]->AddressDetails->Country->AdministrativeArea->Locality->LocalityName;
} elseif ($this->json->Placemark[0]->AddressDetails->Country->AdministrativeArea->SubAdministrativeArea->Locality->LocalityName != NULL) {
$code['cityname'] = $this->json->Placemark[0]->AddressDetails->Country->AdministrativeArea->SubAdministrativeArea->Locality->LocalityName;
}
如果您有多个地标,则需要遍历所有数组。
"results" : [
{
"address_components" : [
{
"long_name" : "277",
"short_name" : "277",
"types" : [ "street_number" ]
},
{
"long_name" : "Bedford Avenue",
"short_name" : "Bedford Ave",
"types" : [ "route" ]
},
{
"long_name" : "Williamsburg",
"short_name" : "Williamsburg",
"types" : [ "neighborhood", "political" ]
},
{
"long_name" : "Brooklyn",
"short_name" : "Brooklyn",
"types" : [ "sublocality", "political" ]
},
{
"long_name" : "Kings",
"short_name" : "Kings",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "New York",
"short_name" : "NY",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "USA",
"short_name" : "US",
"types" : [ "country", "political" ]
},
{
"long_name" : "11211",
"short_name" : "11211",
"types" : [ "postal_code" ]
}
],
"formatted_address" : "277 Bedford Avenue, Brooklyn, New York 11211, USA",
"geometry" : {
"location" : {
"lat" : 40.714232,
"lng" : -73.9612889
},
"location_type" : "ROOFTOP",
"viewport" : {
"northeast" : {
"lat" : 40.7155809802915,
"lng" : -73.9599399197085
},
"southwest" : {
"lat" : 40.7128830197085,
"lng" : -73.96263788029151
}
}
},
"types" : [ "street_address" ]
},
{
"address_components" : [
{
"long_name" : "Grand St/Bedford Av",
"short_name" : "Grand St/Bedford Av",
"types" : [ "bus_station", "transit_station", "establishment" ]
},
{
"long_name" : "Williamsburg",
"short_name" : "Williamsburg",
"types" : [ "neighborhood", "political" ]
},
{
"long_name" : "Brooklyn",
"short_name" : "Brooklyn",
"types" : [ "sublocality", "political" ]
},
{
"long_name" : "Kings",
"short_name" : "Kings",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "New York",
"short_name" : "NY",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "USA",
"short_name" : "US",
"types" : [ "country", "political" ]
},
{
"long_name" : "11211",
"short_name" : "11211",
"types" : [ "postal_code" ]
}
],
"formatted_address" : "Grand St/Bedford Av, Brooklyn, New York 11211, USA",
"geometry" : {
"location" : {
"lat" : 40.714321,
"lng" : -73.961151
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : 40.71566998029149,
"lng" : -73.95980201970849
},
"southwest" : {
"lat" : 40.7129720197085,
"lng" : -73.96249998029151
}
}
},
"types" : [ "bus_station", "transit_station", "establishment" ]
},
{
"address_components" : [
{
"long_name" : "Grand St/Bedford Av",
"short_name" : "Grand St/Bedford Av",
"types" : [ "bus_station", "transit_station", "establishment" ]
},
{
"long_name" : "Williamsburg",
"short_name" : "Williamsburg",
"types" : [ "neighborhood", "political" ]
},
{
"long_name" : "Brooklyn",
"short_name" : "Brooklyn",
"types" : [ "sublocality", "political" ]
},
{
"long_name" : "Kings",
"short_name" : "Kings",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "New York",
"short_name" : "NY",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "USA",
"short_name" : "US",
"types" : [ "country", "political" ]
},
{
"long_name" : "11249",
"short_name" : "11249",
"types" : [ "postal_code" ]
}
],
"formatted_address" : "Grand St/Bedford Av, Brooklyn, New York 11249, USA",
"geometry" : {
"location" : {
"lat" : 40.714684,
"lng" : -73.961563
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : 40.71603298029149,
"lng" : -73.9602140197085
},
"southwest" : {
"lat" : 40.7133350197085,
"lng" : -73.96291198029151
}
}
},
"types" : [ "bus_station", "transit_station", "establishment" ]
},
{
"address_components" : [
{
"long_name" : "Bedford Av/Grand St",
"short_name" : "Bedford Av/Grand St",
"types" : [ "bus_station", "transit_station", "establishment" ]
},
{
"long_name" : "Williamsburg",
"short_name" : "Williamsburg",
"types" : [ "neighborhood", "political" ]
},
{
"long_name" : "Brooklyn",
"short_name" : "Brooklyn",
"types" : [ "sublocality", "political" ]
},
{
"long_name" : "Kings",
"short_name" : "Kings",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "New York",
"short_name" : "NY",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "USA",
"short_name" : "US",
"types" : [ "country", "political" ]
},
{
"long_name" : "11211",
"short_name" : "11211",
"types" : [ "postal_code" ]
}
],
"formatted_address" : "Bedford Av/Grand St, Brooklyn, New York 11211, USA",
"geometry" : {
"location" : {
"lat" : 40.71471,
"lng" : -73.960999
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : 40.7160589802915,
"lng" : -73.95965001970849
},
"southwest" : {
"lat" : 40.7133610197085,
"lng" : -73.9623479802915
}
}
},
"types" : [ "bus_station", "transit_station", "establishment" ]
},
{
"address_components" : [
{
"long_name" : "11211",
"short_name" : "11211",
"types" : [ "postal_code" ]
},
{
"long_name" : "Brooklyn",
"short_name" : "Brooklyn",
"types" : [ "sublocality", "political" ]
},
{
"long_name" : "New York City",
"short_name" : "New York City",
"types" : [ "locality", "political" ]
},
{
"long_name" : "New York",
"short_name" : "NY",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "USA",
"short_name" : "US",
"types" : [ "country", "political" ]
}
],
"formatted_address" : "Brooklyn, New York 11211, USA",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 40.7280089,
"lng" : -73.9207299
},
"southwest" : {
"lat" : 40.7008331,
"lng" : -73.9644697
}
},
"location" : {
"lat" : 40.7093358,
"lng" : -73.9565551
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : 40.7280089,
"lng" : -73.9207299
},
"southwest" : {
"lat" : 40.7008331,
"lng" : -73.9644697
}
}
},
"types" : [ "postal_code" ]
},
{
"address_components" : [
{
"long_name" : "Williamsburg",
"short_name" : "Williamsburg",
"types" : [ "neighborhood", "political" ]
},
{
"long_name" : "Brooklyn",
"short_name" : "Brooklyn",
"types" : [ "sublocality", "political" ]
},
{
"long_name" : "New York City",
"short_name" : "New York City",
"types" : [ "locality", "political" ]
},
{
"long_name" : "Kings",
"short_name" : "Kings",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "New York",
"short_name" : "NY",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "USA",
"short_name" : "US",
"types" : [ "country", "political" ]
}
],
"formatted_address" : "Williamsburg, Brooklyn, New York, USA",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 40.7251773,
"lng" : -73.936498
},
"southwest" : {
"lat" : 40.6979329,
"lng" : -73.96984499999999
}
},
"location" : {
"lat" : 40.7064461,
"lng" : -73.95361629999999
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : 40.7251773,
"lng" : -73.936498
},
"southwest" : {
"lat" : 40.6979329,
"lng" : -73.96984499999999
}
}
},
"types" : [ "neighborhood", "political" ]
},
{
"address_components" : [
{
"long_name" : "Kings",
"short_name" : "Kings",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "New York",
"short_name" : "NY",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "USA",
"short_name" : "US",
"types" : [ "country", "political" ]
}
],
"formatted_address" : "Kings, New York, USA",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 40.739446,
"lng" : -73.83336509999999
},
"southwest" : {
"lat" : 40.5695637,
"lng" : -74.0429503
}
},
"location" : {
"lat" : 40.6528762,
"lng" : -73.95949399999999
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : 40.739446,
"lng" : -73.83336509999999
},
"southwest" : {
"lat" : 40.5695637,
"lng" : -74.0429503
}
}
},
"types" : [ "administrative_area_level_2", "political" ]
},
{
"address_components" : [
{
"long_name" : "Brooklyn",
"short_name" : "Brooklyn",
"types" : [ "sublocality", "political" ]
},
{
"long_name" : "New York City",
"short_name" : "New York City",
"types" : [ "locality", "political" ]
},
{
"long_name" : "Kings",
"short_name" : "Kings",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "New York",
"short_name" : "NY",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "USA",
"short_name" : "US",
"types" : [ "country", "political" ]
}
],
"formatted_address" : "Brooklyn, New York, USA",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 40.739446,
"lng" : -73.83336509999999
},
"southwest" : {
"lat" : 40.551042,
"lng" : -74.05663
}
},
"location" : {
"lat" : 40.65,
"lng" : -73.95
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : 40.739446,
"lng" : -73.83336509999999
},
"southwest" : {
"lat" : 40.551042,
"lng" : -74.05663
}
}
},
"types" : [ "sublocality", "political" ]
},
{
"address_components" : [
{
"long_name" : "New York City",
"short_name" : "New York City",
"types" : [ "locality", "political" ]
},
{
"long_name" : "New York",
"short_name" : "NY",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "USA",
"short_name" : "US",
"types" : [ "country", "political" ]
}
],
"formatted_address" : "New York City, New York, USA",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 40.91525559999999,
"lng" : -73.70027209999999
},
"southwest" : {
"lat" : 40.4959961,
"lng" : -74.2590879
}
},
"location" : {
"lat" : 40.7143528,
"lng" : -74.00597309999999
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : 40.91525559999999,
"lng" : -73.70027209999999
},
"southwest" : {
"lat" : 40.496006,
"lng" : -74.25573489999999
}
}
},
"types" : [ "locality", "political" ]
},
{
"address_components" : [
{
"long_name" : "ニュー・ヨーク=ノーザン・ニュー・ジャージー=ロング・アイランド",
"short_name" : "ニュー・ヨーク=ノーザン・ニュー・ジャージー=ロング・アイランド",
"types" : [ "political" ]
},
{
"long_name" : "USA",
"short_name" : "US",
"types" : [ "country", "political" ]
}
],
"formatted_address" : "USA, ニュー・ヨーク=ノーザン・ニュー・ジャージー=ロング・アイランド",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 41.6018065,
"lng" : -71.85621399999999
},
"southwest" : {
"lat" : 39.49853299999999,
"lng" : -75.3585939
}
},
"location" : {
"lat" : 40.9590293,
"lng" : -74.0300122
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : 41.6018065,
"lng" : -71.85621399999999
},
"southwest" : {
"lat" : 39.49853299999999,
"lng" : -75.3585939
}
}
},
"types" : [ "political" ]
},
{
"address_components" : [
{
"long_name" : "New York",
"short_name" : "NY",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "USA",
"short_name" : "US",
"types" : [ "country", "political" ]
}
],
"formatted_address" : "New York, USA",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 45.015865,
"lng" : -71.85626429999999
},
"southwest" : {
"lat" : 40.4959961,
"lng" : -79.76214379999999
}
},
"location" : {
"lat" : 43.2994285,
"lng" : -74.21793260000001
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : 45.0156269,
"lng" : -71.85626429999999
},
"southwest" : {
"lat" : 40.4960191,
"lng" : -79.76214379999999
}
}
},
"types" : [ "administrative_area_level_1", "political" ]
},
{
"address_components" : [
{
"long_name" : "USA",
"short_name" : "US",
"types" : [ "country", "political" ]
}
],
"formatted_address" : "USA",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 90,
"lng" : 180
},
"southwest" : {
"lat" : -90,
"lng" : -180
}
},
"location" : {
"lat" : 37.09024,
"lng" : -95.712891
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : 49.38,
"lng" : -66.94
},
"southwest" : {
"lat" : 25.82,
"lng" : -124.39
}
}
},
"types" : [ "country", "political" ]
}
],
"status" : "OK"
}
关于php - 如何解析谷歌地图地理编码结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5789298/
我正在学习如何使用Nokogiri,根据这段代码我遇到了一些问题:require'rubygems'require'mechanize'post_agent=WWW::Mechanize.newpost_page=post_agent.get('http://www.vbulletin.org/forum/showthread.php?t=230708')puts"\nabsolutepathwithtbodygivesnil"putspost_page.parser.xpath('/html/body/div/div/div/div/div/table/tbody/tr/td/div
总的来说,我对ruby还比较陌生,我正在为我正在创建的对象编写一些rspec测试用例。许多测试用例都非常基础,我只是想确保正确填充和返回值。我想知道是否有办法使用循环结构来执行此操作。不必为我要测试的每个方法都设置一个assertEquals。例如:describeitem,"TestingtheItem"doit"willhaveanullvaluetostart"doitem=Item.new#HereIcoulddotheitem.name.shouldbe_nil#thenIcoulddoitem.category.shouldbe_nilendend但我想要一些方法来使用
我有一个字符串input="maybe(thisis|thatwas)some((nice|ugly)(day|night)|(strange(weather|time)))"Ruby中解析该字符串的最佳方法是什么?我的意思是脚本应该能够像这样构建句子:maybethisissomeuglynightmaybethatwassomenicenightmaybethiswassomestrangetime等等,你明白了......我应该一个字符一个字符地读取字符串并构建一个带有堆栈的状态机来存储括号值以供以后计算,还是有更好的方法?也许为此目的准备了一个开箱即用的库?
关闭。这个问题是opinion-based.它目前不接受答案。想要改进这个问题?更新问题,以便editingthispost可以用事实和引用来回答它.关闭4年前。Improvethisquestion我想在固定时间创建一系列低音和高音调的哔哔声。例如:在150毫秒时发出高音调的蜂鸣声在151毫秒时发出低音调的蜂鸣声200毫秒时发出低音调的蜂鸣声250毫秒的高音调蜂鸣声有没有办法在Ruby或Python中做到这一点?我真的不在乎输出编码是什么(.wav、.mp3、.ogg等等),但我确实想创建一个输出文件。
给定这段代码defcreate@upgrades=User.update_all(["role=?","upgraded"],:id=>params[:upgrade])redirect_toadmin_upgrades_path,:notice=>"Successfullyupgradeduser."end我如何在该操作中实际验证它们是否已保存或未重定向到适当的页面和消息? 最佳答案 在Rails3中,update_all不返回任何有意义的信息,除了已更新的记录数(这可能取决于您的DBMS是否返回该信息)。http://ar.ru
我在我的项目目录中完成了compasscreate.和compassinitrails。几个问题:我已将我的.sass文件放在public/stylesheets中。这是放置它们的正确位置吗?当我运行compasswatch时,它不会自动编译这些.sass文件。我必须手动指定文件:compasswatchpublic/stylesheets/myfile.sass等。如何让它自动运行?文件ie.css、print.css和screen.css已放在stylesheets/compiled。如何在编译后不让它们重新出现的情况下删除它们?我自己编译的.sass文件编译成compiled/t
我正在寻找执行以下操作的正确语法(在Perl、Shell或Ruby中):#variabletoaccessthedatalinesappendedasafileEND_OF_SCRIPT_MARKERrawdatastartshereanditcontinues. 最佳答案 Perl用__DATA__做这个:#!/usr/bin/perlusestrict;usewarnings;while(){print;}__DATA__Texttoprintgoeshere 关于ruby-如何将脚
我正在使用的第三方API的文档状态:"[O]urAPIonlyacceptspaddedBase64encodedstrings."什么是“填充的Base64编码字符串”以及如何在Ruby中生成它们。下面的代码是我第一次尝试创建转换为Base64的JSON格式数据。xa=Base64.encode64(a.to_json) 最佳答案 他们说的padding其实就是Base64本身的一部分。它是末尾的“=”和“==”。Base64将3个字节的数据包编码为4个编码字符。所以如果你的输入数据有长度n和n%3=1=>"=="末尾用于填充n%
我主要使用Ruby来执行此操作,但到目前为止我的攻击计划如下:使用gemsrdf、rdf-rdfa和rdf-microdata或mida来解析给定任何URI的数据。我认为最好映射到像schema.org这样的统一模式,例如使用这个yaml文件,它试图描述数据词汇表和opengraph到schema.org之间的转换:#SchemaXtoschema.orgconversion#data-vocabularyDV:name:namestreet-address:streetAddressregion:addressRegionlocality:addressLocalityphoto:i
Rackup通过Rack的默认处理程序成功运行任何Rack应用程序。例如:classRackAppdefcall(environment)['200',{'Content-Type'=>'text/html'},["Helloworld"]]endendrunRackApp.new但是当最后一行更改为使用Rack的内置CGI处理程序时,rackup给出“NoMethodErrorat/undefinedmethod`call'fornil:NilClass”:Rack::Handler::CGI.runRackApp.newRack的其他内置处理程序也提出了同样的反对意见。例如Rack