Skip to content

Commit

Permalink
Basic map localization support added
Browse files Browse the repository at this point in the history
  • Loading branch information
Cameron Mace committed Feb 21, 2018
1 parent 4cb4a74 commit a65da9c
Show file tree
Hide file tree
Showing 20 changed files with 1,030 additions and 2 deletions.
1 change: 1 addition & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ dependencies {
implementation project(':plugin-cluster')
implementation project(':plugin-places')
implementation project(':plugin-offline')
implementation project(':plugin-localization')
}

sonarqube {
Expand Down
12 changes: 12 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,18 @@
android:value=".activity.FeatureOverviewActivity"/>
</activity>

<activity
android:name=".activity.LocalizationActivity"
android:description="@string/description_localization"
android:label="@string/title_localization">
<meta-data
android:name="@string/category"
android:value="@string/category_localization"/>
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".activity.FeatureOverviewActivity"/>
</activity>

<activity android:name="com.mapbox.maboxsdk.plugins.SingleFragmentActivity"/>

<service
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
package com.mapbox.mapboxsdk.plugins.testapp.activity;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;

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;
import com.mapbox.mapboxsdk.plugins.testapp.R;
import com.mapbox.mapboxsdk.plugins.testapp.Utils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class LocalizationActivity extends AppCompatActivity implements OnMapReadyCallback {

@BindView(R.id.mapView)
MapView mapView;

private LocalizationPlugin localizationPlugin;
private MapboxMap mapboxMap;
private boolean mapIsLocalized;

private static final MapLocale[] LOCALES = new MapLocale[] {
MapLocale.CANADA,
MapLocale.GERMANY,
MapLocale.CHINA,
MapLocale.US,
MapLocale.CANADA_FRENCH,
MapLocale.ITALY,
MapLocale.JAPAN,
MapLocale.KOREA,
MapLocale.FRANCE
};

private static int index;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_localization);
ButterKnife.bind(this);
mapIsLocalized = true;
Toast.makeText(this, R.string.change_language_instruction, Toast.LENGTH_LONG).show();
mapView.onCreate(savedInstanceState);
mapView.getMapAsync(this);
}

@Override
public void onMapReady(MapboxMap mapboxMap) {
this.mapboxMap = mapboxMap;
localizationPlugin = new LocalizationPlugin(mapView, mapboxMap);
localizationPlugin.matchMapLanguageWithDeviceDefault();
}

@OnClick(R.id.localize_fab)
public void localizeToggleFab() {
if (mapIsLocalized) {
localizationPlugin.setMapLanguage(new MapLocale(MapLocale.FRENCH));
Toast.makeText(this, R.string.map_not_localized, Toast.LENGTH_SHORT).show();
mapIsLocalized = false;
} else {
localizationPlugin.matchMapLanguageWithDeviceDefault();
Toast.makeText(this, R.string.map_localized, Toast.LENGTH_SHORT).show();
mapIsLocalized = true;
}
}

@OnClick(R.id.camera_localization)
public void localizeCameraFab() {
MapLocale locale = getNextMapLocale();
localizationPlugin.setMapLanguage(locale);
localizationPlugin.setCameraToLocaleCountry(locale);
}

@OnClick(R.id.change_map_style)
public void changeMapStyleFab() {
if (mapboxMap != null) {
mapboxMap.setStyleUrl(Utils.getNextStyle());
}
}

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

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

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

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

@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
mapView.onSaveInstanceState(outState);
}

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

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

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_languages, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.english:
localizationPlugin.setMapLanguage(MapLocale.ENGLISH);
return true;
case R.id.spanish:
localizationPlugin.setMapLanguage(MapLocale.SPANISH);
return true;
case R.id.french:
localizationPlugin.setMapLanguage(MapLocale.FRENCH);
return true;
case R.id.german:
localizationPlugin.setMapLanguage(MapLocale.GERMAN);
return true;
case R.id.russian:
localizationPlugin.setMapLanguage(MapLocale.RUSSIAN);
return true;
case R.id.chinese:
localizationPlugin.setMapLanguage(MapLocale.CHINESE);
return true;
case R.id.simplified_chinese:
localizationPlugin.setMapLanguage(MapLocale.SIMPLIFIED_CHINESE);
return true;
case R.id.portuguese:
localizationPlugin.setMapLanguage(MapLocale.PORTUGUESE);
return true;
case R.id.arabic:
localizationPlugin.setMapLanguage(MapLocale.ARABIC);
return true;
case android.R.id.home:
finish();
return true;
}
return super.onOptionsItemSelected(item);
}

public static MapLocale getNextMapLocale() {
index++;
if (index == LOCALES.length) {
index = 0;
}
return LOCALES[index];
}
}

