2016-08-20 75 views
-1

我使用下面的代碼。當我的用戶登錄並輸入adderess時,我想在地圖上顯示他們的位置。在谷歌地圖中使用變量

<?php 
// Get lat and long by address 
$dlocation = '2286 Wellman Place,Oak Harbor,WA'; 
$address = $dlocation; // Google HQ 
$prepAddr = str_replace(' ','+',$address); 
$geocode=file_get_contents('https://maps.google.com/maps/api/geocode/json?address='.$prepAddr.'&sensor=false'); 
$output= json_decode($geocode); 
$latitude = $output->results[0]->geometry->location->lat; 
$longitude = $output->results[0]->geometry->location->lng; 
echo $latitude; 
?> 

<html> 
<head> 
    <style type="text/css"> 
     div#map { 
      position: relative; 
     } 

     div#crosshair { 
      position: absolute; 
      top: 192px; 
      height: 19px; 
      width: 19px; 
      left: 50%; 
      margin-left: -8px; 
      display: block; 
      /* background: url(crosshair.gif); */ 
      background-position: center center; 
      background-repeat: no-repeat; 
     } 
    </style> 
    <script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script> 
    <script type="text/javascript"> 
     var map; 
     var geocoder; 
     var centerChangedLast; 
     var reverseGeocodedLast; 
     var currentReverseGeocodeResponse; 

     function initialize() { 
      var latlng = new google.maps.LatLng(48.258545,-122.7399284); 
      var myOptions = { 
       zoom: 16, 
       center: latlng, 
       mapTypeId: google.maps.MapTypeId.ROADMAP 
      }; 
      map = new google.maps.Map(document.getElementById("map_canvas"), myOptions); 
      geocoder = new google.maps.Geocoder(); 

      var marker = new google.maps.Marker({ 
       position: latlng, 
       map: map, 
       title: "Hello World!" 
      }); 

     } 

    </script> 
</head> 
<body onload="initialize()"> 
<div id="map" style="width:700px; height:800px"> 
    <div id="map_canvas" style="width:100%; height:800px"></div> 
    <div id="crosshair"></div> 
</div> 


</body> 
</html> 

當我嘗試更換48.258545,-122.7399284與變量$緯度和經度$地圖不加載。 我需要改變什麼才能使用。

回答

0

如果你想使用PHP瓦爾在JavaScript必須呼應的結果

 function initialize() { 
     var latlng = new google.maps.LatLng(<?= $latitude . ',' . $longitude ?>); 
     var myOptions = { 
      zoom: 16, 
      center: latlng, 
      mapTypeId: google.maps.MapTypeId.ROADMAP 
     }; 

或JavaScript的瓦爾

<script type="text/javascript"> 
    var map; 
    var geocoder; 
    var centerChangedLast; 
    var my_latitude = <?= $latitude ?>; 
    var my_longitude = <?= $longitude ?>; 
    ....... 

     ..... 
     function initialize() { 
     var latlng = new google.maps.LatLng(my_latitude , my_longitude); 
     var myOptions = { 
      zoom: 16, 
      center: latlng, 
      mapTypeId: google.maps.MapTypeId.ROADMAP 
     }; 
+0

謝謝你,偉大的工作分配PHP的VAR值。我不得不改變我的態度 – gary1946