Converting code to use API v3 from API v2.

107 views
Skip to first unread message

Adam Tallon

unread,
Dec 30, 2011, 4:59:43 AM12/30/11
to google-map...@googlegroups.com

To date I've been using the below code with API v2 of Google maps, I'm trying to use the new API v3 but can't seem to change my code to work with the API v3, can onyone point me in the rigth direction, I've spent hours looking through posts and the documentation!?

Any help greatly appreciated.
 
<script type="text/javascript">
    //<![CDATA[
    function createMarker(point, name)
    {
        var marker = new GMarker(point, {title:name});
        return marker;
    }

    function mapclick(ov,pt)
    {
        if (marker) map.removeOverlay(marker);
        if (pt) { marker = createMarker(pt,pt.toUrlValue());
                map.addOverlay(marker);
                }
        var matchll = /\(([-.\d]*), ([-.\d]*)/.exec(pt);
        if (matchll)
        {
            var lat = parseFloat( matchll[1] );
            var lon = parseFloat( matchll[2] );
            lat = lat.toFixed(6);
            lon = lon.toFixed(6);
        }
        document.getElementById("lat").value = lat;
        if(document.getElementById("lat").value == "undefined")
        {
            document.getElementById("lat").value = "";
        }
        document.getElementById("lon").value = lon;
        if(document.getElementById("lon").value == "undefined")
        {
            document.getElementById("lon").value = "";
        }
    }

    if (GBrowserIsCompatible())
    {
        var map=new GMap2(document.getElementById("map"));
        map.setCenter(new GLatLng(53.558584,-7.987061), 6);
        map.addControl(new GLargeMapControl());
        var marker = null;
        GEvent.addListener(map,\'click\',function(overlay,point){mapclick(overlay,point)});
    }
    //]]>
</script>

Adam

unread,
Dec 30, 2011, 6:49:58 AM12/30/11
to google-map...@googlegroups.com
Got the code 99% working since the original post, my issue is now regarding the loading of a marker if one is already set, I have an if statement that says if the lat and lon variable are not set then output 'var marker = null;' but if lat and lon are set then output the below code:

var marker = new google.maps.Marker({ position: (53.558584,-7.987061), map: map });

I'm getting the below error and can't seem to figure it out, is it the format that position is in is incorrect!?

Invalid value for property <position>: -7.987061



Pil

unread,
Dec 30, 2011, 7:04:20 AM12/30/11
to Google Maps JavaScript API v3
The position property expects a LatLng object.
Try

var marker = new google.maps.Marker({
position: new google.maps.LatLng(53.558584,-7.987061),
map: map
});

You're using the marker variable twice: as local inside the
createMarker() function and as global inside the function mapclick().
That's possible but isn't that confusing?

Andrew Leach

unread,
Dec 30, 2011, 7:04:47 AM12/30/11
to google-map...@googlegroups.com
On 30 December 2011 11:49, Adam <adamt...@gmail.com> wrote:
>
> I'm getting the below error and can't seem to figure it out, is it the
> format that position is in is incorrect!?

The documentation says

position LatLng Marker position. Required.

So it needs to be a LatLng, not a couple of numbers.

http://code.google.com/apis/maps/documentation/javascript/reference.html#LatLng

var marker = new google.maps.Marker({

Reply all
Reply to author
Forward
0 new messages