我正在尝试遵循此处所说的内容,但进行了一些编辑。我似乎无法弄清楚我做错了什么。什么都没有显示。自定义字段显然也被填充了。
的JavaScript:
function initialize() {
lat = 0;
long = 0;
if (typeof my-coordinates !== 'undefined' && my-coordinates.lat && my-coordinates.long) {
lat = my-coordinates.lat;
long = my-coordinates.long;
}
var mapProp = {
center: new google.maps.LatLng(lat, long),
zoom: 5,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById("googleMap"), mapProp);
}
的HTML:
<script src="https://maps.googleapis.com/maps/api/js?sensor=false" type="text/javascript"></script>
<script type="text/javascript" src="../map.js"></script>
</div>
<div class="googlemap">
<body onload="initialize()">
<div id="map" style="width: 350px; height: 310px;"></div>
</div>
功能:
function register_plugin_scripts() {
global $post;
$woo_maps_lat = get_post_meta($post->ID, 'woo_maps_lat', true);
$woo_maps_long = get_post_meta($post->ID, 'woo_maps_long', true);
if( !empty($woo_maps_lat) && !empty($woo_maps_long) ) {
wp_localize_script('my-coordinates-script', 'my-coordinates', array(
'lat' => $woo_maps_lat,
'long' => $woo_maps_long
));
}
} // end register_plugin_scripts
add_action( 'wp_enqueue_scripts', 'register_plugin_scripts' );
编辑:我仍然无法弄清楚这一点。这是我到目前为止所得到的。
function register_plugin_scripts() {
global $post;
$woo_maps_lat = get_post_meta($post->ID, 'woo_maps_lat', true);
$woo_maps_long = get_post_meta($post->ID, 'woo_maps_long', true);
if( !empty($woo_maps_lat) && !empty($woo_maps_long) ) {
wp_enqueue_script('my_coordinates_script', get_template_directory_uri() . 'http://michaeltieso.com/map/wp-content/plugins/medellin-living-map/map.js');
wp_localize_script('my_coordinates_script', 'my_coordinates', array(
'lat' => $woo_maps_lat,
'long' => $woo_maps_long
));
}
}
add_action( 'wp_enqueue_scripts', 'register_plugin_scripts' );
对于 JS
function initialize() {
lat = 0;
long = 0;
if (typeof my_coordinates !== 'undefined' && my_coordinates.lat && my_coordinates.long) {
lat = my_coordinates.lat;
long = my_coordinates.long;
}
var mapProp = {
center: new google.maps.LatLng(lat, long),
zoom: 5,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById("googleMap"), mapProp);
}
您可以在此处查看示例:http: //michaeltieso.com/map/hello-world/