1

I am trying to create a custom google map using Longitudes and Latitudes that I have in my sql database. So I am trying to place these results directly into my jquery code for the geocoder. But it is not working. Does anyone have ANY idea how I could do this?? I have looked up everything possible and can not find an answer. Thank you so much in advance. Here is my code:

<script type='text/javascript'>//<![CDATA[ 

var map;
var global_markers = []; 
[<?php 
include 'admin/dbconn.php';

$getLoc = mysql_query("SELECT * FROM EanHotels WHERE City = 'Acapulco'") or die(mysql_error());
while($row = mysql_fetch_array($getLoc)){
    $name = $row['name'];
    $lat = $row['Latitud'];
    $lon = $row['Longitude'];
    $address = $row['Address1'];
    echo $lat;
    echo $lon;

?>   
var markers = [[<?php echo $lat ?>, <?php echo $lon ?>, '<?php echo $name ?>'], <?php } ?>
];


var infowindow = new google.maps.InfoWindow({});

function initialize() {
    geocoder = new google.maps.Geocoder();
    var latlng = new google.maps.LatLng(40.77627, -73.910965);
    var myOptions = {
        zoom: 1,
        center: latlng,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    }
    map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
    addMarker();
}

function addMarker() {
    for (var i = 0; i < markers.length; i++) {
        // obtain the attribues of each marker
        var lat = parseFloat(markers[i][0]);
        var lng = parseFloat(markers[i][1]);
        var trailhead_name = markers[i][2];

        var myLatlng = new google.maps.LatLng(lat, lng);

        var contentString = "<html><body><div><p><h2>" + trailhead_name + "</h2></p></div></body></html>";

        var marker = new google.maps.Marker({
            position: myLatlng,
            map: map,
            title: "Coordinates: " + lat + " , " + lng + " | Trailhead name: " + trailhead_name
        });

        marker['infowindow'] = contentString;

        global_markers[i] = marker;

        google.maps.event.addListener(global_markers[i], 'click', function() {
            infowindow.setContent(this['infowindow']);
            infowindow.open(map, this);
        });
    }
}

window.onload = initialize;
//]]>  

</script>
0

1 Answer 1

1

Have you checked the output of this script on the markers variable? This seems to have something with the formatted number output.

JavaScript expects numbers in the international format, with periods as the decimal separator: 10.2

In some places the server is configured to use comma as the decimal separators, so the output of your script would return an array of numbers, not a number with decimal places:

[[ 1,00012, 1,45678, 'name' ]]

Also, you have an extra comma at the end of the markers variable declaration, before the closing square bracket ]

Sign up to request clarification or add additional context in comments.

1 Comment

I did not think of seeing the results... I feel like a bit stupid but that was it! I figured it out! Thanks

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.