在函数中插入地址字段

时间:2017-10-22 作者:Randomer11

下面的代码使用Google的地理编码API获取地址/位置的坐标,它可以手动键入我在下面写下“地址在这里”的地址,然后适当地输出坐标。

<?php
function getCoordinates($address) {

    $address = urlencode($address); 

    // set HTTP header
    $headers = array(\'Content-Type: application/json\');

    // Open connection
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, \'http://maps.google.com/maps/api/geocode/json?sensor=false&address=\' . $address);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);

    // Execute request
    $result = curl_exec($curl);

    // Close connection
    curl_close($curl);

    // get the result and parse to JSON

    $json = json_decode($result);

    $lat = $json->results[0]->geometry->location->lat;
    $lng = $json->results[0]->geometry->location->lng;

    return array($lat, $lng);

}

$coords = getCoordinates("ADDRESS GOES HERE ");
$coords = join(\',\', $coords);

?>
id现在想做的是使用自定义字段(ACF)将地址动态调用到“ADDRESS GOES HERE”。

包含地址的字段当前被调用到模板中,如下所示:

<?php if( get_field(\'address_line_1\') ): ?><?php the_title(); ?>, <?php the_field(\'address_line_1\', $post_id); ?>,<?php the_field(\'address_line_2\', $post_id); ?>, <?php the_field(\'town\', $post_id); ?>, <?php the_field(\'post_code\', $post_id); ?>, <?php the_field(\'country\', $post_id); ?><?php endif; ?>
在“地址在此处”位置使用上述字段的最佳方式是什么?

作为参考,curl脚本是https://colinyeoh.wordpress.com/2013/02/12/simple-php-function-to-get-coordinates-from-address-through-google-services/, 这似乎没有那么有效。

1 个回复
最合适的回答,由SO网友:Shital Marakana 整理而成

Insert Address fields into function

<?php
function getCoordinates($address) {

    $address = urlencode($address); 

    // set HTTP header
    $headers = array(\'Content-Type: application/json\');

    // Open connection
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, \'http://maps.google.com/maps/api/geocode/json?sensor=false&address=\' . $address);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);

    // Execute request
    $result = curl_exec($curl);

    // Close connection
    curl_close($curl);

    // get the result and parse to JSON

    $json = json_decode($result);

    $lat = $json->results[0]->geometry->location->lat;
    $lng = $json->results[0]->geometry->location->lng;

    return array($lat, $lng);

}

$post_id = get_the_ID();
$title = get_the_title($post_id );
$address_line_1 = get_field( "address_line_1",$post_id );
$address_line_2 = get_field( "address_line_2",$post_id );
$town = get_field( "town",$post_id );
$post_code = get_field( "post_code",$post_id );
$country = get_field( "country",$post_id );

if($address_line_1!="")
{
    $address_string = $title.",".$address_line_2.",".$town.",".$post_code.",".$country;
}
else
{
    $address_string = "";
}
$coords = getCoordinates($address_string);
$coords = join(\',\', $coords);

?>
结束

相关推荐

Functions.php中的入队样式

这对我没用functions.php: if( is_page_template( \'template-flat.php\' ) ) { function flatsome_scripts() { wp_enqueue_style( \'flatsome-style\', get_template_directory_uri() .\'/flatash/css/foundation.css\', array(), \'2.1\', \'all\'); }