Google maps V3 JS no happen “center_changed” from

2019-04-28 15:28发布

问题:

I'm trying to display GoogleMap(Google maps V3 JS) in UIWebView(ios6).
but,
When I moveing the map in webview,event no fire center_changed.
Move map has been completed, event fire.
Why?
...
Someone told me page:
http://gmaps-samples-v3.googlecode.com/svn/trunk/map_events/map_events.html
Mac' Safri access - fire center_changed while the map is moving.
iOS6 Safri access - move map is complete, fire center_changed.

...
I want to know the Center-Cordinates of the map while moving.
Please tell me a good way.

`<html>
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
<meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
<title>Google Maps JavaScript API v3 Example: iPhone Geolocation</title>
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
<script type="text/javascript">
var map;
var moveCenterLat;
var moveCenterLng;

function initialize() {
    var myOptions = {
    zoom:reqZoomLevel,
    disableDefaultUI:true,
    draggable:true,
    keyboardShortcuts:false,
    scrollwheel:false,
    mapTypeId: google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);

google.maps.event.addListener(map, 'center_changed', function(){
    var mce = map.getCenter();
    moveCenterLat = mce.lat();
    moveCenterLng = mce.lng();
});

}
</script>
</head>
<body style="margin:0px; padding:0px;" onload="initialize()">
<div id="map_canvas" style="width:100%; height:100%"></div>
</body>
</html>`

回答1:

This is something that doesn't work on google maps on IOS devices. The center_changed is fired when the map is dragged but the center does not update until the drag is complete. So the answer is, 'no', you can not do this.



回答2:

It sounds like you want the drag event, not center_changed. From the Google Maps Javascript API V3 Reference:

drag: this event is repeatedly fired while the user drags the map.

Something like this:

google.maps.event.addListener(map, 'drag', function(){
    var mce = map.getCenter();
    moveCenterLat = mce.lat();
    moveCenterLng = mce.lng();
});

Edit: It seems the map bounds are no longer updated while dragging, so although the drag event does fire while the map is being dragged, both getCenter() returns the coordinates of the map when the drag started. Thanks to @Zubair for pointing this out.