2013-12-18 120 views
0

自從我嘗試一次又一次找到合適的地方,但沒有任何運氣。 我找不到在哪裏放置map.panby (x , y)在我的下面的谷歌地圖代碼,以獲得更多的標記在右側的機器人。map.panby(x,y)在谷歌地圖代碼中放置的位置

如果有人可以幫助我,那將會很棒!

最佳

<script type="text/javascript"> 
     var geocoder; 
     var map; 
     function initialize() { 
     geocoder = new google.maps.Geocoder(); 
     var latlng = new google.maps.LatLng(52.5200066, 13.404954); 
     var mapOptions = { 
      zoom: 6, 
      scrollwheel: false, 
      navigationControl: false, 
      mapTypeControl: false, 
      scaleControl: false, 
      draggable: false, 
      zoomControl: true, 
      zoomControlOptions: { 
      style: google.maps.ZoomControlStyle.SMALL, 
      position: google.maps.ControlPosition.LEFT_CENTER 
     }, 
      streetViewControl:false, 
      center: latlng, 
      mapTypeId: google.maps.MapTypeId.ROADMAP, 
      styles: [ 
    { 
    "featureType": "administrative.country", 
    "elementType": "geometry", 
    "stylers": [ 
     { "weight": 1.2 }, 
     { "color": "#dd4b39" } 
    ] 
    },{ 
    "featureType": "administrative", 
    "stylers": [ 
     { "visibility": "simplified" } 
    ] 
    },{ 
    "featureType": "administrative.country", 
    "stylers": [ 
     { "visibility": "on" } 
    ] 
    },{ 
    "featureType": "administrative.locality", 
    "stylers": [ 
     { "visibility": "on" } 
    ] 
    } 
] 
} 

     map = new google.maps.Map(document.getElementById('user-map-canvas'), mapOptions); 
     } 

     function showAddress(address) { 
    geocoder.geocode({ 'address': address}, function(results, status) { 
     if (status == google.maps.GeocoderStatus.OK) { 
     map.setCenter(results[0].geometry.location); 
     var marker = new google.maps.Marker({ 
      map: map, 
      position: results[0].geometry.location 
     }); 
     } else { 
     alert("Adresse konnte aus folgendem Grund nicht gefunden werden: " + status); 
     } 



     google.maps.event.addListener(marker, 'click', function() { // Add a Click Listener to our marker 
    window.location='/freiwillige/?s=<?php echo $country ?>'; // URL to Link Marker to (i.e Google Places Listing) 
}); 
    }); 

    }  

    jQuery(document).ready(function() { initialize(); showAddress('<?php echo $address ?>'); }); 
    </script> 

回答

0

你應該能夠把panby在地圖上的實例化後的任意點,所以在這個例子中,你已經給:

map = new google.maps.Map(document.getElementById('user-map-canvas'), mapOptions); 
map.panby(x, y); 

應該很好地工作。

請注意,此功能平鋪地圖,它不會相對於瓷磚移動標記,我認爲這是你的意圖?

編輯您應該能夠在任何執行點發出呼叫。因此,例如,以抵消地圖略有您的地址解析請求後,你可以這樣做:

if (status == google.maps.GeocoderStatus.OK) { 
    map.setCenter(results[0].geometry.location); 
    var marker = new google.maps.Marker({ 
     map: map, 
     position: results[0].geometry.location 
    }); 
    map.panby(x, y); 
    } 

編輯好了,所以我已經修改了原來的代碼與我提到的更改。試試這個:

<script type="text/javascript"> 
    var geocoder; 
    var map; 
    function initialize() { 
    geocoder = new google.maps.Geocoder(); 
    var mapOptions = { 
     zoom: 6, 
     scrollwheel: false, 
     navigationControl: false, 
     mapTypeControl: false, 
     scaleControl: false, 
     draggable: false, 
     zoomControl: true, 
     zoomControlOptions: { 
      style: google.maps.ZoomControlStyle.SMALL, 
      position: google.maps.ControlPosition.LEFT_CENTER 
     }, 
     streetViewControl:false, 
     mapTypeId: google.maps.MapTypeId.ROADMAP, 
     styles: [ 
     { 
      "featureType": "administrative.country", 
      "elementType": "geometry", 
      "stylers": [ 
      { "weight": 1.2 }, 
      { "color": "#dd4b39" } 
      ] 
     },{ 
      "featureType": "administrative", 
      "stylers": [ 
      { "visibility": "simplified" } 
      ] 
     },{ 
      "featureType": "administrative.country", 
      "stylers": [ 
      { "visibility": "on" } 
      ] 
     },{ 
      "featureType": "administrative.locality", 
      "stylers": [ 
      { "visibility": "on" } 
      ] 
     } 
     ] 
    } 

    map = new google.maps.Map(document.getElementById('user-map-canvas'), mapOptions); 
    } 

    function showAddress(address) { 
     geocoder.geocode({ 'address': address}, function(results, status) { 
      if (status == google.maps.GeocoderStatus.OK) { 
       var marker = new google.maps.Marker({ 
        map: map, 
        position: results[0].geometry.location 
       }); 
       map.setCenter(results[0].geometry.location); 
       map.panby(x, y); 
      } 
      else 
      { 
       alert("Adresse konnte aus folgendem Grund nicht gefunden werden: " + status); 
      } 



      google.maps.event.addListener(marker, 'click', function() { // Add a Click Listener to our marker 
       window.location='/freiwillige/?s=<?php echo $country ?>'; // URL to Link Marker to (i.e Google Places Listing) 
      }); 
     }); 

    }  

    jQuery(document).ready(function() { initialize(); showAddress('<?php echo $address ?>'); }); 
</script> 

我已經刪除了地圖的原始中心(和可變經緯度,你不使用任何其他地方),然後爲中心,地址解析請求完成後偏移。手指。交叉。

編輯查看答案Here如果panby仍然不能正常工作,但我看不到任何代碼問題。

+0

哇!這讓我更進一步!謝謝...... :) 但現在地圖集中在這裏定義的點:'var latlng = new google.maps.LatLng(52.5200066,13.404954);'而不是標記。 有沒有辦法解決這個問題呢? – Philipp

+0

您是否希望地圖在初始化時或在地理編碼請求之後居中於標記? – Swires

+0

嗯,我只是想達到它加載頁面時標記的中心,但標記應該稍微偏右。 ;) 非常感謝,這會讓我瘋狂! – Philipp