简体   繁体   中英

Update map marker without refreshing page - Leaflet

I have a situation here. I am trying to show current position of a subject on online map using leaflet Javascript library. There are two pages of my site. Page-1 have the map display and Page-2 inputs the current position of the subject and sets the map accordingly. I am using a blue marker to show the subject location on the map. The marker is appearing correctly if I load the Page-1, but if I update the position on Page-2 then subject's position is not get updated on the map on Page-1.

Here is my Page-1 code:

<!DOCTYPE html>
<html>
<head>
<title></title>
<h1></h1>
<link rel="stylesheet" href="http://cdn.leafletjs.com/leaflet-0.5/leaflet.css" />
<link rel="stylesheet" href="CSS/mystyle.css" />
<!--[if lte IE 8]>
    <link rel="stylesheet" href="http://cdn.leafletjs.com/leaflet-0.5/leaflet.ie.css" />
<![endif]-->
<script src="http://cdn.leafletjs.com/leaflet-0.5/leaflet.js"></script>
<script src="SCRIPT/jquery-1.9.1.js"></script>
<script src="SCRIPT/myscript.js"></script>
</head>

<body onload="LoadMap()">
<h2>Current location:</h2>
<table border="1" id="currentloc_table">
    <tr>
        <th>Name</th>
        <th>IMEI</th>
        <th>Latitude</th>
        <th>Longitude</th>
    </tr>
    <tr>
        <td>Azeem HTC Device</td>
        <td>357710043411902</td>
        <td></td>
        <td></td>
    </tr>
</table>
<br/><br/>
<div id="map"></div>
</body>
</html>

Here is the code of myscript.js :

            var map;
            var marker;

            function LoadMap()
            {

                $.ajax({
                        url: "../Database/getcurrentlocation.php?deviceId=357710043411902",
                        success: function(data){
                            if(data!=null)
                            {
                                var dataArray = data.split(/~/);
                                SetMap(dataArray[0],dataArray[1]);
                            }
                         }
                      });


                setTimeout(function(){LoadMap();},10000);
            }

            function SetMap(lati,longi)
            {
                var loc_table = document.getElementById('currentloc_table');
                loc_table.rows[1].cells[2].innerHTML = lati;
                loc_table.rows[1].cells[3].innerHTML = longi;
                map = L.map('map').setView([lati, longi], 12);
                L.tileLayer('http://{s}.tile.cloudmade.com/XXXXXXXXXXXXXXXXXXXXXXXXXXXX/997/256/{z}/{x}/{y}.png', {
                attribution: '',
                maxZoom: 18
                }).addTo(map);
                marker = L.marker([lati, longi]).addTo(map);
            }

The map gets updated when I reload the page. But can i update the map with new position without performing page-1 refresh?

Thanks

just put

setTimeout(function(){LoadMap();},10000);

outside the LoadMap function

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