Skip to content

Commit 1f064e2

Browse files
Fixes Nearby export: Pins not all around me (commons-app#5658)
* Fixed Grey empty screen at Upload wizard caption step after denying files permission * Empty commit * Fixed loop issue * Created docs for earlier commits * Fixed javadoc * Fixed spaces * Added added basic features to OSM Maps * Added search ___location feature * Added filter to Open Street Maps * Fixed chipGroup in Open Street Maps * Removed mapBox code * Removed mapBox's code * Reformat code * Reformatted code * Removed rotation feature to map * Removed rotation files and Fixed Marker click problem * Ignored failing tests * Added voice input feature * Fixed test cases * Changed caption and description text * Replaced mapbox to osmdroid in upload activity * Fixed Unit Tests * Made selected marker to be fixed on map * Changed color of map marker * Fixes commons-app#4345 * Delete app/src/main/res/values-yue-hant directory * Added comment explaining the context * Fixes commons-app#5651 * Deleted directory * Changed query to 10 kilometers * Fixed issue --------- Co-authored-by: Nicolas Raoul <[email protected]>
1 parent 6d4ba12 commit 1f064e2

File tree

3 files changed

+116
-141
lines changed

3 files changed

+116
-141
lines changed

app/src/main/java/fr/free/nrw/commons/mwapi/OkHttpJsonApiClient.java

Lines changed: 68 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -412,63 +412,44 @@ public String getPlacesAsKML(final LatLng leftLatLng, final LatLng rightLatLng)
412412
"<!--Created by Wikimedia Commons Android app -->\n" +
413413
"<kml xmlns=\"http://www.opengis.net/kml/2.2\">\n" +
414414
" <Document>";
415-
416-
int increment = 1;
417-
double longitude = leftLatLng.getLongitude();
418-
419-
while (longitude <= rightLatLng.getLongitude()) {
420-
double NEXT_LONGITUDE =
421-
(increment + longitude) >= 0.0 && (increment + longitude) <= 1.0 ? 0.0
422-
: increment + longitude;
423-
424-
double latitude = leftLatLng.getLatitude();
425-
426-
while (latitude <= rightLatLng.getLatitude()) {
427-
double NEXT_LATITUDE =
428-
(increment + latitude) >= 0.0 && (increment + latitude) <= 1.0 ? 0.0
429-
: increment + latitude;
430-
List<PlaceBindings> placeBindings = runQuery(new LatLng(latitude, longitude, 0),
431-
new LatLng(NEXT_LATITUDE, NEXT_LONGITUDE, 0));
432-
if (placeBindings != null) {
433-
for (PlaceBindings item : placeBindings) {
434-
if (item.getItem() != null && item.getLabel() != null && item.getClas() != null) {
435-
String input = item.getLocation().getValue();
436-
Pattern pattern = Pattern.compile(
437-
"Point\\(([-+]?[0-9]*\\.?[0-9]+) ([-+]?[0-9]*\\.?[0-9]+)\\)");
438-
Matcher matcher = pattern.matcher(input);
439-
440-
if (matcher.find()) {
441-
String longStr = matcher.group(1);
442-
String latStr = matcher.group(2);
443-
String itemUrl = item.getItem().getValue();
444-
String itemName = item.getLabel().getValue().replace("&", "&amp;");
445-
String itemLatitude = latStr;
446-
String itemLongitude = longStr;
447-
String itemClass = item.getClas().getValue();
448-
449-
String formattedItemName =
450-
!itemClass.isEmpty() ? itemName + " (" + itemClass + ")"
451-
: itemName;
452-
453-
String kmlEntry = "\n <Placemark>\n" +
454-
" <name>" + formattedItemName + "</name>\n" +
455-
" <description>" + itemUrl + "</description>\n" +
456-
" <Point>\n" +
457-
" <coordinates>" + itemLongitude + ","
458-
+ itemLatitude
459-
+ "</coordinates>\n" +
460-
" </Point>\n" +
461-
" </Placemark>";
462-
kmlString = kmlString + kmlEntry;
463-
} else {
464-
Timber.e("No match found");
465-
}
466-
}
415+
List<PlaceBindings> placeBindings = runQuery(leftLatLng,
416+
rightLatLng);
417+
if (placeBindings != null) {
418+
for (PlaceBindings item : placeBindings) {
419+
if (item.getItem() != null && item.getLabel() != null && item.getClas() != null) {
420+
String input = item.getLocation().getValue();
421+
Pattern pattern = Pattern.compile(
422+
"Point\\(([-+]?[0-9]*\\.?[0-9]+) ([-+]?[0-9]*\\.?[0-9]+)\\)");
423+
Matcher matcher = pattern.matcher(input);
424+
425+
if (matcher.find()) {
426+
String longStr = matcher.group(1);
427+
String latStr = matcher.group(2);
428+
String itemUrl = item.getItem().getValue();
429+
String itemName = item.getLabel().getValue().replace("&", "&amp;");
430+
String itemLatitude = latStr;
431+
String itemLongitude = longStr;
432+
String itemClass = item.getClas().getValue();
433+
434+
String formattedItemName =
435+
!itemClass.isEmpty() ? itemName + " (" + itemClass + ")"
436+
: itemName;
437+
438+
String kmlEntry = "\n <Placemark>\n" +
439+
" <name>" + formattedItemName + "</name>\n" +
440+
" <description>" + itemUrl + "</description>\n" +
441+
" <Point>\n" +
442+
" <coordinates>" + itemLongitude + ","
443+
+ itemLatitude
444+
+ "</coordinates>\n" +
445+
" </Point>\n" +
446+
" </Placemark>";
447+
kmlString = kmlString + kmlEntry;
448+
} else {
449+
Timber.e("No match found");
467450
}
468451
}
469-
latitude += increment;
470452
}
471-
longitude += increment;
472453
}
473454
kmlString = kmlString + "\n </Document>\n" +
474455
"</kml>\n";
@@ -495,60 +476,42 @@ public String getPlacesAsGPX(final LatLng leftLatLng, final LatLng rightLatLng)
495476
" xsi:schemaLocation=\"http://www.topografix.com/GPX/1/0 http://www.topografix.com/GPX/1/0/gpx.xsd\">"
496477
+ "\n<bounds minlat=\"$MIN_LATITUDE\" minlon=\"$MIN_LONGITUDE\" maxlat=\"$MAX_LATITUDE\" maxlon=\"$MAX_LONGITUDE\"/>";
497478

498-
int increment = 1;
499-
double longitude = leftLatLng.getLongitude();
500-
501-
while (longitude <= rightLatLng.getLongitude()) {
502-
double NEXT_LONGITUDE =
503-
(increment + longitude) >= 0.0 && (increment + longitude) <= 1.0 ? 0.0
504-
: increment + longitude;
505-
506-
double latitude = leftLatLng.getLatitude();
507-
508-
while (latitude <= rightLatLng.getLatitude()) {
509-
double NEXT_LATITUDE =
510-
(increment + latitude) >= 0.0 && (increment + latitude) <= 1.0 ? 0.0
511-
: increment + latitude;
512-
List<PlaceBindings> placeBindings = runQuery(new LatLng(latitude, longitude, 0),
513-
new LatLng(NEXT_LATITUDE, NEXT_LONGITUDE, 0));
514-
if (placeBindings != null) {
515-
for (PlaceBindings item : placeBindings) {
516-
if (item.getItem() != null && item.getLabel() != null && item.getClas() != null) {
517-
String input = item.getLocation().getValue();
518-
Pattern pattern = Pattern.compile(
519-
"Point\\(([-+]?[0-9]*\\.?[0-9]+) ([-+]?[0-9]*\\.?[0-9]+)\\)");
520-
Matcher matcher = pattern.matcher(input);
521-
522-
if (matcher.find()) {
523-
String longStr = matcher.group(1);
524-
String latStr = matcher.group(2);
525-
String itemUrl = item.getItem().getValue();
526-
String itemName = item.getLabel().getValue().replace("&", "&amp;");
527-
String itemLatitude = latStr;
528-
String itemLongitude = longStr;
529-
String itemClass = item.getClas().getValue();
530-
531-
String formattedItemName =
532-
!itemClass.isEmpty() ? itemName + " (" + itemClass + ")"
533-
: itemName;
534-
535-
String gpxEntry =
536-
"\n <wpt lat=\"" + itemLatitude + "\" lon=\"" + itemLongitude
537-
+ "\">\n" +
538-
" <name>" + itemName + "</name>\n" +
539-
" <url>" + itemUrl + "</url>\n" +
540-
" </wpt>";
541-
gpxString = gpxString + gpxEntry;
542-
543-
} else {
544-
Timber.e("No match found");
545-
}
546-
}
479+
List<PlaceBindings> placeBindings = runQuery(leftLatLng,rightLatLng);
480+
if (placeBindings != null) {
481+
for (PlaceBindings item : placeBindings) {
482+
if (item.getItem() != null && item.getLabel() != null && item.getClas() != null) {
483+
String input = item.getLocation().getValue();
484+
Pattern pattern = Pattern.compile(
485+
"Point\\(([-+]?[0-9]*\\.?[0-9]+) ([-+]?[0-9]*\\.?[0-9]+)\\)");
486+
Matcher matcher = pattern.matcher(input);
487+
488+
if (matcher.find()) {
489+
String longStr = matcher.group(1);
490+
String latStr = matcher.group(2);
491+
String itemUrl = item.getItem().getValue();
492+
String itemName = item.getLabel().getValue().replace("&", "&amp;");
493+
String itemLatitude = latStr;
494+
String itemLongitude = longStr;
495+
String itemClass = item.getClas().getValue();
496+
497+
String formattedItemName =
498+
!itemClass.isEmpty() ? itemName + " (" + itemClass + ")"
499+
: itemName;
500+
501+
String gpxEntry =
502+
"\n <wpt lat=\"" + itemLatitude + "\" lon=\"" + itemLongitude
503+
+ "\">\n" +
504+
" <name>" + itemName + "</name>\n" +
505+
" <url>" + itemUrl + "</url>\n" +
506+
" </wpt>";
507+
gpxString = gpxString + gpxEntry;
508+
509+
} else {
510+
Timber.e("No match found");
547511
}
548512
}
549-
latitude += increment;
550513
}
551-
longitude += increment;
514+
552515
}
553516
gpxString = gpxString + "\n</gpx>";
554517
return gpxString;

app/src/main/java/fr/free/nrw/commons/nearby/NearbyController.java

Lines changed: 42 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package fr.free.nrw.commons.nearby;
22

3+
import static fr.free.nrw.commons.utils.LengthUtils.computeDistanceBetween;
4+
import static fr.free.nrw.commons.utils.LengthUtils.formatDistanceBetween;
5+
36
import androidx.annotation.MainThread;
47
import androidx.annotation.Nullable;
5-
68
import fr.free.nrw.commons.BaseMarker;
79
import fr.free.nrw.commons.MapController;
810
import fr.free.nrw.commons.___location.LatLng;
@@ -16,9 +18,6 @@
1618
import javax.inject.Inject;
1719
import timber.log.Timber;
1820

19-
import static fr.free.nrw.commons.utils.LengthUtils.computeDistanceBetween;
20-
import static fr.free.nrw.commons.utils.LengthUtils.formatDistanceBetween;
21-
2221
public class NearbyController extends MapController {
2322

2423
private static final int MAX_RESULTS = 1000;
@@ -39,7 +38,7 @@ public NearbyController(NearbyPlaces nearbyPlaces) {
3938
/**
4039
* Prepares Place list to make their distance information update later.
4140
*
42-
* @param currentLatLng current ___location for user
41+
* @param currentLatLng current ___location for user
4342
* @param searchLatLng the ___location user wants to search around
4443
* @param returnClosestResult if this search is done to find closest result or all results
4544
* @param customQuery if this search is done via an advanced query
@@ -118,18 +117,44 @@ public NearbyPlacesInfo loadAttractionsFromLocation(final LatLng currentLatLng,
118117
return nearbyPlacesInfo;
119118
}
120119

121-
public String getPlacesAsKML(LatLng leftLatLng, LatLng rightLatLng) throws Exception {
122-
return nearbyPlaces.getPlacesAsKML(leftLatLng, rightLatLng);
120+
public String getPlacesAsKML(LatLng currentLocation) throws Exception {
121+
return nearbyPlaces.getPlacesAsKML(
122+
calculateSouthWest(currentLocation.getLatitude(), currentLocation.getLongitude(), 10),
123+
calculateNorthEast(currentLocation.getLatitude(), currentLocation.getLongitude(), 10)
124+
);
123125
}
124126

125-
public String getPlacesAsGPX(LatLng leftLatLng, LatLng rightLatLng) throws Exception {
126-
return nearbyPlaces.getPlacesAsGPX(leftLatLng, rightLatLng);
127+
public String getPlacesAsGPX(LatLng currentLocation) throws Exception {
128+
return nearbyPlaces.getPlacesAsGPX(
129+
calculateSouthWest(currentLocation.getLatitude(), currentLocation.getLongitude(), 10),
130+
calculateNorthEast(currentLocation.getLatitude(), currentLocation.getLongitude(), 10)
131+
);
132+
}
133+
134+
public static LatLng calculateNorthEast(double latitude, double longitude, double distance) {
135+
double lat1 = Math.toRadians(latitude);
136+
double deltaLat = distance * 0.008;
137+
double deltaLon = distance / Math.cos(lat1)*0.008;
138+
double lat2 = latitude + deltaLat;
139+
double lon2 = longitude + deltaLon;
140+
141+
return new LatLng(lat2, lon2, 0);
142+
}
143+
144+
public static LatLng calculateSouthWest(double latitude, double longitude, double distance) {
145+
double lat1 = Math.toRadians(latitude);
146+
double deltaLat = distance * 0.008;
147+
double deltaLon = distance / Math.cos(lat1)*0.008;
148+
double lat2 = latitude - deltaLat;
149+
double lon2 = longitude - deltaLon;
150+
151+
return new LatLng(lat2, lon2, 0);
127152
}
128153

129154
/**
130155
* Prepares Place list to make their distance information update later.
131156
*
132-
* @param currentLatLng The current latitude and longitude.
157+
* @param currentLatLng The current latitude and longitude.
133158
* @param screenTopRight The top right corner of the screen (latitude,
134159
* longitude).
135160
* @param screenBottomLeft The bottom left corner of the screen (latitude,
@@ -221,7 +246,7 @@ public NearbyPlacesInfo loadAttractionsFromLocation(final LatLng currentLatLng,
221246
/**
222247
* Prepares Place list to make their distance information update later.
223248
*
224-
* @param currentLatLng current ___location for user
249+
* @param currentLatLng current ___location for user
225250
* @param searchLatLng the ___location user wants to search around
226251
* @param returnClosestResult if this search is done to find closest result or all results
227252
* @return NearbyPlacesInfo a variable holds Place list without distance information and
@@ -239,12 +264,12 @@ public NearbyPlacesInfo loadAttractionsFromLocation(final LatLng currentLatLng,
239264
* Loads attractions from ___location for map view, we need to return BaseMarkerOption data type.
240265
*
241266
* @param currentLatLng users current ___location
242-
* @param placeList list of nearby places in Place data type
267+
* @param placeList list of nearby places in Place data type
243268
* @return BaseMarkerOptions list that holds nearby places
244269
*/
245270
public static List<BaseMarker> loadAttractionsFromLocationToBaseMarkerOptions(
246-
LatLng currentLatLng,
247-
List<Place> placeList) {
271+
LatLng currentLatLng,
272+
List<Place> placeList) {
248273
List<BaseMarker> baseMarkersList = new ArrayList<>();
249274

250275
if (placeList == null) {
@@ -259,16 +284,14 @@ public static List<BaseMarker> loadAttractionsFromLocationToBaseMarkerOptions(
259284
baseMarker.setPosition(
260285
new fr.free.nrw.commons.___location.LatLng(
261286
place.___location.getLatitude(),
262-
place.___location.getLongitude(),0));
287+
place.___location.getLongitude(), 0));
263288
baseMarker.setPlace(place);
264289
baseMarkersList.add(baseMarker);
265290
}
266291
return baseMarkersList;
267292
}
268293

269294

270-
271-
272295
/**
273296
* Updates makerLabelList item isBookmarked value
274297
*
@@ -280,7 +303,8 @@ public static void updateMarkerLabelListBookmark(Place place, boolean isBookmark
280303
for (ListIterator<MarkerPlaceGroup> iter = markerLabelList.listIterator();
281304
iter.hasNext(); ) {
282305
MarkerPlaceGroup markerPlaceGroup = iter.next();
283-
if (markerPlaceGroup.getPlace().getWikiDataEntityId().equals(place.getWikiDataEntityId())) {
306+
if (markerPlaceGroup.getPlace().getWikiDataEntityId()
307+
.equals(place.getWikiDataEntityId())) {
284308
iter.set(new MarkerPlaceGroup(isBookmarked, place));
285309
}
286310
}

0 commit comments

Comments
 (0)