【发布时间】:2017-11-28 10:16:08
【问题描述】:
我在 php 变量中有一个坐标(latlng)。我需要将这些变量传递给 javascript google maps api 请求。我想在没有 onClick 函数的情况下将 $lat1 和 $lon1 传递给 javascript 中的 point1。我怎样才能做到这一点?
<?php
$lat1 = 6.893732;
$lon1 = 79.857516;
$lat2 = 6.856007;
$lon2 = 79.865284;
?>
<html>
<script src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&callback=initMap"
async defer></script>
<script>
var point1 = {lat: 41.85, lng: -87.65}; //Send $point1 value here
var point2 = {lat: 39.79, lng: -86.14}; //Send $point2 value here
// Set destination, origin and travel mode.
var request = {
destination: point1,
origin: point2,
travelMode: 'DRIVING'
};
// Pass the directions request to the directions service.
var directionsService = new google.maps.DirectionsService();
directionsService.route(request, function(response, status) {
if (status == 'OK') {
// Display the route on the map.
directionsDisplay.setDirections(response);
}
});
</script>
【问题讨论】:
-
可以将其作为js变量回显:echo "";
-
你的项目中的php和js代码是在同一个文件吗?还是在单独的文件中?
标签: javascript php google-maps-api-3