I wonder if we know why the "reset to initial map state" button / functionality seems to have been dropped in Google Maps API v3?
In API v2, the hand button in the middle of the arrow buttons was a kind of "home" or "reset" button that retuned the map to its initial position and zoom level.
Not the end of the world of course, merely curious.开发者_如何学C..
I think because it is rather easy for the developer to do themselves and it wasn't a very widely used feature so they probably decided not to weight everyone down with code that only a few were using.
What you can do is add a custom control on the page and when the user clicks on it then move the map back to the zoom and center that you want. One way to collect that could be to listen to the map 'idle' event and then set a timeout to only store the map position after it has been untouched for X seconds. Of course this won't look like the v2 version :)
Here is a small hack to make the reset button works on v3. I use jQuery here.
var attachEventToResetButton;
// Attach event to the reset button
var attachResetEvent = function(){
var $resetImg = $('img[src*=mapcontrols3d6.png]');
// We have to check if the image is available yet.
// The reason is although the map has been loaded, the navigation might
// take some time to load and we don't know when it will be fully loaded.
// There doesn't seem to have an event for "Navigation loaded" in the API
// So here is a way to work around
if ($resetImg.length > 0)
{
$resetImg.css('cursor', 'pointer').attr('title', 'Return to center').click(function(){
alert('Clicked on reset button');
// Put your code to reset the map here. For example:
//map.setMapTypeId(MAP_TYPE_ID);
//map.setCenter(new google.maps.LatLng(LAT, LNG));
//map.setZoom(ZOOM_LEVEL);
});
window.clearInterval(attachEventToResetButton);
}
}
// Periodically checking to attach event to the reset button
attachEventToResetButton = window.setInterval(attachResetEvent, 500);
What I did was, I notice that the reset image filename is 'mapcontrols3d6.png'. So I set an interval to check if that image has been loaded (ie. available) yet. If yes, I attach a function into it.
As this is a hack, it has some issue. The main one is we have to rely on the reset image file name. So finger cross that Google won't update this.
Does anyone have any better way?
精彩评论