0

以下のスクリーン ショットでは、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);

アラートを介して正しいデータが 1 つずつ入力されていることを確認し、JSON の出力と比較しました。すべてが正しいように見えますが、私は立ち往生しています.2番目の目のセットをいただければ幸いです. ありがとう!

4

1 に答える 1

1

コードの最後の 2 行は、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());
于 2016-12-30T05:19:30.020 に答える