6

Leaflet is loading partial tiles. Below is the screenshot:

enter image description here

Below is my code, where I am trying center the marker using panTO() function:

        $scope.plotMap = function(lat, lng) {
            var latlng = new L.LatLng(lat, lng);

            $scope.map = new L.Map('detailsmap', {
                center: latlng,
                zoom: 3}
            );

            var center = $scope.map.project(latlng);
            center = new L.point(center.x - 150, center.y - 100);
            var target = $scope.map.unproject(center);
            $scope. map.panTo(target);

            L.tileLayer(
                'http://{s}.tile.osm.org/{z}/{x}/{y}.png',
                {
                attribution: '&copy; <a href="http://openstreetmap' +
                    '.org">OpenStreetMap</a> contributors',
                }
            ).addTo($scope.map);

            L.marker(latlng, {icon: new L.Icon.Default()}).addTo(
                $scope.map);
        };
3
  • You might be in that situation: stackoverflow.com/questions/36246815/…
    – ghybs
    Commented Sep 1, 2016 at 16:55
  • you are right, I had the same problem and invalidateSize() has resolved it.
    – vips
    Commented Sep 2, 2016 at 15:06
  • Thank you for the feedback! Please can you kindly write it down as an answer and auto-accept it, so that people know your issue is solved? Thanks!
    – ghybs
    Commented Sep 5, 2016 at 6:05

1 Answer 1

3

Calling invalidateSize() function solved the problem. Below is the code snippet:

    $scope.plotMap = function(lat, lng) {
        var latlng = new L.LatLng(lat, lng);

        $scope.map = new L.Map('detailsmap', {
            center: latlng,
            zoom: 3}
        );

        var center = $scope.map.project(latlng);
        center = new L.point(center.x - 150, center.y - 100);
        var target = $scope.map.unproject(center);
        $scope. map.panTo(target);

        L.tileLayer(
            'http://{s}.tile.osm.org/{z}/{x}/{y}.png',
            {
            attribution: '&copy; <a href="http://openstreetmap' +
                '.org">OpenStreetMap</a> contributors',
            }
        ).addTo($scope.map);

        L.marker(latlng, {icon: new L.Icon.Default()}).addTo(
            $scope.map);
    };

    $scope.dispMap = function() {
        $scope.plotMap();
        $scope.map.invalidateSize();
    };
1
  • invalidateSize definitely works. In my case, I needed to wrap the call in a setTimeout function with a small delay, as the page drawing wasn't quite completed when the map was set up.
    – sherb
    Commented Sep 9, 2016 at 18:19

Not the answer you're looking for? Browse other questions tagged or ask your own question.