简体   繁体   中英

To center a leaflet map on a marker from a link or button

I need to be able to center the leaflet map on a marker when the appropriate button is click.

<button onclick='getLoc(" + geojson.features[i].geometry.coordinates[0] + "," +     geojson.features[i].geometry.coordinates[1] + ")'>View</button>



function getLoc(c){
var c = L.GeoJSON([lng,lat]);
}

I'm stuck from here, any help to finish this would be greatly appreciated. Or if anyone else has a different way of doing it using JSON.

I think what you're looking for is the setView method. Assuming that your map variable is called map , you can set the lat/lng of the map by calling the method like this:

map.setView([51.505, -0.09], 13); // ([lat, lng], zoom)

I managed to solve this problem

Here is the solution

<button onclick='getLoc(" + geojson.features[i].geometry.coordinates[0] + "," +     geojson.features[i].geometry.coordinates[1] + ")'>View</button>


function getLoc(lat, lng){
    map.setView(new L.LatLng(lat, lng), 12);
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM