hellomello
hellomello

Reputation: 8597

jquery javascript google maps: getting coordinates

I have div with coordinates attribute:

    <div class="c" coordinates="32.100,-118.3232">
    ..
    </div>

I'm getting coordinates with the help of jquery:

    var coord = $(".c").attr("coordinates");

Do I need to convert this somehow when I need to put this into my google maps javascript:

    var latlng = new google.maps.LatLng(coord );
    addmarker(latlng)

function addmarker(latilongi) // function to add new marker when click on compare
{
    var marker = new google.maps.Marker({
        position: latilongi,
        title: 'new marker',
        draggable: true,
        map: map
    });
    map.setCenter(marker.getPosition())
}

It seems like it doesn't fall into the right place when it deploys onto my page,

Thanks

Upvotes: 0

Views: 1288

Answers (1)

Marcelo
Marcelo

Reputation: 9407

Provided that var coord = $(".c").attr("coordinates") actually works, you'd have the same as

var coord = "32.100,-118.3232"

That is a string, but the google.maps.LatLng() object takes 2 numerical parameters. So,

var coord = "32.100,-118.3232";
var arr = coord.split(',');
var latlon = new google.maps.LatLng(parseFloat(arr[0]),parseFloat(arr[1]));
addmarker(latlon);

Upvotes: 3

Related Questions