Skip to content

Commit 0707671

Browse files
committed
Use wikimedia tiles and support localization on nearby map (commons-app#2335)
Wikimedia tiles support adding a "?lang=xx" suffix to tiles url to set preferred language. Use this feature to set the preferred language to user's default Locale.
1 parent 44c18ac commit 0707671

File tree

1 file changed

+33
-2
lines changed

1 file changed

+33
-2
lines changed

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

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,13 @@
4545
import com.mapbox.mapboxsdk.maps.MapboxMap;
4646
import com.mapbox.mapboxsdk.maps.MapboxMapOptions;
4747

48+
import java.io.BufferedReader;
49+
import java.io.IOException;
50+
import java.io.InputStreamReader;
4851
import java.lang.reflect.Type;
4952
import java.util.ArrayList;
5053
import java.util.List;
54+
import java.util.Locale;
5155

5256
import javax.inject.Inject;
5357
import javax.inject.Named;
@@ -505,16 +509,44 @@ public void onSlide(@NonNull View bottomSheet, float slideOffset) {
505509
});
506510
}
507511

512+
private String getLocaleMapStyleJson() {
513+
String styleFileName = "mapstyle.json";
514+
String suffix = "png\"";
515+
StringBuilder styleJson = new StringBuilder(4096);
516+
String language = Locale.getDefault().getLanguage();
517+
518+
try {
519+
BufferedReader reader = new BufferedReader(new InputStreamReader(getActivity().getAssets().open(styleFileName)));
520+
String line = reader.readLine();
521+
while (line != null) {
522+
if (line.endsWith(suffix)) {
523+
String newSuffix = suffix.replace("\"", String.format("?lang=%s\"", language));
524+
styleJson.append(line.replace(suffix, newSuffix));
525+
Timber.d("Localized tiles url: %s", line.replace(suffix, newSuffix));
526+
} else {
527+
styleJson.append(line);
528+
}
529+
530+
line = reader.readLine();
531+
}
532+
} catch (IOException e){
533+
Timber.e(e, "failed to read file: %s", styleFileName);
534+
}
535+
536+
return styleJson.toString();
537+
}
538+
508539
/**
509540
* Sets up map view of first time it created, it passes MapBoxMap options and style assets.
510541
* @param savedInstanceState bundle coming from Nearby Fragment
511542
*/
512543
private void setupMapView(Bundle savedInstanceState) {
513544
Timber.d("setupMapView called");
545+
String styleJson = getLocaleMapStyleJson();
514546
MapboxMapOptions options = new MapboxMapOptions()
515547
.compassGravity(Gravity.BOTTOM | Gravity.LEFT)
516548
.compassMargins(new int[]{12, 0, 0, 24})
517-
.styleUrl(Style.OUTDOORS)
549+
.styleJson(styleJson)
518550
.logoEnabled(false)
519551
.attributionEnabled(false)
520552
.camera(new CameraPosition.Builder()
@@ -531,7 +563,6 @@ private void setupMapView(Bundle savedInstanceState) {
531563
addMapMovementListeners();
532564
updateMapSignificantlyForCurrentLocation();
533565
});
534-
mapView.setStyleUrl("asset://mapstyle.json");
535566
}
536567
}
537568

0 commit comments

Comments
 (0)