12 changes: 12 additions & 0 deletions app/src/main/res/drawable/ic_camera.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<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="#FF000000"
android:pathData="M12,12m-3.2,0a3.2,3.2 0,1 1,6.4 0a3.2,3.2 0,1 1,-6.4 0"/>
<path
android:fillColor="#FF000000"
android:pathData="M9,2L7.17,4L4,4c-1.1,0 -2,0.9 -2,2v12c0,1.1 0.9,2 2,2h16c1.1,0 2,-0.9 2,-2L22,6c0,-1.1 -0.9,-2 -2,-2h-3.17L15,2L9,2zM12,17c-2.76,0 -5,-2.24 -5,-5s2.24,-5 5,-5 5,2.24 5,5 -2.24,5 -5,5z"/>
</vector>
9 changes: 9 additions & 0 deletions app/src/main/res/drawable/ic_translate_white_24dp.xml
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="M12.87,15.07l-2.54,-2.51 0.03,-0.03c1.74,-1.94 2.98,-4.17 3.71,-6.53L17,6L17,4h-7L10,2L8,2v2L1,4v1.99h11.17C11.5,7.92 10.44,9.75 9,11.35 8.07,10.32 7.3,9.19 6.69,8h-2c0.73,1.63 1.73,3.17 2.98,4.56l-5.09,5.02L4,19l5,-5 3.11,3.11 0.76,-2.04zM18.5,10h-2L12,22h2l1.12,-3h4.75L21,22h2l-4.5,-12zM15.88,17l1.62,-4.33L19.12,17h-3.24z"/>
</vector>
65 changes: 65 additions & 0 deletions app/src/main/res/layout/activity_localization.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<com.mapbox.mapboxsdk.maps.MapView
android:id="@+id/mapView"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:mapbox_cameraTargetLat="51.505300"
app:mapbox_cameraTargetLng="-0.075073"
app:mapbox_cameraTilt="20"
app:mapbox_cameraZoom="2.5"
app:mapbox_styleUrl="@string/mapbox_style_mapbox_streets">

</com.mapbox.mapboxsdk.maps.MapView>

<android.support.design.widget.FloatingActionButton
android:id="@+id/localize_fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp"
android:src="@drawable/ic_translate_white_24dp"
app:backgroundTint="@color/colorPrimary"
app:fabSize="normal"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"/>

<android.support.design.widget.FloatingActionButton
android:id="@+id/change_map_style"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp"
android:src="@drawable/ic_layers"
android:tint="@android:color/white"
app:backgroundTint="@color/colorAccent"
app:fabSize="normal"
app:layout_constraintBottom_toTopOf="@+id/camera_localization"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintRight_toRightOf="parent"/>

<android.support.design.widget.FloatingActionButton
android:id="@+id/camera_localization"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp"
android:src="@drawable/ic_camera"
android:tint="@android:color/white"
app:backgroundTint="@color/colorAccent"
app:fabSize="normal"
app:layout_constraintBottom_toTopOf="@+id/localize_fab"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintRight_toRightOf="parent"/>

</android.support.constraint.ConstraintLayout>
52 changes: 52 additions & 0 deletions app/src/main/res/menu/menu_languages.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">

<item
android:id="@+id/english"
android:title="English"
app:showAsAction="never"/>

<item
android:id="@+id/spanish"
android:title="Spanish"
app:showAsAction="never"/>

<item
android:id="@+id/french"
android:title="French"
app:showAsAction="never"/>

<item
android:id="@+id/german"
android:title="German"
app:showAsAction="never"/>

<item
android:id="@+id/russian"
android:title="Russian"
app:showAsAction="never"/>

<item
android:id="@+id/chinese"
android:title="Chinese"
app:showAsAction="never"/>

<item
android:id="@+id/simplified_chinese"
android:title="Simplified Chinese"
app:showAsAction="never"/>


<item
android:id="@+id/portuguese"
android:title="Portuguese"
app:showAsAction="never"/>

<item
android:id="@+id/arabic"
android:title="Arabic"
app:showAsAction="never"/>


</menu>
8 changes: 7 additions & 1 deletion app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<resources xmlns:tools="http://schemas.android.com/tools">
<string name="app_name" tools:ignore="PrivateResource">Map Plugins</string>
<string name="app_name" tools:ignore="PrivateResource">Map Plugins</string>

<!-- Categories -->
<string name="category">category</string>
Expand All @@ -10,6 +10,7 @@
<string name="category_annotations">Annotations</string>
<string name="category_places">Places</string>
<string name="category_offline">Offline</string>
<string name="category_localization">Localization</string>

<!-- Titles -->
<string name="title_traffic">Traffic Plugin</string>
Expand All @@ -25,6 +26,7 @@
<string name="title_offline_plugin">Create region</string>
<string name="title_offline_regions">List regions</string>
<string name="title_place_picker">Place picker</string>
<string name="title_localization">Localization Plugin</string>

<!-- Descriptions -->
<string name="description_traffic">Add Traffic layers to any Mapbox basemap.</string>
Expand All @@ -40,6 +42,7 @@
<string name="description_offline_plugin">Use a form to create an offline region.</string>
<string name="description_offline_regions">List all offline regions.</string>
<string name="description_place_picker">Launch the place picker activity and receive result</string>
<string name="description_localization">Automatically localize the map labels into the device\'s set language.</string>

<!-- Buttons -->
<string name="button_location_mode_none">None</string>
Expand All @@ -50,6 +53,9 @@
<!-- Random -->
<string name="min_zoom_textview">Min zoom: %1$d</string>
<string name="max_zoom_textview">Max zoom: %1$d</string>
<string name="change_language_instruction">Make sure that the device\'s default language is not English</string>
<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>

<!-- Place picker -->
<string name="detailed_description_place_picker">Example shows how to launch the Place Picker using the Floating action button and receiving a result in onActivityResult.</string>
Expand Down
Loading

0 comments on commit a65da9c

Please sign in to comment.