Sencha Touch - Google Map and centering a Marker

青春壹個敷衍的年華 提交于 2019-12-07 18:02:02

问题


Is it just me or does the google maps api seem to be on the quirky side? Trying to simply show a marker for some coordinates, and then center the screen on the marker.

In my controller fired from a view:

onGoogleMapRender: function (googleMap) {

        var long = record.get("Longitude");
        var lat = record.get("Latitude");

        var pos = new google.maps.LatLng(lat, long);

        var marker = new google.maps.Marker({
            position: pos
        });

        marker.setMap(googleMap);
        googleMap.setCenter(marker.getPosition());
        googleMap.setZoom(14);

    }

It seems to place the marker in the center of the screen but when I return and show the same screen the 2nd, or 3rd time, the marker is added but this time the marker doesn't get centered.

Has anyone got this working correctly? I don't suppose it is any ST2 related but more a quirk of the google api.


回答1:


You have to center it with a small timeout, than it will work fine.

setTimeout(function() {
    googleMap.panTo(marker.getPosition());
}, 100);

I don't know if it's a bug, but this way is shown in the showcase.



来源:https://stackoverflow.com/questions/15041697/sencha-touch-google-map-and-centering-a-marker

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!