地图框绘图标记位置错误

Map Box plotting marker in wrong location

在下面的屏幕截图中,您可以看到标记为 Mother Earth Brewery 的标记,但它应该显示为 Dogfish Head:

我添加地图框标记的代码是:

var lat;
            var lng;

            var markers = [];
            var markerTemp;

            for (i = 0; i < data.length; i++) {
                lat = parseFloat(data[i].lat);
                lng = parseFloat(data[i].lng);


                if (lat == "" || lng == "") {

                } else {
                    L.marker([lat, lng]).addTo(map).bindPopup(data[i].brewery);
                    markerTemp = L.marker([lat, lng]);
                    markers.push(markerTemp);

                    alert("Brewery: " + data[i].brewery + "Lat: " + lat + "Long: " + lng );

                }
                var group = L.featureGroup(markers);

                map.fitBounds(group.getBounds());


            }

下面正在解析的变量数据是json:

[{"brewery":"East Coast Beer Company","lat":"40.091779","lng":"-74.049265","breweryID":"0BRjKa"},{"brewery":"Ballast Point Brewing Company","lat":"32.766785","lng":"-117.195246","breweryID":"1d90Lc"},{"brewery":"Fulton Brewing Company","lat":"44.9849423","lng":"-93.2790206","breweryID":"5GoGSi"},{"brewery":"Stone Brewing Co.","lat":"33.115682","lng":"-117.119552","breweryID":"709vEK"},{"brewery":"The Lost Abbey","lat":"33.141417","lng":"-117.150076","breweryID":"9x7wNn"},{"brewery":"Dogfish Head Craft Brewery","lat":"38.770622","lng":"-75.310893","breweryID":"g0jHqt"},{"brewery":"Latitude 33 Brewing Co.","lat":"33.1362843","lng":"-117.2244859","breweryID":"GpPbOw"},{"brewery":"Coronado Brewing Company","lat":"32.6977264","lng":"-117.1731838","breweryID":"gVynTw"},{"brewery":"Iron Fist Brewing Company","lat":"33.145823","lng":"-117.238546","breweryID":"hjb9rn"},{"brewery":"Heritage Brewing","lat":"38.7523173","lng":"-77.4887259","breweryID":"LWv6MU"},{"brewery":"Karl Strauss Brewing Company","lat":"32.832461","lng":"-117.2318045","breweryID":"mtUjck"},{"brewery":"Bagby Beer Company","lat":"33.189225","lng":"-117.374257","breweryID":"Nl2U0R"},{"brewery":"Mother Earth Brew Company","lat":"38.770622","lng":"-75.310893","breweryID":"NwFxny"},{"brewery":"Pizza Port Brewing Company","lat":"33.1596505","lng":"-117.3478111","breweryID":"QSVVxD"},{"brewery":"Burley Oak Craft Brewery","lat":"38.3338561","lng":"-75.2169099","breweryID":"rKXfsB"},{"brewery":"Belching Beaver Brewery","lat":"33.1450383","lng":"-117.2285575","breweryID":"rVzPCl"},{"brewery":"San Marcos Brewery & Grill","lat":"33.134539","lng":"-117.190813","breweryID":"SEibYT"},{"brewery":"Union Craft Brewing Company","lat":"39.331938","lng":"-76.641971","breweryID":"t6Gyij"},{"brewery":"Rinn Duin Brewing","lat":"40.123373","lng":"-74.24754","breweryID":"T90yOZ"},{"brewery":"Victory Brewing Company","lat":"40.0061038","lng":"-75.6941546","breweryID":"VoKbnS"},{"brewery":"Heavy Seas","lat":"39.230827","lng":"-76.6751309","breweryID":"wTFQaf"},{"brewery":"Booze Brothers Brewing Co.","lat":"33.148172","lng":"-117.218047","breweryID":"xaBLno"},{"brewery":"Kane Brewing","lat":"40.2368095","lng":"-74.0442545","breweryID":"XqWTM7"},{"brewery":"AleSmith Brewing Company","lat":"32.892034","lng":"-117.144379","breweryID":"ygAzC9"},{"brewery":"Bay City Brewing","lat":"32.7579949","lng":"-117.2118382","breweryID":"yOH4Du"}]

在代码中你可以看到我添加了这个alert("Brewery: " + data[i].brewery + "Lat: " + lat + "Long: " + lng );

确保正确的值进入

markerTemp = L.marker([lat, lng]);
markers.push(markerTemp);

我通过警报验证了正确的数据正在一个接一个地输入,并与 JSON 的输出进行比较。一切看起来都是正确的,我被卡住了,将不胜感激第二组眼睛。谢谢!

代码的最后两行应该在 for 循环之外。您打算将所有标记作为一个组添加到地图中,但实际上您所做的似乎在编程上是不正确的。

我注意到将浮点数与字符串进行比较不是一个好的编程习惯。

if (lat == "" || lng == "")

这里我对代码进行了相应的修改:

var lat;
var lng;

var markers = [];
var markerTemp;

for (i = 0; i < data.length; i++) {
    lat = parseFloat(data[i].lat);
    lng = parseFloat(data[i].lng);

    if (data[i].lat !== "" && data[i].lng !== "") {
        markerTemp = L.marker([lat, lng]).addTo(map).bindPopup(data[i].brewery);
        markers.push(markerTemp);

        console.log("Brewery: " + data[i].brewery + "Lat: " + lat + "Long: " + lng );
    }
}
var group = L.featureGroup(markers);
map.fitBounds(group.getBounds());