0

我正在使用以下代码初始化 Google 地图:

function googleMapinitialize(fenceAddIn){
var fenceAdd=new google.maps.LatLng(37.2146,121.5545);
var mapProp = {
        center:fenceAdd,
        zoom:30,
        mapTypeId:google.maps.MapTypeId.TERRAIN
};

var map = new google.maps.Map(document.getElementById("fenceMap"),mapProp);

var geocoder = new google.maps.Geocoder();
var location = fenceAddIn; //"2001 Gateway PI, San Jose, CA";

if(!geocoder) {
    geocoder = new google.maps.Geocoder();
}

var geocoderRequest = {
    address: location
}

var myCity = null;
var marker = null;
var infowindow = null;
geocoder.geocode(geocoderRequest, function(results, status) {
    if (status =="" || status == google.maps.GeocoderStatus.ZERO_RESULTS){
        alert("'" + location + "' not found!!!");
        map.setCenter(fenceAdd);
    } else if (status == google.maps.GeocoderStatus.OK) {
        map.setCenter(results[0].geometry.location);
        fenceAdd = new google.maps.LatLng(results[0].geometry.location.hb,results[0].geometry.location.ib);
        if (!marker) {
            marker = new google.maps.Marker({
                map: map,
                raiseOnDrag:false,
                draggable:true
            });
            marker.setPosition(results[0].geometry.location);
            google.maps.event.addListener(marker,'click',function(){
                if (!infowindow) {
                  infowindow = new google.maps.InfoWindow({
                      disableAutoPan:true,
                      maxWidth:100
                  });
                }
                var content = '<strong>' + results[0].formatted_address + '</strong>';
                infowindow.setContent(content);
                infowindow.setPosition(results[0].geometry.location);
                infowindow.open(map, marker);                   
            });

        }
    }
    myCity = new google.maps.Circle({
        center:fenceAdd,
        radius:125,
        strokeColor:"#0000ff",
        strokeOpacity:0.1,
        strokeWeight:0.1,
        fillColor:"#0000ff",
        fillOpacity:0.20,
        map:map
    });

    google.maps.event.addListener(map, 'click', function(){
        infowindow.close();
    });
    google.maps.event.addListener(map, 'dblclick', function(){
        window.open("<%=request.getContextPath() %>/jsp/googleMapPopup.jsp?fenceAddress="+$('#haddr').val(),'ADDRESSMAP','height=400,width=600');
    });
});
}

我正在发送一个经过地理编码的地址,并在地图上用大头针标记位置。但如果地理编码失败,地图将以中国为中心。因此,为了克服这个问题,我将地图中心重置为原始纬度并在其上绘制圆圈。当我调试代码时,我看到坐标设置正确,但地图仍然显示中国地图。

有人能告诉我在这种情况下如何重置地图位置吗?

4

1 回答 1

1

你的fenceAdd坐标默认为中国:

山东省烟台市牟平省道206号(37.21401、121.5955285)

如果您不希望地图居中,请更改这些坐标。

于 2013-02-27T16:43:23.617 回答