Search code examples
javascriptgoogle-mapsgoogle-maps-markersgoogle-fusion-tables

Google Marker Position Option: retrieving Latitude and Longitude from Google Fusion Table


I have a column called 'xy' in Google Fusion Table. Values are exactly in this format in each cell: 30.319067,-97.739454

I have created an array of these values, var locationList = []. Trying to use this array and retrieve the lat, long data to put in for the position option, but it's not recognizing it. I have an alert function below, to see what's being passed, and it alerts (NaN, NaN).

for (var i = 0; i < locationList.length ; i++){
    if (obj.id == i){
        alert(locationList[0]);

        marker = new google.maps.Marker({
            map:Map,
            position: new google.maps.LatLng(locationList[i]),
            animation: google.maps.Animation.DROP,
            zoom: 0});

        break;}


}

Can anybody tell me what's going on with these values being passed? Is it the format problem...? Data type problem?

I also have the 'Geometry' column in my Fusion Table in this format:

<Point><coordinates>-97.739454,30.319067,0.0</coordinates></Point>

Not sure which column suits the best for the Marker position. Whichever the column, I just want to see that marker bouncing!

Thanks for your help!


I have created two different columns for latitude and longitude and they are in the number data type in my Fusion Table. The data are in arrays var latitudeList = []; and var longitudeList = [];.

for (var i = 0; i < numRow ; i++){
    if (obj.id == i){
        alert(latitudeList[3]);
        alert(nameList[i]);

        var LatLng = new google.maps.LatLng(parseFloat(latitudeList[i]),parseFloat(longitudeList[i]));

        marker = new google.maps.Marker({
            map:PuppyMap,
            position: LatLng,
            animation: google.maps.Animation.DROP,
            zoom: 0});

        alert([i]); break;}
};

Now I'm again using the alert to see what's going on, and alert(latitudeList[0]); alerts (30.23423, NaN), with parenthesis, I don't understand. Each cell contains the latitude values as 30.xxxxx as numbers, and they should be in the array that way. And alert(parseFloat(latitudeList[0]),parseFloat(longitudeList[0])); alerts NaN.

I guess I'm not sure what NaN is,,,


Solution

  • a google.maps.LatLng object takes two numbers as its arguments, not a string containing two numbers separated by a comma. This is not correct:

        marker = new google.maps.Marker({
            map:Map,
            position: new google.maps.LatLng(locationList[i]),
            animation: google.maps.Animation.DROP,
            zoom: 0});
    

    should be

        var coords = locationList[i].split(',');
        var latlng = new google.maps.LatLng(coords[0], coords[1]);
        marker = new google.maps.Marker({
            map:Map,
            position: latlng,
            animation: google.maps.Animation.DROP,
            zoom: 0});
    

    and safer to be:

        var coords = locationList[i].split(',');
        var latlng = new google.maps.LatLng(parseFloat(coords[0]), parseFloat(coords[1]));
        marker = new google.maps.Marker({
            map:Map,
            position: latlng,
            animation: google.maps.Animation.DROP,
            zoom: 0});;
    

    as coming from the FusionTable it is a string. You could even verify they are valid numbers, by testing "isNaN" before using them to construct the google.maps.LatLng.