0

我在使用 Google Maps API 时遇到了一个令人费解的小问题,这让我大吃一惊。以下代码

<?php
$anim='images/animation.gif';
session_start();
if ($_SESSION['anim']==1) {
$anim='images/transparent.png';
}

//Select database
require_once('../Connections/MySQL_extranet.php');
mysql_select_db($database_MySQL_extranet, $MySQL_extranet);

// Get the hid which is passed from previous page
if (isset($_REQUEST['hid'])) {
    $hid=$_REQUEST['hid'];
}

//MySQL query to select location details
$query_location = "SELECT hotel, lat, lng, zoomair, sp_lat, sp_lng FROM ex_hotel WHERE hid = '$hid'";
$location = mysql_query($query_location, $MySQL_extranet) or die(mysql_error());
$row_location = mysql_fetch_assoc($location);

//Set the session variable to pass the hotel name
$_SESSION['hotel'] = $row_location['hotel'];
?>

<!DOCTYPE html>
<!--[if IE 7 ]>    <html class="ie7"> <![endif]-->
<!--[if (gt IE 7)|!(IE)]><!--> <html> <!--<![endif]-->
<!--Dump the above when IE7 users go below 2% -->
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Little Hotels - Directions to <?php echo $row_location['hotel'];?></title>
<meta name="description" content="Little Hotels provides accurate directions to hotels from any starting point you choose.">
<meta name="keywords" content="hotel directions, google maps, Little Hotels, driving directions, directions to <?php echo $row_location['hotel'];?>">
<link rel="shortcut icon" href="favicon.ico" type="image/x-icon" >
<?php 
$full_url = (!empty($_SERVER['HTTPS'])) ? "https://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'] : "http://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];
$canonical_url = strtok($full_url,'?');
echo '<link rel="canonical" href="' . $canonical_url . '" >'; 
?>
<link rel="stylesheet" href="css/littlehotels.css" type="text/css">
<style type="text/css">
#content{padding: 70px 5px 0; width: 100%;}
#mapcontainer{float:left; display:block;}
#mapCanvas {width: 600px; height: 400px;}

#markerStatus {height: 0px;}
#info {height: 0px;}
#infoPanel {margin-left: 620px; margin-right:10px; display:block;}
#infoPanel div {margin-bottom: 5px;}
</style>
<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=false"></script>
<script type="text/javascript">
var geocoder = new google.maps.Geocoder();
    var lat = <?php echo $row_location['lat'];?>;
    var lng = <?php echo $row_location['lng'];?>;
    var latlng = new google.maps.LatLng(lat, lng);

function geocodePosition(pos) {
  geocoder.geocode({
    latLng: pos
  }, function(responses) {
    if (responses && responses.length > 0) {
      updateMarkerAddress(responses[0].formatted_address);
    } else {
      updateMarkerAddress('Cannot determine address at this location.');
    }
  });
}

function updateMarkerStatus(str) {
  document.getElementById('markerStatus').innerHTML = str;
}

function updateMarkerPosition(startpoint) {
  document.getElementById('info').innerHTML = [
    startpoint.lat(),
    startpoint.lng()
  ].join(', ');
  }

function nextPage() {
  saddress = document.getElementById('info').innerHTML;
location='directions_detail.php?saddr='+saddress+'&daddr='+latlng;
}

function updateMarkerAddress(str) {
  document.getElementById('address').innerHTML = str;
}

function initialize() {
    var zoom = <?php echo $row_location['zoomair'];?>;
    var sp_lat = <?php echo $row_location['sp_lat'];?>;
    var sp_lng = <?php echo $row_location['sp_lng'];?>;
    var startpoint = new google.maps.LatLng(sp_lat, sp_lng);

    var map = new google.maps.Map(document.getElementById('mapCanvas'), {
    zoom: zoom,
    center: latlng,
    mapTypeId: google.maps.MapTypeId.ROADMAP,
    streetViewControl: false,
    zoomControl: true,
    zoomControlOptions: {
    style: google.maps.ZoomControlStyle.SMALL
    }
  });

var destimage = new google.maps.MarkerImage('/images/hotel_icon.gif',
      new google.maps.Size(32, 37),
      new google.maps.Point(0,0),
      new google.maps.Point(16, 35));
var destshadow = new google.maps.MarkerImage('/images/hotelshadow.png',
      new google.maps.Size(51, 37),
      new google.maps.Point(0,0),
      new google.maps.Point(16, 35));
var marker = new google.maps.Marker({
  icon: destimage,
  shadow: destshadow,
  map: map,
  position: latlng
});

var startimage = new google.maps.MarkerImage(
  '/images/start_icon.png',
      new google.maps.Size(59, 37),
      new google.maps.Point(0,0),
      new google.maps.Point(29, 35));
    var marker = new google.maps.Marker({
    position: startpoint,
    title: 'If necessary, drag this to a different start point',
    map: map,
    draggable: true,
    icon: startimage
  });

  // Update current position info.
  updateMarkerPosition(startpoint);
  geocodePosition(startpoint);

  // Add dragging event listeners.
  google.maps.event.addListener(marker, 'dragstart', function() {
    updateMarkerAddress('Dragging...');
  });

  google.maps.event.addListener(marker, 'drag', function() {
    updateMarkerStatus('Dragging...');
    updateMarkerPosition(marker.getPosition());
  });

  google.maps.event.addListener(marker, 'dragend', function() {
    updateMarkerStatus('Drag ended');
    geocodePosition(marker.getPosition());
  });
}

