Google Maps setCenter()

前端 未结 5 1344
予麋鹿
予麋鹿 2021-01-03 19:07

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 te

相关标签:
5条回答
  • 2021-01-03 19:10

    @phoenix24 answer actually helped me (whose own asnwer did not solve my problem btw). The correct arguments for setCenter is

    map.setCenter({lat:LAT_VALUE, lng:LONG_VALUE});
    

    Google Documentation

    By the way if your variable are lat and lng the following code will work

    map.setCenter({lat:lat, lng:lng});
    

    This actually solved my very intricate problem so I thought I will post it here.

    0 讨论(0)
  • 2021-01-03 19:11

    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));
    
    0 讨论(0)
  • 2021-01-03 19:14

    For me above solutions didn't work then I tried

    map.setCenter(new google.maps.LatLng(lat, lng));
    

    and it worked as expected.

    0 讨论(0)
  • 2021-01-03 19:16

    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) });
    
    <div id="map_canvas2" style="width: 850px; height: 600px">
    </div>
    
    0 讨论(0)
  • 2021-01-03 19:30
     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>
    

    0 讨论(0)
提交回复
热议问题