2013-02-27 63 views
0

我初始化使用下面的代码谷歌地图:在失败天气预报地图没有设置为提供的坐标

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'); 
    }); 
}); 
} 

我送这是一个地理编码地址和地图上的销标记的位置。但是,如果地理编码失败,则地图以中国为中心。因此,为了克服这个问题,我将地图中心重置为原始的lat长度并在其上绘制圆圈。当我调试代码时,我发现坐标设置正确,但地图仍然显示中国地图。

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

回答

1

你fenceAdd坐标默认为中国:

206省道,牟平,山东烟台,中国(37.21401,121.5955285)

如果你不希望地图中央有,改变这些坐标。