2017-02-12 114 views
0

我試圖讓這個design 我的代碼不工作,我期待.. 如果舊的寬度比新的寬度更大,偏移中心-50 如果老寬度比新的寬度和偏移中心50谷歌地圖的變化offsetCenter

偏移中心工作同列,如果 但是這意味着,如果用戶調整窗口偏移中心將是50或-50每當用戶調整

the rectangl在地圖上ES是用於覆蓋名稱僅 HTML

CSS

#map{ 
    width: 100%; 
    height: 400px; 
} 

JS

<script async defer 
src="https://maps.googleapis.com/maps/api/js?key=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx&callback=initMap"> 
</script> 


<script> 
function oh_ow() { 
     var oh = $(window).height(); 
     var ow = $(window).width(); 
     return ow; 
}  
    var old_w = oh_ow() 
    alert(old_w); 

     alert(ow); 
    function initMap() { 
    var uluru = {lat: 62.26393, lng: 82.386004}; 
    var map = new google.maps.Map(document.getElementById('map'), { 
    zoom: 16, 
    center: uluru 
    }); 
    var marker = new google.maps.Marker({ 
    position: uluru, 
    map: map 
    }); 



function offsetCenter(latlng, offsetx, offsety) { 

    // latlng is the apparent centre-point 
    // offsetx is the distance you want that point to move to the right, in pixels 
    // offsety is the distance you want that point to move upwards, in pixels 
    // offset can be negative 
    // offsetx and offsety are both optional 

    var scale = Math.pow(2, map.getZoom()); 

    var worldCoordinateCenter = map.getProjection().fromLatLngToPoint(latlng); 
    var pixelOffset = new google.maps.Point((offsetx/scale) || 0,(offsety/scale) ||0); 

    var worldCoordinateNewCenter = new google.maps.Point(
     worldCoordinateCenter.x - pixelOffset.x, 
     worldCoordinateCenter.y + pixelOffset.y 
    ); 

    var newCenter = map.getProjection().fromPointToLatLng(worldCoordinateNewCenter); 

    map.setCenter(newCenter); 

} 

google.maps.event.addDomListener(window, 'resize', function() { 

//---------------------------------------------  
var rtime; 
var timeout = false; 
var delta = 200; 
$(window).resize(function() { 
    rtime = new Date(); 
    if (timeout === false) { 
     timeout = true; 

     setTimeout(resizeend, delta); 
    } 
}); 
function resizeend() { 
    if (new Date() - rtime < delta) { 
     setTimeout(resizeend, delta); 
    } else { 
     timeout = false; 
      var nh = $(window).height(); 
      var nw = $(window).width(); 
      //alert("old"+old_w); 
      //alert("new"+nw); 
     if(nw > old_w){ 
      offsetCenter(map.getCenter(),50,-0); 
     }else if(nw < old_w){ 
      offsetCenter(map.getCenter(),-50,-0); 
     } 


    }    
}  
//---------------------------------------------  
}); 





} 



</script> 

什麼,我不會是How to offset the center point in Google maps api V3

一個負責任的處女請某人幫助

+2

您的要求不明確。您是否試圖鎖定地理位置'{lat:25.26393,lng:55.386004}'? – Diode

+0

我不會將標記放在頁面加載時的右側,並且如果調整大小 –

+0

響應處女http://stackoverflow.com/questions/10656743/how-to-offset-the-center-point-in- google-maps-api-v3 @ Diode –

回答

0

從我的理解,我認爲這是可以用setCenter,panTopanBy方法實現的。

function initMap() { 
    var uluru = { 
    lat: 25.26393, 
    lng: 55.386004 
    }; 
    var map = new google.maps.Map(document.getElementById('map'), { 
    zoom: 16, 
    center: uluru 
    }); 
    var marker = new google.maps.Marker({ 
    position: uluru, 
    map: map 
    }); 

    var winWidth = $(window).width(); 
    panMap(); 

    $(window).resize(function() { 
    map.setCenter(uluru); 
    panMap(); 
    }); 

    google.maps.event.addListener(map, 'zoom_changed', function() { 
    map.panTo(uluru); 
    panMap(); 
    }); 

    function panMap(xChange){ 
    var winWidth = $(window).width(); 
    if(winWidth > 671){ 
     map.panBy(xChange || -170, 0); 
    } 
    } 

} 

您可以調整-170以獲得所需的位置。

此處演示:https://jsbin.com/fasawofofa/1/edit?html,output

+0

不錯,但如果頁面寬度低於671px,如何將其設置爲居中; @Diode –

+0

我已經爲此添加了條件。請看新的答案 – Diode