3

I am using a PHP script (this one) to generate a JSON file for a Google Map.

this is the PHP code (note: I am using Laravel):

<?php

$query = "SELECT id, info, lat, lng FROM places";
$results = DB::select($query);

$myLocations = array();
$i = 0;

$testLoc = array('loc95' => array( 'lat' => 15, 'lng' => 144.9634 ));

foreach ($results as $result)
{
  $myLocation = array(
    'loc'.++$i => array(
    'lat' => round((float)$result->lat, 4),
    'lng' => round((float)$result->lng, 4)
  ));
  $myLocations += $myLocation;
}

$myLocations += $testLoc;

echo json_encode($myLocations);
?>

and this is the output:

{"loc1":{"lat":45.4833,"lng":9.1854},"loc2":{"lat":45.4867,"lng":9.1648},"loc3":{"lat":45.4239,"lng":9.1652},"loc95":{"lat":15,"lng":144.9634}}

ok. the script I use to put the JSON data in a Google Map, unfortunately, keeps ignoring any data coming from the MySQL database, and shows only the test data place(s). I have tried to swap data, to put in test data the same info found in database... nothing, I keep seeing only the test data.

but, really: I cannot figure out why. What am I missing... ?

Community
  • 1
  • 1
beccoblu
  • 151
  • 7
  • 1
    You have an `sprintf` on a string which does nothing. [Cargo-cult programming](http://en.wikipedia.org/wiki/Cargo_cult_programming)? Or is this a relic of some other code where that's actually used? – tadman Sep 19 '13 at 17:45
  • The 2nd you said :) The starting query was a bit longer... (btw, fixed now) – beccoblu Sep 19 '13 at 17:46
  • You should just `DB::select("...")` rather than creating the intermediate and otherwise useless `$query` string. If you need to log your queries, the general query log on the server is much better anyway. – tadman Sep 19 '13 at 17:58
  • Yes, you are right. But the outputted JSON, as you can see, is ok, to me. What I need to get is _why_ the GMaps routine shows the `$testLoc` placeholder and ignores the automated ones... – beccoblu Sep 19 '13 at 18:03
  • 1
    You should post anything related to the "GMaps routine" when you want to get an answer. – Dr.Molle Sep 19 '13 at 21:59
  • 1
    I took your JSON insert it into the script that you have provided and all works fine. Dig deeper... – Makc Sep 20 '13 at 09:04

1 Answers1

0

You wrote that you're using another script and that the other script only shows the testlocations on google maps.

My guess is that you didn't update the other script to your needs, specifically my crystal ball is telling me that you still have this line in there:

setMarkers(locs);//Create markers from the initial dataset served with the document.

In your question you only showed the part which worked and I agree, but you only mentioned that "something else" isn't working. If you want an answer for that part, try reprashing your question and include the parts which cause you problems.

Stefan M
  • 868
  • 6
  • 17
  • Or you are trying to loop through the markers but are actually overwriting one over and over leaving just the last entry: the default – Robot Woods Sep 21 '13 at 01:04