0

m irritating from this error/////m trying to fetch the langitude and lattitude from database and according to them i want show marker on map.....and there m facing that error....pls help me....thanx my code is:

    // 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 file

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(); } ?>
Deedar
  • 1
  • 3
  • It would help if you would state in which line do you get this error. Thanks – Danilo Radenovic Dec 26 '12 at 08:27
  • 1
    Looks like a text encoding problem. Unicode BOM? Or some invisible control or whitespace characters? – Thilo Dec 26 '12 at 08:31
  • m getting error in this line....var data = JSON.parse(data.responseText); – Deedar Dec 26 '12 at 08:34
  • Can you print `data.responseText` and also `getLocation.php`. Looks like a non-utf8 character finds its way in the response. – web-nomad Dec 26 '12 at 08:36
  • this is get_location.php file 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(); } ?> – Deedar Dec 26 '12 at 08:37
  • It signifies a non-utf8 character has been encountered. Can you follow the step in this post before doing `json_encode` in your `getLocation.php` file and see if it solves the issue - http://stackoverflow.com/questions/1401317/remove-non-utf8-characters-from-string – web-nomad Dec 26 '12 at 08:40
  • now m getting this error in same line::: Uncaught SyntaxError: Unexpected token – Deedar Dec 26 '12 at 08:42

1 Answers1

0

Try this and see if this helps:

<?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(); 
}
?>

Also, if this does not work, can you print your $locations variable so that we can see the structure.

Hope this helps.

web-nomad
  • 6,003
  • 3
  • 34
  • 49
  • this error occr when i run this code::: Parse error: syntax error, unexpected T_SL in C:\xampp\htdocs\google_api\get_location.php on line 5 – Deedar Dec 26 '12 at 08:54
  • **There should be no whitespace** before `END;` in the function `removeNonUTF8`. Remove all spaces, tabs etc before the `END;` – web-nomad Dec 26 '12 at 08:57
  • Also, **no whitespace after** `$regex = <<<'END'`. – web-nomad Dec 26 '12 at 08:59
  • You sure there is no whitespace before/after `$regex = <<<'END'` and `END;`? – web-nomad Dec 26 '12 at 09:00
  • function removeNonUTF8( &$value, $key ) { $regex =<<<'END' / ( (?:[\x00-\x7F] |[\xC0-\xDF][\x80-\xBF] |[\xE0-\xEF][\x80-\xBF]{2} |[\xF0-\xF7][\x80-\xBF]{3} )+ ) |. /x END; $value = preg_replace( $regex, '$1', $value ); } check this there is no space – Deedar Dec 26 '12 at 09:02
  • See this - http://phpfiddle.org/main/code/6fi-r2c. If this doesn't work, i am sorry :(. – web-nomad Dec 26 '12 at 09:05