我一直在尝试找到一种方法来设置放针以将焦点放在地图的中心,或者让地图进行调整以将放针的焦点集中在地图的中心。从下图可以看到,下降针显示在屏幕的左上角。以下是我当前正在处理的代码片段。
我目前正在使用leaftlet.js提供交互式地图
$(document).ready(function() {
var map = L.map('mapid').setView([14.5906216, 120.9799696], 12);
L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
attribution: '© <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors'
}).addTo(map);
let geocoder = new L.Control.Geocoder.Nominatim();
var location = $('.location').text();
geocoder.geocode(location, function(results) {
var latLng = new L.LatLng(results[0].center.lat, results[0].center.lng);
var marker = new L.Marker (latLng);
var geocodeLocation = Object.values(latLng);
// console.log(geocodeLocation);
L.marker(geocodeLocation).addTo(map)
.bindPopup('A pretty CSS3 popup.<br> Easily customizable.')
.openPopup()
});
})
<link rel="stylesheet" href="https://unpkg.com/leaflet@1.5.1/dist/leaflet.css"
integrity="sha512-xwE/Az9zrjBIphAcBb3F6JVqxf46+CDLwfLMHloNu6KEQCAWi6HcDUbeOfBIptF7tcCzusKFjFw2yuvEpDL9wQ=="
crossorigin=""/>
<link rel="stylesheet" href="styles.css" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script src="https://unpkg.com/leaflet@1.5.1/dist/leaflet.js"
integrity="sha512-GffPMF3RvMeYyc1LWMHtK8EbPv0iNZ8/oTtHPx9/cc2ILxQ+u905qIwdpULaqDkyBKgOaB57QTMg7ztg8Jm2Og=="
crossorigin=""></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/perliedman-leaflet-control-geocoder/1.9.0/Control.Geocoder.js"></script>
<script src="script.js"></script>
<body>
<div id="mapid"></div>
<span class="location">New York</span>
</body>