seveves
seveves

Reputation: 1282

Google Maps does not update tiles while dragging

I have to use the google maps javascript api in a webview. The problem is that while dragging the map the missing tiles won't be loaded. In a browser on my desktop pc I can't get the same behaviour but loading the tiles while dragging the map around. In the picture I hold the map and nothing happens till I release my finger.

Dragging and holding

I have found an issue (not perfectly fitting on my problem but kinda close) in the gmaps bugtracker and tried all the solutions mentioned: https://code.google.com/p/gmaps-api-issues/issues/detail?id=1448

This is what I've tried so far:

// Setting the maps div container to a fixed size
#map-canvas {
    height: 600px;
    width: 600px;
}

// Emit resize trigger on mouse move
$(document).ready(function () {
    $("#map-canvas").mousemove(function (event) {
        setTimeout(function () { 
            google.maps.event.trigger(map,'resize');
            map.setZoom(map.getZoom());
        }, 100);
    });
});

// loading Google Maps API after document has been loaded
function loadScript() {
  var script = document.createElement('script');
  script.type = 'text/javascript';
  script.src = 'https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false&' +
      'callback=initialize';
  document.body.appendChild(script);
}
window.onload = loadScript;

The code is working and the mousemove event gets fired. I am using android:minSdkVersion="19" on my nexus 10 device.

Upvotes: 27

Views: 5180

Answers (7)

Mir Shakeel Hussain
Mir Shakeel Hussain

Reputation: 517

try this it works

google.maps.event.addListenerOnce(map, 'tilesloaded', function () {
    google.maps.event.addListenerOnce(map, 'tilesloaded', function () {
        google.maps.event.trigger(map, 'resize');
    });
});

Upvotes: 0

Ajay Peter
Ajay Peter

Reputation: 153

touch event are differs from mouse events, you can use touch event bindings like touch-start,touch-end,etc or try to use hammer.js

http://hammerjs.github.io/

Upvotes: 1

Glynne Turner
Glynne Turner

Reputation: 149

i have answered this one before. check out this jsfiddle

http://jsfiddle.net/cQTdc/

<html>
<head>
<script type="text/javascript" src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
<script src="https://maps.googleapis.com/maps/api/js?sensor=false"></script>
<script>
  function initialize() {
    var map_canvas = document.getElementById('map_canvas');
    var map_options = {
      center: new google.maps.LatLng(51.372658, 1.354386),
      zoom:16,
      mapTypeId: google.maps.MapTypeId.HYBRID
    }
    var map = new google.maps.Map(map_canvas, map_options)
  }
  google.maps.event.addDomListener(window, 'load', initialize);
</script>
<style>
  #map_canvas {
    width: 500px;  
    height:400px;
    border:1px solid #888888;
    margin-bottom:20px;
    box-shadow: 2px 2px 2px #888888;
  }
</style>   
</head>
<body>
<div id="map_canvas"></div>
</body>

you will need to change here for the longitude and latitude for your location. You can get this from google maps. the part to change is "new google.maps.LatLng(51.372658, 1.354386)"

Upvotes: -1

Wen
Wen

Reputation: 2237

I have same issue of not centring, and displays only part of map on map canvas.

This line fixed my problem:

google.maps.event.trigger(map, 'resize');

Should not trigger it when mouseover, need have this line after map.initialize(); and then do setCenter and setZoom

map.setCenter(myLatlng);
map.setZoom(10);

Upvotes: 0

KSEL
KSEL

Reputation: 19

//Create One Div as 'map_canvas'

    var htmldesign = '<table><tr><td>India</td></tr></table>';
    var locationdetails = 'India';
    var popdetail = 'India';
    var zoomper = 5;
    GetLocation(locationdetails, popdetail, zoomper);

    function GetLocation(address, popdetail, zoomper) {
        var geocoder = new google.maps.Geocoder();
        geocoder.geocode({ 'address': address }, function (results, status) {
            if (status == google.maps.GeocoderStatus.OK) {
                var latitude = results[0].geometry.location.lat();
                var longitude = results[0].geometry.location.lng();
                initialize(latitude, longitude, popdetail, zoomper);
            } else {
                initialize(21.0000, 78.8718, 'India', zoomper);
            }
        });
    };

    function initialize(lat, lon, popdetail, zoom) {

        var myLatlng = new google.maps.LatLng(lat, lon) // This is used to center the map to show our markers
        var mapOptions;
        mapOptions = {
            center: myLatlng,
            zoom: zoom,
            mapTypeId: google.maps.MapTypeId.ROADMAP,
            marker: true
        };
        var map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions);
        var marker = new google.maps.Marker({
            animation: google.maps.Animation.BOUNCE,
            position: myLatlng
        });
        if (popdetail != 'India') {
            popdetail = '<div style="font-size:small;font-family:@Arial Unicode MS;border-radius: 6px;" class="phoneytext"> ' + popdetail + '</div>';
            infoBubble = new InfoBubble({
                map: map,
                content: popdetail,
                position: new google.maps.LatLng(lat, lon),
                shadowStyle: 1,
                padding: 10,
                backgroundColor: '#E6E6E6',
                borderRadius: 6,
                arrowSize: 30,
                borderWidth: 1,
                borderColor: '#2c2c2c',
                disableAutoPan: false,
                hideCloseButton: true,
                arrowPosition: 10,
                backgroundClassName: 'phoney',
                arrowStyle: 2
            });
            marker.setMap(map);
            infoBubble.open(map, marker);
        }
        else {
            marker.setMap(map);
        }
    }
</script>

Upvotes: -1

Sourabh Upadhyay
Sourabh Upadhyay

Reputation: 1044

Try this... This problem raise only when map get resized.

google.maps.event.addDomListener( map, 'drag', function(e) {
    google.maps.event.trigger(map,'resize');
    map.setZoom(map.getZoom());
});

Upvotes: 2

Alex McMillan
Alex McMillan

Reputation: 17952

Could you perhaps buffer a larger map, and only show a portion in the viewport? EG load the map into a 1200x1200 div inside the 600x600 one, with overflow: hidden?

Upvotes: 1

Related Questions