Skip to content
This repository has been archived by the owner on Oct 7, 2024. It is now read-only.

Adding localization plugin example #593

Merged
merged 15 commits into from
Mar 9, 2018
1 change: 1 addition & 0 deletions MapboxAndroidDemo/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ dependencies {
implementation dependenciesList.mapboxPluginGeoJson
implementation dependenciesList.mapboxPluginMarkerCluster
implementation dependenciesList.mapboxPluginPlaces
implementation dependenciesList.mapboxPluginLocalization

// Firebase
implementation dependenciesList.firebaseCrash
Expand Down
7 changes: 7 additions & 0 deletions MapboxAndroidDemo/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,13 @@
android:name="android.support.PARENT_ACTIVITY"
android:value="com.mapbox.mapboxandroiddemo.MainActivity" />
</activity>
<activity
android:name=".examples.plugins.LocalizationPluginActivity"
android:label="@string/activity_plugins_localization_plugin_title">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.mapbox.mapboxandroiddemo.MainActivity" />
</activity>
<activity
android:name=".examples.camera.BoundingBoxCameraActivity"
android:label="@string/activity_camera_bounding_box_title">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
import com.mapbox.mapboxandroiddemo.examples.offline.SimpleOfflineMapActivity;
import com.mapbox.mapboxandroiddemo.examples.plugins.BuildingPluginActivity;
import com.mapbox.mapboxandroiddemo.examples.plugins.GeoJsonPluginActivity;
import com.mapbox.mapboxandroiddemo.examples.plugins.LocalizationPluginActivity;
import com.mapbox.mapboxandroiddemo.examples.plugins.LocationPluginActivity;
import com.mapbox.mapboxandroiddemo.examples.plugins.MarkerClustersPluginActivity;
import com.mapbox.mapboxandroiddemo.examples.plugins.PlacesPluginActivity;
Expand Down Expand Up @@ -418,8 +419,14 @@ private void listItems(int id) {
R.string.activity_plugins_marker_clusters_plugin_title,
R.string.activity_plugins_geojson_marker_clusters_description,
new Intent(MainActivity.this, MarkerClustersPluginActivity.class),
R.string.activity_plugins_markers_clusters_plugin_url, true, BuildConfig.MIN_SDK_VERSION));

R.string.activity_plugins_markers_clusters_plugin_url, true, BuildConfig.MIN_SDK_VERSION)
);
exampleItemModels.add(new ExampleItemModel(
R.string.activity_plugins_localization_plugin_title,
R.string.activity_plugins_localization_plugin_description,
new Intent(MainActivity.this, LocalizationPluginActivity.class),
R.string.activity_plugins_localization_plugin_url, true, BuildConfig.MIN_SDK_VERSION)
);
currentCategory = R.id.nav_plugins;
break;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
package com.mapbox.mapboxandroiddemo.examples.plugins;

import android.os.Bundle;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.view.View;

import com.mapbox.mapboxandroiddemo.R;
import com.mapbox.mapboxsdk.Mapbox;
import com.mapbox.mapboxsdk.camera.CameraPosition;
import com.mapbox.mapboxsdk.camera.CameraUpdateFactory;
import com.mapbox.mapboxsdk.geometry.LatLng;
import com.mapbox.mapboxsdk.maps.MapView;
import com.mapbox.mapboxsdk.maps.MapboxMap;
import com.mapbox.mapboxsdk.maps.OnMapReadyCallback;
import com.mapbox.mapboxsdk.plugins.localization.LocalizationPlugin;
import com.mapbox.mapboxsdk.plugins.localization.MapLocale;

/**
* Use the localization plugin to retrieve the device's language and set all map text labels to that language.
*/
public class LocalizationPluginActivity extends AppCompatActivity implements OnMapReadyCallback {

private MapView mapView;
private LocalizationPlugin localizationPlugin;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

// Mapbox access token is configured here. This needs to be called either in your application
// object or in the same activity which contains the mapview.
Mapbox.getInstance(this, getString(R.string.access_token));

// This contains the MapView in XML and needs to be called after the access token is configured.
setContentView(R.layout.activity_localization_plugin);
mapView = findViewById(R.id.mapView);
mapView.onCreate(savedInstanceState);
mapView.getMapAsync(this);
}

@Override
public void onMapReady(final MapboxMap mapboxMap) {

localizationPlugin = new LocalizationPlugin(mapView, mapboxMap);

findViewById(R.id.language_one_cardview).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
localizationPlugin.setMapLanguage(MapLocale.ARABIC);
}
});


Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we get rid of the double spacings between click listeners?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

