Latest Post

Error: No input file specified.

While trying to run a codeigniter app the other day I came across this error just after I had finished uploading the files and making the ne...

Wednesday, February 4, 2015

Google Maps API HOWTO: Retain zoom and center after refresh using cookies

Q: How do I make Google Maps retain the user's view (zoom level and center point) after an HTTP refresh?

A: You could do the following:

// Use events to load the saved map state any given time

// You could use event listeners to keep track of map view changes as zoom change, map finishes loading tile etc
google.maps.event.addListener(map, 'tilesloaded', tilesLoaded);
function tilesLoaded() {
    google.maps.event.clearListeners(map, 'tilesloaded');
    google.maps.event.addListener(map, 'zoom_changed', saveMapState);
    google.maps.event.addListener(map, 'dragend', saveMapState);

// Here are the suppoert functions needed to make all this work

function saveMapState() { 
    var mapZoom=map.getZoom(); 
    var mapCentre=map.getCenter(); 
    var mapLng=mapCentre.lng(); 
    var cookiestring=mapLat+"_"+mapLng+"_"+mapZoom; 
    setCookie("myMapCookie",cookiestring, 30); 

function loadMapState() { 
    var gotCookieString=getCookie("myMapCookie"); 
    var splitStr = gotCookieString.split("_");
    var savedMapLat = parseFloat(splitStr[0]);
    var savedMapLng = parseFloat(splitStr[1]);
    var savedMapZoom = parseFloat(splitStr[2]);
    if ((!isNaN(savedMapLat)) && (!isNaN(savedMapLng)) && (!isNaN(savedMapZoom))) {
        map.setCenter(new google.maps.LatLng(savedMapLat,savedMapLng));

function setCookie(c_name,value,exdays) {
    var exdate=new Date();
    exdate.setDate(exdate.getDate() + exdays);
    var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
    document.cookie=c_name + "=" + c_value;

function getCookie(c_name) {
    var i,x,y,ARRcookies=document.cookie.split(";");
    for (i=0;i
Source: Stackoverflow
NOTE: If you are using Google API v2 then you just can use map.savePosition()to save the current position of the map and then map.returnToSavedPosition()to restore the saved position . Trying to use these functions in v3 of the API can result in an error "map.savePosition is not a function" or something to that effect.

Tuesday, February 3, 2015

How to Ignore Changes in Tracked Files With Git

If you have added files into your GIT repository and at a later stage realized that you do not want to track changes to these files, here is how to achieve this without using .gitignore.

So you would type the following command:

git update-index --assume-unchanged

To start tracking changes again on that file you need to use the following command:

git update-index --no-assume-unchanged

See the git manual for more info

Chitika Ads