【问题标题】:How do you recenter mapbox js after pushing button?按下按钮后如何重新定位 mapbox js?
【发布时间】:2015-10-14 17:54:27
【问题描述】:

因此,使用 googlemaps api 在按下按钮后重新定位新城市的地图非常简单,但我需要使用来自 MapBox 的自定义地图项目。不幸的是,他们的文档没有帮助。

var map;
function initialize() {
  map = new google.maps.Map(document.getElementById('map-canvas'), {
    center: new google.maps.LatLng(48.1293954,11.556663), // Munich Germany
    zoom: 10
  });
}

function newLocation(newLat,newLng) {
	map.setCenter({
		lat : newLat,
		lng : newLng
	});
}

google.maps.event.addDomListener(window, 'load', initialize);
    </script>
 <script>
$(document).ready(function () {
     $("#1").on('click', function () {
	  newLocation(48.1293954,11.556663);
	});
	 $("#2").on('click', function () {
	  newLocation(40.7033127,-73.979681);
	});
   $("#3").on('click', function () {
	  newLocation(55.749792,37.632495);
	});
});

此代码来自另一个帖子:How to change Google Map Center by clicking a button

我的问题是您如何使用 ma​​pbox.jsleaflet api 做到这一点?我尝试过的一切都失败了。

【问题讨论】:

    标签: javascript jquery google-maps mapbox


    【解决方案1】:

    实际上,我想通了。您可以使用 Leaflet 的 L.LatLng 为坐标添加一个新的运算符。让我失望的是 .setView() 函数。

    var map;
    
    function initialize() {
        var coordinates = new L.LatLng(40.673859, -73.970114);
        map = new L.mapbox.map('map')
            .setView(coordinates, 12)
            .addLayer(L.mapbox.tileLayer('myMapID'));
      
    }
    
    function newLocation(newLat,newLng,newZoom) {
        var newCoord = new L.LatLng(newLat,newLng)
        map.setView(newCoord, newZoom);
    }
      
    window.addEventListener('load',initialize,false);
    
    
    $(document).ready(function() {
        $("#1").on('click', function () {
            newLocation(40.765172,-73.978844,12);
        });
        $("#2").on('click', function () {
            newLocation(35,-75,4);
        });
        $("#3").on('click', function () {
            newLocation(15,-75,4);
        });
    
    });
    
      

    【讨论】:

      【解决方案2】:

      我认为你应该改变:

      map.setCenter({
          lat : newLat,
          lng : newLng
      });
      

      到:

      map.setCenter(new google.maps.LatLng(newLat, newLng));
      

      【讨论】:

        猜你喜欢
        • 2015-04-06
        • 2013-06-21
        • 2019-08-28
        • 1970-01-01
        • 2018-09-30
        • 2015-10-07
        • 1970-01-01
        • 1970-01-01
        • 2015-08-09
        相关资源
        最近更新 更多