Skip to content

Commit e85a72a

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 e85a72a

File tree

1 file changed

+34
-2
lines changed

1 file changed

+34
-2
lines changed

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

Lines changed: 34 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,45 @@ 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+
Style style = null;
516+
StringBuilder styleJson = new StringBuilder(4096);
517+
String language = Locale.getDefault().getLanguage();
518+
519+
try {
520+
BufferedReader reader = new BufferedReader(new InputStreamReader(getActivity().getAssets().open(styleFileName)));
521+
String line = reader.readLine();
522+
while (line != null) {
523+
if (line.endsWith(suffix)) {
524+
String newSuffix = suffix.replace("\"", String.format("?lang=%s\"", language));
525+
styleJson.append(line.replace(suffix, newSuffix));
526+
Timber.d("Localized tiles url: %s", line.replace(suffix, newSuffix));
527+
} else {
528+
styleJson.append(line);
529+
}
530+
531+
line = reader.readLine();
532+
}
533+
} catch (IOException e){
534+
Timber.e(e, "failed to read file: %s", styleFileName);
535+
}
536+
537+
return styleJson.toString();
538+
}
539+
508540
/**
509541
* Sets up map view of first time it created, it passes MapBoxMap options and style assets.
510542
* @param savedInstanceState bundle coming from Nearby Fragment
511543
*/
512544
private void setupMapView(Bundle savedInstanceState) {
513545
Timber.d("setupMapView called");
546+
String styleJson = getLocaleMapStyleJson();
514547
MapboxMapOptions options = new MapboxMapOptions()
515548
.compassGravity(Gravity.BOTTOM | Gravity.LEFT)
516549
.compassMargins(new int[]{12, 0, 0, 24})
517-
.styleUrl(Style.OUTDOORS)
550+
.styleJson(styleJson)
518551
.logoEnabled(false)
519552
.attributionEnabled(false)
520553
.camera(new CameraPosition.Builder()
@@ -531,7 +564,6 @@ private void setupMapView(Bundle savedInstanceState) {
531564
addMapMovementListeners();
532565
updateMapSignificantlyForCurrentLocation();
533566
});
534-
mapView.setStyleUrl("asset://mapstyle.json");
535567
}
536568
}
537569

0 commit comments

Comments
 (0)