Geolocation: moving only google maps marker without reload the map

后端 未结 2 1212
执笔经年
执笔经年 2020-12-28 23:34

I need to update only the marker when the device is moving or when the device is getting more accuracy. When the position change also reload the map and I need to move only

相关标签:
2条回答
  • 2020-12-29 00:20

    one easy way to do is, simply call map.clearOverlays(); then read add new positions with map.addOverlay(<marker>)

    0 讨论(0)
  • 2020-12-29 00:32

    I believe that the problem is that you create a new map inside the watchPosition function. You only need to create one marker and then update its position inside the watchPosition function.

    navigator.geolocation.watchPosition(
        function (position) {
            setMarkerPosition(
                currentPositionMarker,
                position
            );
        });
    
    function setMarkerPosition(marker, position) {
        marker.setPosition(
            new google.maps.LatLng(
                position.coords.latitude,
                position.coords.longitude)
        );
    }
    

    Maybe this example will help you:

    <!doctype html>
    <html lang="en">
    
        <head>
            <title>Google maps</title>
            <script src="http://code.jquery.com/jquery-1.8.2.min.js"></script>
            <script type="text/javascript" src="http://maps.google.com/maps/api/js?v=3&sensor=false&language=en"></script>
            <script type="text/javascript">
    
                var map,
                    currentPositionMarker,
                    mapCenter = new google.maps.LatLng(40.700683, -73.925972),
                    map;
    
                function initializeMap()
                {
                    map = new google.maps.Map(document.getElementById('map_canvas'), {
                       zoom: 13,
                       center: mapCenter,
                       mapTypeId: google.maps.MapTypeId.ROADMAP
                     });
                }
    
                function locError(error) {
                    // the current position could not be located
                    alert("The current position could not be found!");
                }
    
                function setCurrentPosition(pos) {
                    currentPositionMarker = new google.maps.Marker({
                        map: map,
                        position: new google.maps.LatLng(
                            pos.coords.latitude,
                            pos.coords.longitude
                        ),
                        title: "Current Position"
                    });
                    map.panTo(new google.maps.LatLng(
                            pos.coords.latitude,
                            pos.coords.longitude
                        ));
                }
    
                function displayAndWatch(position) {
                    // set current position
                    setCurrentPosition(position);
                    // watch position
                    watchCurrentPosition();
                }
    
                function watchCurrentPosition() {
                    var positionTimer = navigator.geolocation.watchPosition(
                        function (position) {
                            setMarkerPosition(
                                currentPositionMarker,
                                position
                            );
                        });
                }
    
                function setMarkerPosition(marker, position) {
                    marker.setPosition(
                        new google.maps.LatLng(
                            position.coords.latitude,
                            position.coords.longitude)
                    );
                }
    
                function initLocationProcedure() {
                    initializeMap();
                    if (navigator.geolocation) {
                        navigator.geolocation.getCurrentPosition(displayAndWatch, locError);
                    } else {
                        alert("Your browser does not support the Geolocation API");
                    }
                }
    
                $(document).ready(function() {
                    initLocationProcedure();
                });
    
            </script>
        </head>
    
        <body>
            <div id="map_canvas" style="height:600px;"></div>
        </body>
    
    </html>
    
    0 讨论(0)
提交回复
热议问题