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,45 @@ 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
+ 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
+
508
540
/**
509
541
* Sets up map view of first time it created, it passes MapBoxMap options and style assets.
510
542
* @param savedInstanceState bundle coming from Nearby Fragment
511
543
*/
512
544
private void setupMapView (Bundle savedInstanceState ) {
513
545
Timber .d ("setupMapView called" );
546
+ String styleJson = getLocaleMapStyleJson ();
514
547
MapboxMapOptions options = new MapboxMapOptions ()
515
548
.compassGravity (Gravity .BOTTOM | Gravity .LEFT )
516
549
.compassMargins (new int []{12 , 0 , 0 , 24 })
517
- .styleUrl ( Style . OUTDOORS )
550
+ .styleJson ( styleJson )
518
551
.logoEnabled (false )
519
552
.attributionEnabled (false )
520
553
.camera (new CameraPosition .Builder ()
@@ -531,7 +564,6 @@ private void setupMapView(Bundle savedInstanceState) {
531
564
addMapMovementListeners ();
532
565
updateMapSignificantlyForCurrentLocation ();
533
566
});
534
- mapView .setStyleUrl ("asset://mapstyle.json" );
535
567
}
536
568
}
537
569
0 commit comments