45
45
import com .mapbox .mapboxsdk .maps .MapboxMap ;
46
46
import com .mapbox .mapboxsdk .maps .MapboxMapOptions ;
47
47
48
+ import java .io .BufferedReader ;
49
+ import java .io .IOException ;
50
+ import java .io .InputStreamReader ;
48
51
import java .lang .reflect .Type ;
49
52
import java .util .ArrayList ;
50
53
import java .util .List ;
54
+ import java .util .Locale ;
51
55
52
56
import javax .inject .Inject ;
53
57
import javax .inject .Named ;
@@ -505,16 +509,44 @@ public void onSlide(@NonNull View bottomSheet, float slideOffset) {
505
509
});
506
510
}
507
511
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
+
508
539
/**
509
540
* Sets up map view of first time it created, it passes MapBoxMap options and style assets.
510
541
* @param savedInstanceState bundle coming from Nearby Fragment
511
542
*/
512
543
private void setupMapView (Bundle savedInstanceState ) {
513
544
Timber .d ("setupMapView called" );
545
+ String styleJson = getLocaleMapStyleJson ();
514
546
MapboxMapOptions options = new MapboxMapOptions ()
515
547
.compassGravity (Gravity .BOTTOM | Gravity .LEFT )
516
548
.compassMargins (new int []{12 , 0 , 0 , 24 })
517
- .styleUrl ( Style . OUTDOORS )
549
+ .styleJson ( styleJson )
518
550
.logoEnabled (false )
519
551
.attributionEnabled (false )
520
552
.camera (new CameraPosition .Builder ()
@@ -531,7 +563,6 @@ private void setupMapView(Bundle savedInstanceState) {
531
563
addMapMovementListeners ();
532
564
updateMapSignificantlyForCurrentLocation ();
533
565
});
534
- mapView .setStyleUrl ("asset://mapstyle.json" );
535
566
}
536
567
}
537
568
0 commit comments