findViewById(R.id.language_two_cardview).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
localizationPlugin.setMapLanguage(MapLocale.RUSSIAN);
}
});


findViewById(R.id.language_three_cardview).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
localizationPlugin.setMapLanguage(MapLocale.SIMPLIFIED_CHINESE);
}
});

findViewById(R.id.match_map_to_device_language).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {

Snackbar.make(view, R.string.change_device_language_instruction, Snackbar.LENGTH_LONG).show();

try {
localizationPlugin.matchMapLanguageWithDeviceDefault();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what is potentially throwing the null point exception here? Isn't this a runtime exception?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed to runtime exception for when mapLocale==null in the LocalizationPlugin class. I also changed the snackbar message because it doesn't need to show the actual exception message. Now it's

catch (RuntimeException exception) {
          Snackbar.make(view, R.string.try_different_language_instruction, Snackbar.LENGTH_LONG).show();
        }

with try_different_language_instruction being

<string name="try_different_language_instruction">Try setting your phone to a different language. German or French perhaps?</string>

👇

device-2018-03-07-144549

CameraPosition position = new CameraPosition.Builder()
.target(new LatLng(34.032666, -80.363160))
.zoom(2.038777)
.build();

mapboxMap.animateCamera(CameraUpdateFactory
.newCameraPosition(position), 1000);
} catch (NullPointerException exception) {
Snackbar.make(view, exception.toString(), Snackbar.LENGTH_LONG).show();
}
}
});

}

// Add the mapView lifecycle to the activity's lifecycle methods
@Override
public void onResume() {
super.onResume();
mapView.onResume();
}

@Override
protected void onStart() {
super.onStart();
mapView.onStart();
}

@Override
protected void onStop() {
super.onStop();
mapView.onStop();
}

@Override
public void onPause() {
super.onPause();
mapView.onPause();
}

@Override
public void onLowMemory() {
super.onLowMemory();
mapView.onLowMemory();
}

@Override
protected void onDestroy() {
super.onDestroy();
mapView.onDestroy();
}

@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
mapView.onSaveInstanceState(outState);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24.0"
android:viewportHeight="24.0">
<path
android:fillColor="#FFFFFF"
android:pathData="M6.99,11L3,15l3.99,4v-3H14v-2H6.99v-3zM21,9l-3.99,-4v3H10v2h7.01v3L21,9z"/>
</vector>
120 changes: 120 additions & 0 deletions MapboxAndroidDemo/src/main/res/layout/activity_localization_plugin.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:mapbox="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

frame layouts don't have orientation

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed


<com.mapbox.mapboxsdk.maps.MapView
android:id="@+id/mapView"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:mapbox_cameraTargetLat="40.745565"
app:mapbox_cameraTargetLng="-73.982058"
app:mapbox_cameraZoom="11.316102"
app:mapbox_styleUrl="@string/mapbox_style_mapbox_streets">

</com.mapbox.mapboxsdk.maps.MapView>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cleaner to />

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed


<android.support.constraint.ConstraintLayout
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not just make the entire layout a constraintLayout? No need to have a FrameLayout as the parent.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's what I originally had, but then decided to have the instructional cardview and buttons to be on top of the map in a frame layout, rather than the top of the map ending at the bottom of the buttons in a constraint layout...

android:layout_width="match_parent"
android:layout_height="wrap_content">

<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/instruction_cardview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:backgroundTint="@color/mapboxBlueDark"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:text="@string/vacation_description"
android:textColor="@color/mapboxWhite" />

</android.support.v7.widget.CardView>

<android.support.v7.widget.CardView
android:id="@+id/language_one_cardview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:backgroundTint="@color/mapboxOrangeDark"
app:layout_constraintEnd_toStartOf="@+id/language_two_cardview"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/instruction_cardview">

<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="16dp"
android:text="@string/arabic"
android:textColor="@color/mapboxWhite" />

</android.support.v7.widget.CardView>

<android.support.v7.widget.CardView
android:id="@+id/language_two_cardview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:backgroundTint="@color/mapboxPurpleDark"
app:layout_constraintEnd_toStartOf="@+id/language_three_cardview"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/language_one_cardview"
app:layout_constraintTop_toTopOf="@id/language_one_cardview">

<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="16dp"
android:text="@string/russian"
android:textColor="@color/mapboxWhite" />

