6

In my database I have lat/lng values and I'm trying to display markers on a google map. (I have my map coming up ok but can't get the markers going!).

 // Selects all the rows in the tester table.
$query = 'SELECT * FROM tester WHERE 1';
$result = mysql_query($query);

if (!$result) 
{
die('Invalid query: ' . mysql_error());
}

    while ($row = mysql_fetch_assoc($result))
    {?>
       var marker = new google.maps.Marker({
    position: <?php $row['lat']?>, <?php $row['lng']?> ,
    map: map,
    title:"Hello World!"
});   

}                

Any help would be great thanks!

2 Answers 2

1

I think you need to use "echo":

position: <?php echo $row['lat']?>, <?php echo $row['lng']?> ,
Sign up to request clarification or add additional context in comments.

Comments

1

It gets quite complex when you start writing javascript out from PHP as its very easy to get lost in all the semi-colons.

Also I find it easier to do it all in PHP rather than keep jumping in and out of HTML-PHP-HTML-PHP etc

Try this

// Selects all the rows in the tester table.
$query = 'SELECT * FROM tester WHERE 1';
$result = mysql_query($query);

if (!$result) {
    die('Invalid query: ' . mysql_error());
}

echo '<script type="text/javascript">';

$i = 0;
while ($row = mysql_fetch_assoc($result))
    echo 'var myLatlng = new google.maps.LatLng(' . $row['lat'] ',' . $row['lng'] . ');';

    echo 'var marker' . $i . ' = new google.maps.Marker({';
    echo '  position: myLatLng ,';
    echo '  map: map,';
    echo '  title:"Hello World!"';
    echo '});';
    echo 'marker' . $i . 'setMap(map);';
    $i++;
}               


echo '<script>';

2 Comments

Gave that a go and still no luck! when I remove the php from initialize() function the map loads ok, but with it added in the map does not appear at all!
What initialize() you did not post anything about initialize()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.