jenkins-bot has submitted this change and it was merged.

Change subject: Surface page images on nearby
......................................................................


Surface page images on nearby

Using the geosearch api in this way does not provide proximity
calculations (boo!) - so using haversine formula to do
calculation on frontend

Change-Id: I446dbd5d328c6a444e67aa1dd48b257b1169f64d
---
M javascripts/specials/nearby.js
1 file changed, 60 insertions(+), 10 deletions(-)

Approvals:
  awjrichards: Verified; Looks good to me, approved
  jenkins-bot: Verified



diff --git a/javascripts/specials/nearby.js b/javascripts/specials/nearby.js
index c22b22f..81e55f6 100644
--- a/javascripts/specials/nearby.js
+++ b/javascripts/specials/nearby.js
@@ -5,17 +5,59 @@
                popup = M.require( 'notifications' ),
                View = M.require( 'view' ),
                cachedPages,
+               curLocation,
                Nearby = View.extend( {
                        template: M.template.get( 'articleList' )
                } );
 
+       // FIXME: Api should surely know this and return it in response to save 
us the hassle
+       // haversine formula ( http://en.wikipedia.org/wiki/Haversine_formula )
+       function calculateDistance( from, to ) {
+               var distance, a,
+                       toRadians = Math.PI / 180,
+                       deltaLat, deltaLng,
+                       startLat, endLat,
+                       haversinLat, haversinLng,
+                       radius = 6378; // radius of Earth in km
+
+               if( from.latitude === to.latitude && from.longitude === 
to.longitude ) {
+                       distance = 0;
+               } else {
+                       deltaLat = ( to.longitude - from.longitude ) * 
toRadians;
+                       deltaLng = ( to.latitude - from.latitude ) * toRadians;
+                       startLat = from.latitude * toRadians;
+                       endLat = to.latitude * toRadians;
+
+                       haversinLat = Math.sin( deltaLat / 2 ) * Math.sin( 
deltaLat / 2 );
+                       haversinLng = Math.sin( deltaLng / 2 ) * Math.sin( 
deltaLng / 2 );
+
+                       a = haversinLat + Math.cos( startLat ) * Math.cos( 
endLat ) * haversinLng;
+                       return 2 * radius * Math.asin( Math.sqrt( a ) );
+               }
+               return distance;
+       }
+
        function render( $content, pages ) {
+               pages = $.map( pages, function( page ) {
+                       var coords, lngLat;
+
+                       if ( page.coordinates ) { // FIXME: protecting us 
against an api bug 47133
+                               if ( page.thumbnail ) {
+                                       page.pageimage = page.thumbnail.source;
+                               }
+                               page.url = M.history.getArticleUrl( page.title 
);
+
+                               coords = page.coordinates[0],
+                               lngLat = { latitude: coords.lat, longitude: 
coords.lon };
+                               page.dist = calculateDistance( curLocation, 
lngLat );
+                               page.proximity = mw.message( 
'mobile-frontend-nearby-distance-report',
+                                       page.dist.toFixed( 2 ) );
+                               pages.push( page );
+                               return page;
+                       }
+               } );
                pages.sort( function( a, b ) {
                        return a.dist > b.dist ? 1 : -1;
-               } ).forEach( function( page ) {
-                       page.url = M.history.getArticleUrl( page.title );
-                       page.proximity = mw.message( 
'mobile-frontend-nearby-distance-report',
-                               ( page.dist / 1000 ).toFixed( 2 ) );
                } );
 
                new Nearby( {
@@ -26,20 +68,27 @@
 
        function findResults( lat, lng ) {
                var $content = $( '#mw-mf-nearby' ), range = 10000, endpoint = 
mw.config.get( 'wgMFNearbyEndpoint' );
+                       limit = 50;
+
                $.ajax( {
                        dataType: endpoint ? 'jsonp' : 'json',
                        url: endpoint || M.getApiUrl(),
                        data: {
                                action: 'query',
-                               list: 'geosearch',
+                               prop: 'pageimages|coordinates',
+                               pithumbsize: 180,
+                               pilimit: limit,
+                               generator: 'geosearch',
                                format: 'json',
-                               gscoord: lat + '|' + lng,
-                               gsradius: range,
-                               gsnamespace: 0,
-                               gslimit: 100
+                               ggscoord: lat + '|' + lng,
+                               ggsradius: range,
+                               ggsnamespace: 0,
+                               ggslimit: limit
                        }
                } ).done( function( data ) {
-                       var pages = data.query.geosearch, $popup;
+                       var pages = $.map( data.query.pages, function( i ) {
+                               return i;
+                       } ), $popup;
                        if ( pages.length > 0 ) {
                                if ( !cachedPages ) {
                                        render( $content, pages );
@@ -70,6 +119,7 @@
                        mw.message( 'mobile-frontend-nearby-loading' ) 
).appendTo( $content );
                navigator.geolocation.watchPosition( function( geo ) {
                        var lat = geo.coords.latitude, lng = 
geo.coords.longitude;
+                       curLocation = geo.coords;
                        findResults( lat, lng );
                },
                function() {

-- 
To view, visit https://gerrit.wikimedia.org/r/58652
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: I446dbd5d328c6a444e67aa1dd48b257b1169f64d
Gerrit-PatchSet: 4
Gerrit-Project: mediawiki/extensions/MobileFrontend
Gerrit-Branch: master
Gerrit-Owner: Jdlrobson <[email protected]>
Gerrit-Reviewer: MaxSem <[email protected]>
Gerrit-Reviewer: Yurik <[email protected]>
Gerrit-Reviewer: awjrichards <[email protected]>
Gerrit-Reviewer: jenkins-bot

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to