Google地图标记未居中

时间:2012-09-12 10:29:17

标签: javascript google-maps google-maps-markers google-maps-api-2

我正在使用谷歌地图显示隐藏的div,在点击标签时切换,我有两个问题,也许它们是相关的:

  1. 地图未完全显示
  2. 标记未在地图中居中,它位于地图的左上角
  3. 我正在粘贴代码,请指导我错误的地方或任何建议。

        var map = new GMap2(document.getElementById(mapid));
        if( maptype == 'G_NORMAL_MAP' )
        {
    
            map.setCenter(new GLatLng(startLatitude,startLongitude ), 6);
            map.addControl(new GLargeMapControl());     
        }
        else if( maptype == 'G_SATELLITE_MAP' )
        {
            map.setCenter(new GLatLng(startLatitude,startLongitude ), 18);
            map.addControl(new GLargeMapControl());
            map.setMapType(G_SATELLITE_MAP);
            map.setUIToDefault();
            map.enableRotation();
        }
        else if( maptype == 'G_AERIAL_MAP' )
        {
            map.setCenter(new GLatLng(startLatitude,startLongitude ), 18);
            map.addControl(new GLargeMapControl());
            map.setMapType(G_AERIAL_MAP);
            map.setUIToDefault();
            map.enableRotation();
        }
        else if( maptype == 'G_AERIAL_HYBRID_MAP' )
        {
            map.setCenter(new GLatLng(startLatitude,startLongitude ), 18);
            map.addControl(new GLargeMapControl());
            map.setMapType(G_AERIAL_HYBRID_MAP);
            map.setUIToDefault();
            map.enableRotation();
        }
    
        // Add 10 markers to the map at random locations
        var bounds = map.getBounds();
        var southWest = bounds.getSouthWest();
        var northEast = bounds.getNorthEast();
        var lngSpan = northEast.lng() - southWest.lng();
        var latSpan = northEast.lat() - southWest.lat();
        var point = new GLatLng(startLatitude, startLongitude);
        map.setCenter(bounds.getCenter());
    
        map.addOverlay(new GMarker(point));
    

    screenshot of the problem

4 个答案:

答案 0 :(得分:0)

尝试为标记添加position:map.getCenter(),以便您的标记在地图上居中

答案 1 :(得分:0)

完全定义map div的大小,或者使用构造函数的可选“size”参数:

map = new GMap2(document.getElementById(mapid),{size:new GSize(510,200)});

打开选项卡

后在地图对象上运行checkResize()方法

答案 2 :(得分:0)

尝试添加

var zoom = map.getZoom();
var center = map.getCenter();
map.checkResize();
map.setCenter(center,zoom);
切换地图div后

答案 3 :(得分:0)

此代码每次都会标记带有标记中心的Google地图。

        var lattitude = data.getLatitude ;
        var longitude = data.getLongitude;
        var pointLatLong = new google.maps.LatLng(lattitude,longitude);
        var pointOptions = {
            zoom: 10,
            center: pointLatLong,
            mapTypeId: google.maps.MapTypeId.ROADMAP
        }
        var map = new google.maps.Map(document.getElementById("map-canvas"), pointOptions);
        var pointMarker = new google.maps.Marker({
            position: pointLatLong,
            title: ""
        });
        pointMarker.setMap(map);
        google.maps.event.addListenerOnce(map, "idle", function () {
            google.maps.event.trigger(map, "resize");
            map.setCenter( pointLatLong );
        });

参考:http://www.onlinekurukshetra.com/google-map-not-loading-completely/