如何改变Mapbox的中心位置?



我试图添加标记值(lat,lng)到数据库(PHP/Wampserver)。使用Mapbox api,我想为地图打开的位置,我目前正在工作,但它不会改变。或者如果我可以把最新的标记放大也可以

my code:


<script>
var saved_markers = <?= get_saved_locations() ?>;
var user_location = [36.168438,9.889008];
mapboxgl.accessToken = 'pk.eyJ1IjoiZmFraHJhd3kiLCJhIjoiY2pscWs4OTNrMmd5ZTNra21iZmRvdTFkOCJ9.15TZ2NtGk_AtUvLd27-8xA';
var map = new mapboxgl.Map({
container: 'map',
style: 'mapbox://styles/mapbox/satellite-v9',
center: user_location,
zoom: 10,

});
//  geocoder here
var geocoder = new MapboxGeocoder({
accessToken: mapboxgl.accessToken,
// limit results to Australia
//country: 'IN',
});
var marker ;
// After the map style has loaded on the page, add a source layer and default
// styling for a single point.
map.on('load', function() {
addMarker(user_location,'load');
add_markers(saved_markers);
// Listen for the `result` event from the MapboxGeocoder that is triggered when a user
// makes a selection and add a symbol that matches the result.
geocoder.on('result', function(ev) {
alert("aaaaa");
console.log(ev.result.center);
});
});

使用您在这里发布的代码,地图应该以[36.168438,9.889008]为中心。

如果你想在加载标记后改变地图的中心,你可以调用map.setCenter(),将新坐标作为数组([lng, lat])传入。

https://docs.mapbox.com/mapbox-gl-js/api/map/地图# setcenter

最新更新