</android.support.v7.widget.CardView>


<android.support.v7.widget.CardView
android:id="@+id/language_three_cardview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:backgroundTint="@color/mapboxGreenDark"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toEndOf="@+id/language_two_cardview"
app:layout_constraintTop_toTopOf="@id/language_two_cardview">

<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="16dp"
android:text="@string/mandarin"
android:textColor="@color/mapboxWhite" />

</android.support.v7.widget.CardView>

</android.support.constraint.ConstraintLayout>

<android.support.design.widget.FloatingActionButton
android:id="@+id/match_map_to_device_language"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|right"
android:layout_marginBottom="16dp"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:srcCompat="@drawable/ic_swap_horiz_white_24dp"
mapbox:fabSize="small" />


</FrameLayout>
12 changes: 12 additions & 0 deletions MapboxAndroidDemo/src/main/res/values/activity_strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,18 @@
<string name="load_raster_button_text">Load custom raster style</string>
<string name="load_local_style_button_text">Load local style</string>

<!-- Localization plugin -->
<string name="map_not_localized">Map not localized to device language and now set to French</string>
<string name="map_localized">Map localized to device language</string>
<string name="camera_bounds_focus">Camera bounds for country associated with language tag: %1$s</string>
<string name="change_language_instruction">Make sure that the device\'s default language is not English</string>
<string name="mandarin">Mandarin</string>
<string name="russian">Russian</string>
<string name="arabic">Arabic</string>
<string name="change_device_language_instruction">Change the device\'s language to German and then tap this button. Already in German? Try French</string>
<string name="vacation_description">Uh-oh! You\'re lost in NYC. Tap the buttons below to
switch the map\'s language so that you can ask for help with directions.</string>

<!-- Matrix API -->
<string name="call_error">Oh no! The call to the Directions Matrix API failed!</string>
<string name="miles_distance">%1$s miles</string>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
<string name="activity_plugins_geojson_plugin_description">Easily retrieve GeoJSON data from a url, asset, or path</string>
<string name="activity_plugins_places_plugin_description">Add location search ("geocoding") functionality and UI to search for any place in the world</string>
<string name="activity_plugins_geojson_marker_clusters_description">Use the plugin to automatically add marker clusters to the map. This example shows bike share stations in Paris.</string>
<string name="activity_plugins_localization_plugin_description">Use the plugin to automatically change map label text to the language set on the device.</string>
<string name="activity_java_services_simplify_polyline_description">Using the polylines utility, simplify a polyline which reduces the amount of coordinates making up the polyline depending on tolerance.</string>
<string name="activity_java_services_map_matching_description">Match raw GPS points to the map so they aligns with the roads/pathways.</string>
<string name="activity_image_generator_snapshot_share_description">Send and share a map snapshot image.</string>
Expand Down
1 change: 1 addition & 0 deletions MapboxAndroidDemo/src/main/res/values/titles_strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
<string name="activity_plugins_traffic_plugin_title">Display real-time traffic</string>
<string name="activity_plugins_building_plugin_title">Display buildings in 3D</string>
<string name="activity_plugins_location_plugin_title">Show a user\'s location</string>
<string name="activity_plugins_localization_plugin_title">Change map text to device language</string>
<string name="activity_plugins_geojson_plugin_title">Load GeoJSON data</string>
<string name="activity_plugins_places_plugin_title">Location search</string>
<string name="activity_plugins_marker_clusters_plugin_title">Add marker clusters</string>
Expand Down
1 change: 1 addition & 0 deletions MapboxAndroidDemo/src/main/res/values/urls_strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
<string name="activity_plugins_places_plugin_url" translatable="false">https://i.imgur.com/oKHx3bv.png</string>
<string name="activity_plugins_geojson_plugin_url" translatable="false">http://i.imgur.com/kju0sKw.jpg</string>
<string name="activity_plugins_markers_clusters_plugin_url" translatable="false">https://i.imgur.com/P6wsuea.jpg</string>
<string name="activity_plugins_localization_plugin_url" translatable="false">https://i.imgur.com/vi3D3qf.png</string>
<string name="activity_java_services_simplify_polyline_url" translatable="false">http://i.imgur.com/uATgul1.png</string>
<string name="activity_java_services_map_matching_url" translatable="false">http://i.imgur.com/bWvVbwC.png</string>
<string name="activity_image_generator_snapshot_notification_url" translatable="false">https://i.imgur.com/OiveDFG.png</string>
Expand Down
Loading