I have made a web application which retrieves roads coordinates from a Postgres database and I when I attempt to visualize this using Google Maps API nothing shows.

I am trying to plot coordinates for every line in a table row.


     $sql =<<<EOF
         SELECT ST_AsGeoJSON(geom) FROM roads;

     $ret = pg_query($db, $sql);
         echo pg_last_error($db);
       while ($line = pg_fetch_array($result, null, PGSQL_ASSOC)) {
           $row[]=$line['coordinates']; //var_dump($array['coordinates'])



Here I try to visualize the rows:

  var map = new google.maps.Map(document.getElementById('map-canvas'),

  var flightPlanCoordinates = [
         new google.maps.LatLng('<?php echo implode("','", $roads); ?>')  
         var flightPath = new google.maps.Polyline({
             path: flightPlanCoordinates,
             geodesic: true,
             strokeColor: '#FF0000',
             strokeOpacity: 1.0,
             strokeWeight: 2


      google.maps.event.addDomListener(window, 'load', initialize);

This is your issue:

       var flightPlanCoordinates = [
         new google.maps.LatLng('<?php echo implode("','", $roads); ?>')

It does not create an array of LatLng. It creates an Array with one Invalid LatLng with in since it's expecting a latitude and a longitude and your giving it many latitudes and longitudes?

If you have more bugs then you need to break down what you are attempting into smaller steps. Try:

  1. Can you set a single marker/point in straight html, without DB access?

  2. Can you set another marker next to it from the DB?

  3. Can you draw an edge (2 point polyline) in straight html?

  4. Can you repeat the above from the DB?

Then I think you'll have a working system.

The other issues might be the format of the points(what projection), the geojson, the database configuration etc.

  • Implode appears to be like join in python/javascript, so almost certainly not what the OP wants in this situation, as he is passing an array to a method that takes a point. Your answer would be better as a comment, imho. – John Powell Oct 18 '14 at 22:59
  • @JohnBarça I was trying to lead them to think about their problem and how they can solve it. If they fix the array issue that I found then that's likely to be their only bug, but if not then following my suggestion will let them find it. It's not a great question as it's unlikely to help anyone else. – MichaelStoner Oct 19 '14 at 0:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.