简体   繁体   中英

leaflet map doesn't show all the marker when one of the marker is null longitude and latitude

I recently get a task from my School, I made a simple map using leaflet, but I have a problem, the marker from my map doesn't show, when one of the markers get null on longitude and latitude from the database, I Want the other marker from my map still work, even one of the marker get null longitude and latitude:) this is some of my code:

            while($row = mysqli_fetch_assoc($data)) {
            if($row['latitude'] == 0 ){

                // what should i do here, ????
                // var_dump('place_name');

            }else{

            // add marker to map
            $marker .= 'var marker = L.circle(['.$row['latitude'].', '.$row['longitude'].'],
            {
                color: "red",
                fillColor: "#f03",
                fillOpacity: 0.5,
                radius: 2000
            }
            ).addTo(map).bindPopup("'.$row['place_name'].'").openPopup().on("mouseover", function (e) {
                this.openPopup();
            }).on("mouseout", function (e) {
                this.closePopup();
            });
            ;
            ';
        }

    }

can someone get me some advice to solve this problem?, thanks for read this:)

you can try something like this to skip null latitude and longitude:

while ($row = mysqli_fetch_assoc($data)) {
    $latitude = (float)$row['latitude'];
    $longitude = (float)$row['longitude'];

    if ($latitude === 0 && $longitude === 0) {
        continue;
        // what should i do here, ????
        // var_dump('place_name');
    }

    // add marker to map
    $marker .= sprintf('var marker = L.circle([ %s, %s],
            {
                color: "red",
                fillColor: "#f03",
                fillOpacity: 0.5,
                radius: 2000
            }
            ).addTo(map).bindPopup("%s").openPopup().on("mouseover", function (e) {
                this.openPopup();
            }).on("mouseout", function (e) {
                this.closePopup();
            });
            ;
            ', $latitude, $longitude, $row['place_name']);
}

:)

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