Ignore:
Timestamp:
2019-06-20T17:13:50+12:00 (5 years ago)
Author:
wy59
Message:

Dr Bainbridge's map fixes to do with 1. extending bounds (updateMap) and 2. logging in works again when there's no gps map overlay meta by changing initialiseMapScripts and setupMap (renamed to renderMap now) and 3. performDistanceSearch and helper methods, which now work with Coordinate format instead of Lat/Lng format

File:
1 edited

Legend:

Unmodified
Added
Removed
  • main/trunk/greenstone3/web/interfaces/default/js/map-scripts.js

    r33148 r33168  
    1616var _searchRunning = false;
    1717var _nearbyDocs = new Array();
    18 var _scrollThroughDefault = true;
     18var _scrollThroughDefault = true; // TODO: default starts at true
    1919
    2020function initializeMapScripts()
    21 {
    22     setUpMap();
    23    
     21{   
     22    //console.log("@@@@ initializeMapScripts()");
     23
    2424    var jsonNodeDiv = $("#jsonNodes");
    2525    if(jsonNodeDiv.length)
     
    2828        var jsonNodes = eval(jsonNodeDiv.html());
    2929       
     30        renderMap();
    3031        if(jsonNodes && jsonNodes.length > 0)
    3132        {
     
    4142        {
    4243            //hiding the map
    43             //$("#map_canvas").css({visibility:"hidden", height:"0px"});
    44             $("#map_canvas").css({visibility:"hidden"}); // if you comment this out, add a log message saying "suppressing hiding map"
    45         }
    46     }
    47    
     44            mapEnabled = false;
     45            $("#map_canvas").css({display:"none"});
     46            return;
     47        }   
     48    }   
    4849    _docList.loopIndex = 0;
    4950   
     
    8485}
    8586
    86 function setUpMap()
    87 {
    88     //alert("@@@@in map-scripts::setUpMap()");
    89    
     87function renderMap()
     88{
     89    //console.log("@@@@in map-scripts::renderMap()");   
    9090    var myOptions =
    9191        {
     
    9797   
    9898    if ($map_canvas.length > 0) {
     99        //console.log("docList is " + _docList.toString());
    99100        _map = new google.maps.Map($map_canvas[0], myOptions);
     101        //console.log("@@@ created Google _map");
     102       
    100103        google.maps.event.addListener(_map, 'bounds_changed', performSearchForMarkers);
    101104    }
     
    104107function performSearchForMarkers()
    105108{
     109    //console.log("@@@ performSearchForMarkers()");
     110   
     111    var bounds = _map.getBounds();
     112    var ne = bounds.getNorthEast();
     113    var sw = bounds.getSouthWest();
     114                 
     115    //console.log("bounds: ne = " + ne + ", sw = " + sw);
     116   
     117   
    106118    if(_searchRunning)
    107119    {
     120        //console.log("*** performSearchForMarkers(): already running search => not initiating an additional search");
    108121        return;
    109122    }
    110    
    111123   
    112124   
     
    243255    var url = gs.xsltParams.library_name;
    244256    var data = "a=q&s=RawQuery&rt=rd&c=" + gs.cgiParams.c + "&s1.rawquery=" + query + "&excerptid=jsonNodes";
     257    //console.log("*** performSearchForMarkers(): rawQuery query data = " + query);
     258   
    245259    $.ajax({type:"POST", url:url, data:data})
    246260    .success(function(responseText)
     
    256270
    257271            var jsonNodes = eval(responseText.substring(startIndex+1, endIndex));
     272            //console.log("Number of matches returned from ajax rawQuery search = " + jsonNodes.length);
    258273            if(jsonNodes && jsonNodes.length > 0)
    259274            {
     
    320335function updateMap()
    321336{
     337    //console.log("@@@ updateMap()");   
    322338    var markersOnMap = 0;
    323339    var bounds =  new google.maps.LatLngBounds();
     
    331347                    doc.shapes[x].setVisible(false);
    332348                }
    333                 markersOnMap = doc.shapes.length;
     349                //markersOnMap += doc.shapes.length;
    334350            } else {
    335351                doc.marker.setVisible(false);
     
    340356        {
    341357            if(doc.shapes) {
    342                 for(var x = 0; x < doc.shapes.length; x++) {
     358                //console.log("@@@ making doc.shapes["+x+"] visible");
     359                for(var x = 0; x < doc.shapes.length; x++) {                   
    343360                    doc.shapes[x].setVisible(true);
    344361                }
    345                 markersOnMap = doc.shapes.length;
     362                markersOnMap += doc.shapes.length;
     363                //alert("HERE I AM " + markersOnMap);
     364               
    346365            } else {
    347366                doc.marker.setVisible(true);
    348             }
    349             markersOnMap++;
    350         }
    351 
     367                markersOnMap++;
     368            }
     369           
     370        }
    352371       
    353372        if(doc.coords) {
     373            //console.log("@@@@ HERE IN doc.coords");
    354374            for(var x = 0; x < doc.coords.length; x++) {
    355375                var coord = doc.coords[x];
     
    358378            }
    359379        }
     380       
     381        if(doc.mapoverlay) {
     382            console.log("@@@@ HERE IN doc.mapoverlay");
     383            var overlay_bounds = ShapesUtil.overlayBounds(doc.shapes);
     384            bounds.extend(overlay_bounds.getNorthEast());
     385            bounds.extend(overlay_bounds.getSouthWest());
     386        }
    360387        else {
    361             bounds.extend(new google.maps.LatLng(doc.lat, doc.lng));
    362         }
    363     }
    364    
     388            var doc_latlng = new google.maps.LatLng(doc.lat, doc.lng);
     389            bounds.extend(doc_latlng);
     390        }
     391    }
     392   
     393    //console.log("@@@ UpdateMap() : bounds = " + bounds);             
    365394    if(markersOnMap > 1)
    366395    {
    367     _map.fitBounds(bounds);
     396        _map.fitBounds(bounds);
    368397    } else if (markersOnMap == 1) {
    369     // sometimes a single point bounds are too small for the map to display, so use center and zoom instead of fitbounds.
    370     _map.setCenter(bounds.getCenter());
    371     _map.setZoom(18); // arbitrary value that looked nice for my example
     398        //console.log("@@@ updating bounds with " + markersOnMap + " markers on the map");
     399        //console.log(bounds);
     400       
     401        // sometimes a single point bounds are too small for the map to display, so use center and zoom instead of fitbounds.
     402        _map.setCenter(bounds.getCenter());
     403        _map.setZoom(18); // arbitrary value that looked nice for my example
    372404    }
    373405}
     
    406438    {
    407439        var doc = _docList.getDocByIndex(i);
    408         if(doc.marker.getVisible())
    409         {
    410             visibleMarkers.push(doc);
    411         }
     440        if(typeof doc.shapes !== 'undefined') {         
     441            for(var x = 0; x < doc.shapes.length; x++) {
     442                var shape = doc.shapes[i];
     443                if(shape.type === google.maps.drawing.OverlayType.MARKER && shape.getVisible())
     444                {
     445                    visibleMarkers.push(doc);
     446                }               
     447            }
     448        }
     449        if(doc.marker && doc.marker.getVisible())
     450        {
     451            visibleMarkers.push(doc);       
     452        }       
    412453    }
    413454   
     
    421462   
    422463    var elem = null;
    423     while(!elem)
     464    while(!elem) // TODO: why redefine elem? Why does this work, and only this, but not while(true) or while(doc.marker) or while(!AnythingFalse)???
     465                // Some clever behaviour here, but no documentation on the cleverness. Hard to understand
    424466    {
    425467        if(_docList.loopIndex >= visibleMarkers.length)
     
    429471
    430472        var doc = visibleMarkers[_docList.loopIndex];
    431         var elem = gs.jqGet("div" + doc.nodeID);
     473        elem = gs.jqGet("div" + doc.nodeID); // TODO: this used to redefine elem by doing var elem = <....>
    432474        if(elem.length)
    433475        {
     
    435477            setTimeout(function(){elem.css("background", "");}, 2000);
    436478        }
    437         _docList.loopIndex++;
    438     }
    439     doc.marker.markerInfo.open(_map, doc.marker);
     479        _docList.loopIndex ++;     
     480    }   
     481    //console.log("@@@@ DOC:");
     482    //console.log(doc);
     483   
     484    if(doc.marker) {
     485        doc.marker.markerInfo.open(_map, doc.marker); // TODO: how does doc have a value here? Where is the value set at this block level?
     486    }
     487   
     488    if(doc.shapes) {           
     489        for(var x = 0; x < doc.shapes.length; x++) {
     490            var shape = doc.shapes[i];
     491            if(shape.type === google.maps.drawing.OverlayType.MARKER) {
     492                shape.markerInfo.open(_map, shape);
     493            } else {
     494                shape.markerInfo.open(_map);
     495            }
     496        }
     497    }   
    440498}
    441499
     
    503561   
    504562    if(shape.type === google.maps.drawing.OverlayType.MARKER) {
    505         var marker = shape;
     563        var marker = shape;     
    506564        marker.addListener('mouseover', function() {
    507565          infoWindow.open(_map, marker);
     
    524582        attachClickHandler(shape, doc.nodeID); // as above     
    525583    }
     584    shape.markerInfo = infoWindow;
    526585}
    527586
     
    535594    // for doc.shapes: don't store JSON anymore, convert them to google Shapes overlays and store them instead
    536595    doc.shapes = [];
     596   
    537597    for (var i=0; i<doc.mapoverlay.length; i++) {
    538         var shape = ShapesUtil.JSONToShape(doc.mapoverlay[i]);     
     598        //console.log("in: mapoverlay["+i+"] =" + JSON.stringify(doc.mapoverlay[i]));
     599        var shape = ShapesUtil.JSONToShape(doc.mapoverlay[i]); 
     600        //console.log("out: shape = " + JSON.stringify(shape));
     601       
    539602        doc.shapes[i] = shape;     
    540603        shape.setMap(_map);
    541         shape["title"] = doc.title;
     604        //shape["title"] = doc.title;  // TODO: Think on it some more.
    542605       
    543606        // Unset editable and draggable properties of shape
     
    551614            }
    552615            marker.clickable = false; // only markers
     616            /*
     617            console.log("@@@ map-scripts::addInfoMarker - marker.position");
     618            console.log("Lat is " + typeof(marker.position.lat()));
     619            console.log(marker.position.lat());
     620            console.log("Long is " + typeof(marker.position.lng()));
     621            console.log(marker.position.lng());
     622            */
     623        } else {           
     624            //console.log("Creating non-marker shape.");
    553625        }
    554626       
    555627        shape.editable = false;
    556628        shape.draggable = false;
    557        
    558629       
    559630       
     
    672743       
    673744        updateMap();
     745        //console.log("getSub Classifier -> updateMap()");
    674746    })
    675747    .error(function()
    676748    {
    677         console.log("Error getting subclassifiers");
     749        //console.log("Error getting subclassifiers");
    678750        return;
    679751    });
     
    711783            var lngDelta = (j - degrees) * 0.01;
    712784           
    713             query += "(" + getDistanceQueryString(lat, latDelta, 2, "LA", ["N","S"]);
    714             query += "+AND+";
    715             query += getDistanceQueryString(lng, lngDelta, 2, "LN", ["E","W"]) + ")";
     785            //query += "(" + getDistanceQueryStringOldApproach(lat, latDelta, 2, "LA", ["N","S"]);
     786            //query += "+AND+";
     787            //query += getDistanceQueryStringOldApproach(lng, lngDelta, 2, "LN", ["E","W"]) + ")";
     788           
     789            query += "(" + getDistanceQueryStringTerm(lat, lng, latDelta, lngDelta, 2, "CS") + ")";
    716790           
    717791            if(i != ((degrees * 2) - 1) || j != ((degrees * 2) - 1)){ query += "+OR+"; }
     
    858932}
    859933
    860 function getDistanceQueryString(currentCoord, delta, precision, indexName, dirs)
    861 {
     934function getDistanceQueryStringOldApproach(currentCoord, delta, precision, indexName, directions)
     935{
     936    console.error("**** Old Approach called!!!");
     937   
    862938    var query = "";
    863939    var coordFloat = parseFloat(currentCoord);
     
    866942    var beforeDec = newCoord.substring(0, newCoord.indexOf("."));
    867943
    868     var dir = dirs[0];
     944    var direction = directions[0];
    869945    if(coordFloat < 0)
    870946    {
    871         dir = dirs[1];
    872         beforeDec = beforeDec.substring(1);
    873     }
    874     beforeDec += dir;
     947        // negative value
     948        direction = directions[1];
     949        beforeDec = beforeDec.substring(1); // skip over '-' at front
     950    }
     951    beforeDec += direction;
    875952
    876953    var afterDec = newCoord.substring(newCoord.indexOf(".") + 1, newCoord.indexOf(".") + (precision) + 1);
     
    878955    return indexName + ":" + beforeDec + "+AND+" + indexName + ":" + afterDec;
    879956}
     957
     958function coordValToIndexToken(coordValStr, delta, precision, directions)
     959{
     960    var coordValFloat = parseFloat(coordValStr);
     961
     962    var deltaCoordValStr = "" + (coordValFloat + delta);
     963    var beforeDec = deltaCoordValStr.substring(0, deltaCoordValStr.indexOf("."));
     964
     965    var direction = directions[0];
     966    if(coordValFloat < 0)
     967    {
     968        // negative value
     969        direction = directions[1];
     970        beforeDec = beforeDec.substring(1); // skip over '-' at front
     971    }
     972   
     973    var beforeDecWithDirection = beforeDec + direction;
     974   
     975    var afterDecPrecision = deltaCoordValStr.substring(deltaCoordValStr.indexOf(".") + 1, deltaCoordValStr.indexOf(".") + (precision) + 1);
     976
     977    var indexToken = beforeDecWithDirection + afterDecPrecision;   
     978
     979    return indexToken;
     980}
     981
     982function getDistanceQueryStringTerm(currentLat,currentLng, deltaLat, deltaLng, precision, indexName)
     983{
     984    var latToken = coordValToIndexToken(currentLat,deltaLat,precision,["N","S"]);
     985    var lngToken = coordValToIndexToken(currentLng,deltaLng,precision,["E","W"]);
     986   
     987    var queryStringTerm = indexName + ":\"" + latToken + " " + lngToken + "\"";
     988   
     989    return queryStringTerm;
     990}
Note: See TracChangeset for help on using the changeset viewer.