0

我对这个错误感到恼火/////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(); 回声 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 回答