// Onload handler to fire off the app.
google.maps.event.addDomListener(window, 'load', initialize);
</script>
  </head>
  <body onload="initialize()">
<div id="wrapper">
<a href="http://www.littlehotels.co.uk"><img src="images/transparent.png" width="266" height="122" border="0" alt="Little Hotels"></a>
<img src="<?php echo $anim ?>" alt="Little Hotels logo" width="93" height="117" align="top">
<img src="images/header/directions.png" alt="Little Hotels" width="364" height="88">
<div id="container">
<?php include("includes/topnavbar.html"); ?>
<div id="breadcrumb">
<div class="bc-link" style="width:200px"><a href="../carhire.php" target="_blank">Car Hire at competitive rates</a></div>
<div class="bc-link"><a href="flights.php" target="_blank">Flights</a></div>
<div class="bc-link"><a href="ferries.php" target="_blank">Ferries</a></div>
<div class="bc-link"><a href="airportparking.php" target="_blank">Airport Parking</a></div>
<div class="bc-link"><a href="airporthotels.php" target="_blank">Airport Hotels</a></div>
<div class="bc-link" style="width:200px"><a href="travelinsurance.php" target="_blank">Travel Insurance</a></div>
<div class="bc-link" style="width:200px" align="right"><a href="wp/" target="_blank">News and Special Offers</a></div>
</div>
  <div id="content">
<div id="mapcontainer">
<table border=1 bordercolor="#666666">
      <tr>
        <td>
  <div id="mapCanvas"></div>
        </td>
      </tr>
</table>
</div>
<div id="infoPanel">
<h2>Driving Directions to <?php echo $row_location['hotel'];?> </h2>
<!-- Following two lines must be kept in, even though not visible. Also corresponding lines in Style -->
    <div id="markerStatus" style="visibility:hidden;"><i>Click and drag the marker.</i></div>
    <div id="info" style="visibility:hidden;"></div>
    <b>Start Point:</b>
    <div id="address"></div>
    <br><b>Destination:</b><br>
    <div><?php echo $row_location['hotel'];?></div>
<br>
To select a different starting point, just drag the start icon to the required location (zoom in if necessary for better precision).<br>
<br>
<button onclick="nextPage()">Get Directions</button>
<br>
<span class="verdana"><br>

</span>
</div>
</div>
</div>
</div>
</body>
</html>
<?php 
$_SESSION['anim']=1;
?>

创建一个令人满意的页面(http://new.littlehotels.co.uk/directions_hotel.php?hid=colorado),但我想添加一些改进。这些更改需要我从一段 MySQL 数据创建一个 javascript 变量。

因此,我想创建一条类似这样的线:

var hotelname = <?php echo $row_location['hotel'];?>;

这应该不会太难,因为我已经在工作代码中有几行类似的行。但是,无论我尝试什么变化,这个新行每次都会打破页面。当我添加该行时,页面显示但没有地图,并且右侧没有起点的名称。

那是我吗?????????????

4

2 回答 2

1

我猜你正在输出一个字符串,$row_location['hotel']在这种情况下你需要用引号括起来告诉javascript它不是一个变量而是一个字符串值。

因此:

var hotelname = "<?php echo $row_location['hotel'];?>";

要检查上述假设是否正确,请检查您的控制台,您将看到以下错误:

ReferenceError: <value returned by $raw_location['hotel']> is not defined
于 2012-04-22T20:57:45.727 回答
0

像这样可以将变量传递给 JS,它可以防止 PHP 解释器抱怨任何丢失的索引,以防万一(这取决于 php.ini 中错误报告的设置方式),例如:

var hotelname = "<?php echo(isset($row_location['hotel']) ? $row_location['hotel'] :''); ?>";
console.info(hotelname);

周围的引号是不同的,就像 $row_location['hotel'] 在 JavaScript 中被强制转换为字符串。

...这段代码返回什么,你甚至得到任何结果?

while ($row_location = mysql_fetch_assoc($location)) {
   echo '<pre>'.print_r($row_location,true).'</pre>';
}
于 2012-04-23T00:06:00.650 回答