0

m このエラーに苛立っています/////m データベースから経度と緯度を取得しようとしています。それらに従って、地図上にマーカーを表示したいのですが...そして、m がそのエラーに直面しています....助けてください.. ..thanx 私のコードは次のとおりです。

    // Ban Jelacic Square - City Center
            var map;
    var center = new google.maps.LatLng(-34.397, 150.644);

    var geocoder = new google.maps.Geocoder();
    var infowindow = new google.maps.InfoWindow();

    var directionsService = new google.maps.DirectionsService();
    var directionsDisplay = new google.maps.DirectionsRenderer();

    function init() {

        var mapOptions = {
          zoom: 8,
          center: center,
          mapTypeId: google.maps.MapTypeId.ROADMAP
        }

        map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions);

        directionsDisplay.setMap(map);
        directionsDisplay.setPanel(document.getElementById('directions_panel'));

    /*  // Detect user location
        if(navigator.geolocation) {
            navigator.geolocation.getCurrentPosition(function(position) {

                var userLocation = new google.maps.LatLng(position.coords.latitude,position.coords.longitude);

                geocoder.geocode( { 'latLng': userLocation }, function(results, status) {
                    if (status == google.maps.GeocoderStatus.OK) {
                        document.getElementById('start').value = results[0].formatted_address;
                    }
                });

            }, function() {
                alert('Geolocation is supported, but it failed');
            });
        }
        */
        makeRequest('get_location.php', function(data) {
            var data = JSON.parse(data.responseText);
            for (var i = 0; i < data.length; i++) {
            displayLocation(data[i]);
            }
        });
    }

    function displayLocation(location) {

        var content =   '<div class="infoWindow"><strong>'  + location.name + '</strong>'
                        + '<br/>'   + location.address
                        + '<br/>'   + location.description + '</div>';

        if (parseInt(location.lat) == 0) {
            geocoder.geocode( { 'address': location.address }, function(results, status) {
                if (status == google.maps.GeocoderStatus.OK) {

                    var marker = new google.maps.Marker({
                        map: map, 
                        position: results[0].geometry.location,
                        title: location.name
                    });

                    google.maps.event.addListener(marker, 'click', function() {
                        infowindow.setContent(content);
                        infowindow.open(map,marker);
                    });
                }
            });
        } else {

            var position = new google.maps.LatLng(parseFloat(location.lat), parseFloat(location.lng));
            var marker = new google.maps.Marker({
                map: map, 
                position: position,
                title: location.name
            });
            google.maps.event.addListener(marker, 'click', function() {
                infowindow.setContent(content);
                infowindow.open(map,marker);
            });
        }
    }  
    function makeRequest(url, callback) {
        var request;
        if (window.XMLHttpRequest) {
            request = new XMLHttpRequest(); // IE7+, Firefox, Chrome, Opera, Safari
        } else {
            request = new ActiveXObject("Microsoft.XMLHTTP"); // IE6, IE5
        }
        request.onreadystatechange = function() {
            if (request.readyState == 4 && request.status == 200) {
                callback(request);
            }
        }
        request.open("GET", url, true);
        request.send();
    }

/////get_location.php ファイル

setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION ); $sth = $db->query("SELECT * FROM data"); $locations = $sth->fetchAll(); echo json_encode( $locations ); } catch (Exception $e) { echo $e->getMessage(); } ?>
4

1 に答える 1

0

これを試して、これが役立つかどうかを確認してください:

<?php
require 'config1.php';

function removeNonUTF8( &$value, $key ) {
    $regex = <<<'END'
/
  (
    (?: [\x00-\x7F]
    |   [\xC0-\xDF][\x80-\xBF]
    |   [\xE0-\xEF][\x80-\xBF]{2}
    |   [\xF0-\xF7][\x80-\xBF]{3}
    )+                              # ...one or more times
  )
| .                                 # anything else
/x
END;

    $value = preg_replace( $regex, '$1', $value );
}

try { 
    $db = new PDO($dsn, $uname, $pass); 
    $db->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION ); 

    $sth = $db->query("SELECT * FROM data"); 
    $locations = $sth->fetchAll(); 

    foreach( $locations as $location ) {
        array_walk( $location, 'removeNonUTF8' );
    }

    echo json_encode( $locations ); 
} catch (Exception $e) { 
    echo $e->getMessage(); 
}
?>

また、これが機能しない場合は、$locations変数を出力して構造を確認できますか。

お役に立てれば。

于 2012-12-26T08:49:52.227 に答える