views:

833

answers:

3

I'm using google maps. In my code i've used setCenter() function. My problem is that marker is always located on top left corner of map area (not at the center). Please tell me how to resolve it?

My piece of code is

 lat = 46.437857;
        lon = -113.466797;

            marker = new GMarker(new GLatLng(lat, lon));


            var topRight = new GControlPosition(G_ANCHOR_TOP_RIGHT, new GSize(20, 40));
            map.addControl(new GLargeMapControl3D(), topRight);
            map.setCenter(new GLatLng(lat, lon), 5);

            map.addOverlay(marker);

alt text

A: 

in your code, at line

map.setCenter(new GLatLng(lat, lon), 5);

the setCenter method takes just one parameter, for the lat:long location. Why are you passing two parameters there ?

I suggest you should change it to,

map.setCenter(new GLatLng(lat, lon));
phoenix24
@phoenix24: `setCenter()` takes an optional zoom parameter, so there's nothing wrong with that: http://code.google.com/apis/maps/documentation/reference.html#GMap2.setCenter
Daniel Vassallo
oh! apologies; I'll edit my response above.
phoenix24
@Daniel I think the optional paramater has been deprecated in the GoogleMapsV3 APIs. http://code.google.com/apis/maps/documentation/v3/reference.html
phoenix24
@phoenix24: Yes, actually the entire API changed in V3. The OP is using v2 however. This can be deduced from the "classes" starting with the `G` prefix. The v3 uses the `google.maps` namespace instead.
Daniel Vassallo
A: 
 function resize() {
        var map_obj = document.getElementById("map_canvas");

      /*  map_obj.style.width = "500px";
        map_obj.style.height = "225px";*/
        if (map) {
            map.checkResize();
            map.panTo(new GLatLng(lat,lon));
        }
    }

<body onload="initialize()" onunload="GUnload()" onresize="resize()">
<div id="map_canvas" style="width: 100%; height: 100%">
</div>

hotcoder
A: 

I searched and searched and finally found that ie needs to know the map size. Set the map size to match the div size.

map = new GMap2(document.getElementById("map_canvas2"), { size: new GSize(850, 600) });