-1

こんにちは、地理位置情報を使用してGoogleの方向を取得しようとしています。地理位置情報の部分が完了しました。方向とリンクしており、正常に機能します。問題は、動的にしようとしたときに、さまざまなページのさまざまなオブジェクトの方向を取得することですut私はそれを動作させることができません.宛先スペクションで変数をエコーすることはできますが、それは機能しますが、ソースコードでは、宛先はコンテンツではなく変数です. 私のコードはこちら

<?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は地理的な場所ではなく、町や店などの名前です.

4

2 に答える 2

2

' でエコーすると、php 変数は無視されます。" を次のように使用します。

            $geo = $rows['GEO'];
            echo 'destination: '."\'$geo\'".',
                travelMode: google.maps.DirectionsTravelMode.DRIVING
            };
于 2013-11-15T00:42:35.763 に答える
2

この問題は一重引用符で囲まれた文字列にありますが、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;
于 2013-11-15T00:42:43.543 に答える