簡體   English   中英

帶有php變量的Google路線

[英]Google directions with php variable

您好,我想讓Google路線與地理位置一起使用,我已經完成了地理位置部分,並且可以與路線鏈接,並且效果很好,問題是當我嘗試使其動態化時,它會為不同頁面的不同對象獲取路線我無法使它正常工作,我可以在目標說明中回顯一個變量,那樣就可以了,但它不起作用,在源代碼中目標是變量而不是內容。 這是我的代碼

<?php $rows = mysql_fetch_array($results) or die(mysql_error());
    echo '<script>';
        echo 'if (navigator.geolocation){
            navigator.geolocation.getCurrentPosition(function (position) {
                var latitude = position.coords.latitude;
                var longitude = position.coords.longitude;
                var coords = new google.maps.LatLng(latitude, longitude);
                var directionsService = new google.maps.DirectionsService();
                var directionsDisplay = new google.maps.DirectionsRenderer();
                var mapOptions ={
                    zoom: 15,
                    center: coords,
                    mapTypeControl: true,
                    navigationControlOptions:{
                        style: google.maps.NavigationControlStyle.SMALL
                    },
                    mapTypeId: google.maps.MapTypeId.ROADMAP
                };
                map = new google.maps.Map(document.getElementById("map-canvas"), mapOptions);
                directionsDisplay.setMap(map);
                directionsDisplay.setPanel(document.getElementById(\'panel\'));
                var request = {
                    origin: coords,';
                    $geo = $rows['GEO'];
                echo 'destination: \'$geo\',
                    travelMode: google.maps.DirectionsTravelMode.DRIVING
                };
                directionsService.route(request, function (response, status) {
                    if (status == google.maps.DirectionsStatus.OK) {
                        directionsDisplay.setDirections(response);
                    }
                });
            });
        }
        google.maps.event.addDomListener(window, \'load\', initialize);';
    echo '</script>';
?>

如果有人看到我在做什么錯,請告訴我,謝謝您的幫助。

編輯

我的查詢(工作時它顯示頁面上的其他所有內容)

$id = $_GET["id"] ;
$sql = "SELECT * FROM Posts where id = $id" ;
$results = mysql_query($sql) or die(mysql_error()) ;

在數據庫GEO而不是地理位置中,它只是一個名稱,例如城鎮,商店等。

如果用'回顯,則將忽略PHP變量。 像這樣使用“:

            $geo = $rows['GEO'];
            echo 'destination: '."\'$geo\'".',
                travelMode: google.maps.DirectionsTravelMode.DRIVING
            };

這個問題與單引號引起來,但您可能應該考慮使用HEREDOC格式。

這將為您提供正確的字符串$geo

 echo "destination: '$geo',";

使用HEREDOC可使閱讀起來更容易:

$geo = $rows['GEO'];
$html = <<<HTML
<script>
if (navigator.geolocation){
    navigator.geolocation.getCurrentPosition(function (position) {
        var latitude = position.coords.latitude;
        var longitude = position.coords.longitude;
        var coords = new google.maps.LatLng(latitude, longitude);
        var directionsService = new google.maps.DirectionsService();
        var directionsDisplay = new google.maps.DirectionsRenderer();
        var mapOptions = {
            zoom: 15,
            center: coords,
            mapTypeControl: true,
            navigationControlOptions:{
                style: google.maps.NavigationControlStyle.SMALL
            },
            mapTypeId: google.maps.MapTypeId.ROADMAP
        };
        map = new google.maps.Map(document.getElementById("map-canvas"), mapOptions);
        directionsDisplay.setMap(map);
        directionsDisplay.setPanel(document.getElementById('panel'));
        var request = {
            origin: coords,
            destination: '$geo',
            travelMode: google.maps.DirectionsTravelMode.DRIVING
        };
        directionsService.route(request, function (response, status) {
            if (status == google.maps.DirectionsStatus.OK) {
                directionsDisplay.setDirections(response);
            }
        });
    });
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>
HTML;
echo $html;

暫無
暫無

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

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