我在餐馆店工作。我的客户需要通过起诉GPS显示附近的餐馆。所以我认为使用ajax和javascript但我的地图短代码没有运行。这是我的HTML和javascript代码。
<p id="demo">...</p>
<script>
var x = document.getElementById("demo");
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition);
} else {
x.innerHTML = "Geolocation is not supported by this browser.";
}
function closest (num, arr) {
var mid;
var lo = 0;
var hi = arr.length - 1;
while (hi - lo > 1) {
mid = Math.floor ((lo + hi) / 2);
if (arr[mid] < num) {
lo = mid;
} else {
hi = mid;
}
}
if (num - arr[lo] <= arr[hi] - num) {
return arr[lo];
}
return arr[hi];
}
function showPosition(position) {
<?php
$jsar = json_encode($lat);
echo "var lant = " . $jsar.";\n";
?>
var my_lat = position.coords.latitude;
var my_lang = position.coords.longitude;
var location = closest (my_lat, lant);
//alert(location);
/*x.innerHTML = "Latitude: " + position.coords.latitude +
"<br>Longitude: " + position.coords.longitude; */
jQuery.ajax({
type: "POST",
url: '<?php echo get_template_directory_uri(); ?>/kitchen.php',
data: {
firstname: location
}
})
.done(function (msg) {
document.getElementById("demo").innerHTML = msg;
});
}
</script>
这是我的ajax代码。
<?php require('./../../../wp-blog-header.php');
require('./../../../wp-load.php');
global $post;
$args = array(
'numberposts' => 1,
'orderby' => 'post_date',
'order' => 'DESC',
'post_type' => 'our_kitchen',
'post_status' => 'publish',
'suppress_filters' => true
);
$postslist = get_posts( $args );
foreach ( $postslist as $post ) :
setup_postdata( $post );
$close_lat = get_post_meta(get_the_ID(), '_wpgmp_metabox_latitude', true);
$title = $_POST['firstname'];
if( $title == $close_lat ) { ?>
<a href="<?php echo get_permalink(); ?>"><?php the_title(); ?></a> <?php
$maptext = '';
if (get_post_meta(get_the_ID(), '_wpgmp_metabox_latitude', true) != '') {
$locationlat = get_post_meta(get_the_ID(), '_wpgmp_metabox_latitude', true);
$locationlong = get_post_meta(get_the_ID(), '_wpgmp_metabox_longitude', true);
$maptext.= '[display_map width="100%" height="210" zoom="15" language="en" map_type="ROADMAP" map_draggable="true" marker1="';
$maptext.= $locationlat;
$maptext.= ' | ';
$maptext.= $locationlong;
$maptext.= ' | ' . get_the_title() . '"]';
echo do_shortcode($maptext);
}
}
?>
<?php
endforeach;
wp_reset_postdata();
?>
提前谢谢。