简体   繁体   中英

leaflet cannot create marker

i'm new in web development, i have problem when using leaflet and codeigniter. The problem is i cannot create marker. Here is the code:

         <html>
    <head>
        <link rel="stylesheet" href="<?php echo base_url();?>dist/css/leaflet.css" />
        <link rel="stylesheet" href="<?php echo base_url();?>dist/css/main.css"/>
    </head>
    <body>
        <div id="map"></div>

    </body> </html>



<script src="<?php echo base_url();?>dist/js/leaflet.js"></script> <script src="<?php echo base_url();?>dist/js/jquery-1.11.0.js"></script>

<script>
    var map = L.map('map').setView([51.505, -0.09], 13);
    function createMarker(lat,long){


        var marker = L.marker([lat,long]).addTo(map);
    }



    $(function(){

       L.tileLayer( 'http://{s}.mqcdn.com/tiles/1.0.0/map/{z}/{x}/{y}.png', {           attribution: '&copy; <a href="http://osm.org/copyright" title="OpenStreetMap" target="_blank">OpenStreetMap</a> contributors | Tiles Courtesy of <a href="http://www.mapquest.com/" title="MapQuest" target="_blank">MapQuest</a> <img src="http://developer.mapquest.com/content/osm/mq_logo.png" width="16" height="16">',            subdomains: ['otile1','otile2','otile3','otile4']           }).addTo( map );
        $.getJSON('index.php/map/getData',function(data){

            debugger;
            for(i=0;i<3;i++)
            {
                var latitude = parseFloat(data[i].latitude);
                var longitude = parseFloat(data[i].longitude);
                createMarker(latitude,longitude);    
            }

        });
    }); </script>

here is the retrieved data inside getjson:

> [
Object
hasil_uji_id: 1
latitude: 51.13
longitude: -0.07
nama_hasil_uji: "iqbal 3"
__proto__: Object
, 
Object
hasil_uji_id: 123
latitude: 51.05
longitude: -0.09
nama_hasil_uji: "iqbal"
__proto__: Object
, 
Object
hasil_uji_id: 1234
latitude: 51.09
longitude: -0.08
nama_hasil_uji: "iqbal 2"
__proto__: Object
]

however, when i hardcoded the value of latitude and longitude, the marker shows up. please kindly enlighten me leaflet guru

My mistake, a really stupid one, shame on me. The code did work, the markers dont show because the latitude and longitude is too far away from the setView

When i zoom out, they did appear.

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