【发布时间】:2018-04-21 13:56:20
【问题描述】:
我的 wordpress 单个帖子页面上有一个谷歌地图,它从 2 个自定义字段中获取地址。它工作正常,但现在我正在尝试添加街景链接/选项。
我的页面上有 --
<iframe width="100%" height="350" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="https://www.google.com/maps/embed/v1/place?q=<?php echo $add; ?>,%20<?php $terms = wp_get_post_terms(get_the_ID(), 'city-type');
if ( ! empty( $terms ) && ! is_wp_error( $terms ) ){
foreach ( $terms as $term ) {
if ($term->parent == 0) //check for parent terms only
echo '' . $term->name . '';
}
} ?>&zoom=17&key=mytoken"></iframe>
然后会输出这样的东西 --
<iframe width="100%" height="350" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="https://www.google.com/maps/embed/v1/place?q=100 las vegas ave,%20Las Vegas, NV&zoom=17&key=mytoken"></iframe>
有没有办法在不使用坐标的情况下添加街景?
我尝试获取坐标,但它们有点偏离 --
<?php
function getCoordinates($address){
$address = str_replace(" ", "+", $address); // replace all the white space with "+" sign to match with google search pattern
$url = "https://maps.google.com/maps/api/geocode/json?sensor=false&address=$address";
$response = file_get_contents($url);
$json = json_decode($response,TRUE); //generate array object from the response from the web
return ($json['results'][0]['geometry']['location']['lat'].",".$json['results'][0]['geometry']['location']['lng']);
}
$terms = wp_get_post_terms(get_the_ID(), 'city-type');
if ( ! empty( $terms ) && ! is_wp_error( $terms ) ){
foreach ( $terms as $term ) {
if ($term->parent == 0) //check for parent terms only
echo getCoordinates($add, $term->name, $property_pin);
}
} else {
echo getCoordinates($add, $term->name, $property_pin);
}
?>
我已经在使用地理编码来尝试事先获取坐标。例如,地理编码给了我这些坐标——34.0229995,-118.4931421,但我正在寻找的坐标是——34.050217,-118.259491
【问题讨论】:
-
不,遗憾的是仍然没有办法先通过地理编码来做到这一点:stackoverflow.com/questions/387942/google-street-view-url?rq=1
-
有什么替代方案?
-
检查另一个问题 :-) 如果你想拥有街景,你需要纬度/经度,你可以得到地理编码。
-
在我的问题中,我说我尝试使用地理编码,但坐标偏离了一小部分。
标签: javascript php wordpress google-maps google-street-view