嗯,这有点容易。。
google map API需要如下格式Your Street 123, 54321 Your City
假设您有如下数据:
<div id="street">Your Street 123</div>
<div id="zip">54321</div>
<div id="city">Your City</div>
假设您使用的是jQuery:
(function($){
$.fn.create_gmap_address = function() {
var street = $(\'#street\').html();
var zip = $(\'#zip\').html();
var city = $(\'#city\').html();
return street + \',\' + zip + \' \' + city;
}
})(jQuery);
然后在地图API代码中:
geocoder.geocode( { \'address\': $.create_gmap_address() }, function(results, status) {
// your maps code
Solution 2
当然,您也可以通过php获取所有这些数据,然后将其存储到全局JS变量中(也许是更好的方法)。
function set_address_js_var() {
if ( \'your_post_type\' == get_post_type() ) {
// Assuming you are using meta fields to store the address
$postMeta = get_post_meta(get_the_ID());
return $postMeta[\'sreet\'] . \',\' . $postMeta[\'zip\'] . \' \' . $postMeta[\'city\'];
}
}
function load_fe_scripts() {
wp_enqueue_script( \'global-js-var\', get_template_directory_uri() . \'/js/my_file.js\' );
$localize_array = array(
\'google_maps_address\' => set_address_js_var()
);
wp_localize_script( \'global-js-var\', \'my_global\', $localize_array );
}
add_action(\'wp_enqueue_scripts\', \'load_fe_scripts\');
现在,您可以通过my\\u global访问JS文件中的var。google\\u maps\\u地址