簡體   English   中英

如何從php變量給谷歌地圖腳本提供經度和緯度值

[英]How to give longitude and latitude value to google map script from a php variable

我有一個像這樣的谷歌地圖腳本,這是JQuery論壇中提供的標准谷歌地圖腳本

<script type="text/javascript">          
/*
 * Google Maps documentation: http://code.google.com/apis/maps/documentation/javascript/basics.html
 * Geolocation documentation: http://dev.w3.org/geo/api/spec-source.html
*/
$( document ).on( "pageinit", "#map-page", function() {
    <?php $coordenadas=explode(',',$fila['Googlemap']);?>
    var defaultLatLng = new google.maps.LatLng(<?php echo $coordenadas[0];?>,<?php echo $coordenadas[1];?> ); 
    drawMap(defaultLatLng); // Default to Hollywood, CA when no geolocation support

    function drawMap(latlng) {
        var myOptions = {
            zoom: 10,
            center: latlng,
            mapTypeId: google.maps.MapTypeId.ROADMAP
        };
        var map = new google.maps.Map(document.getElementById("map-canvas"), myOptions);
        // Add an overlay to the map of current lat/lng
        var marker = new google.maps.Marker({
            position: latlng,
            map: map,
            title: "Greetings!"
        });
    }
}); 
</script>

如果你在代碼中看到我試圖將從php變量中提取的經度和緯度值傳遞給函數draw_map(); 但我無法做到這一點。 有人可以幫我這個嗎?

編輯-1

現在地圖正在使用@sukhwinder提供的解決方案,但另一個問題是地圖不是以屏幕為中心。 當我在移動瀏覽器中打開時,標記顯示在左上角,並且地圖完全不可見。

因為您的值不是用單引號括起來的。

更正代碼:

var defaultLatLng = new google.maps.LatLng('<?php echo $coordenadas[0];?>','<?php echo $coordenadas[1];?>');

我不確定你想要做什么,但如果你想將lat / long值傳遞給PHP頁面,

  var center = map.getCenter();
  var centerLat = center.lat();
  var centerLong = center.lng();

然后,正如前面提到的jon,您可以將值設置為隱藏字段。 或者,您只需將值添加到查詢字符串即可。 例如http://foo.com/your_code.php?centerLat=...&centerLong= ...

當用戶提交表單時,您的PHP代碼會獲得lat / long值

<?php
  $centerLat = $_REQUEST['centerLat'];
  $centerLong = $_REQUEST['centerLong'];

  // Store the values in your database.
?>

希望能幫助到你。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM