Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Maps v2 change language

The problem is that the google maps retain the language it has when it was first open. For example, I have 2 activities (Activity A and Activity B). The Activity A consist of settings page where I change the language to that activity. Note that it's not the system language, but using the Locale.setDefault(). This successfully translates my app, no problem with the text, except the google maps language. The Activity B consists of the MapFragment where I show the map.

When I start the MapActivity on English language, the map shows the English language. When I go back to Activity A and change the language, then open again the MapActivity, the map retains the English Language as its like saving its instance from a background process.

My investigation is that, the google maps language was based on the first language the maps rendered and it will retain unless the app is completely close.

Can someone help me with this problem to change the google maps language base on the Locale set in my Settings page. What I've tried so far is to clear the cache(my app and the google services) and also destroying the fragments by activating the useViewLifeCycleInFragments of the SupportMapFragment and use the fragment life cycle.

Thanks.

Update

Still cant get to translate the Google Maps:

What I have tried:

  1. Close the google play services process -> not working (impossible)

  2. Use 2 Map Fragment -> not working, it will load same maps

like image 293
Rashid Avatar asked May 10 '16 07:05

Rashid


People also ask

Why can't I change the language on Google Maps?

Google Maps on the web: Click the menu in the top left, then click Language and select any language to set it. Google Maps app for Android: Tap your profile picture in the top right, tap Settings > Navigation settings > Voice selection > a language. To also adjust the text language go to Settings > App language.

Why is my Google Maps talking in a different language?

Here is how to change Google Maps navigation language: Open the Google Maps app, on your Android phone or tablet, Now tap on your profile picture or initial Account Circle and then Settings. Then tap on Navigation settings and then on Voice. Choose a voice and language.


1 Answers

The problem is the Tile caching of google map. What you should do is using Tile Overlays and Tile Provider

Google put some sample for you in the links i provided.

you should get tiles by yourself and use imageloader or picaso library, then disable caching in their setups.

By doing this you always get new tiles by the local language you provided

Sample :

import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.TileOverlay;
import com.google.android.gms.maps.model.TileOverlayOptions;
import com.google.android.gms.maps.model.TileProvider;
import com.google.android.gms.maps.model.UrlTileProvider;

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

import java.net.MalformedURLException;
import java.net.URL;
import java.util.Locale;

/**
 * This demonstrates how to add a tile overlay to a map.
 */
public class TileOverlayDemoActivity extends AppCompatActivity implements OnMapReadyCallback {

/** This returns moon tiles. */
private static final String MOON_MAP_URL_FORMAT =
        "http://mw1.google.com/mw-planetary/lunar/lunarmaps_v1/clem_bw/%d/%d/%d.jpg";

private TileOverlay mMoonTiles;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.tile_overlay_demo);

    SupportMapFragment mapFragment =
            (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);
}

@Override
public void onMapReady(GoogleMap map) {
    map.setMapType(GoogleMap.MAP_TYPE_NONE);

    TileProvider tileProvider = new UrlTileProvider(256, 256) {
        @Override
        public synchronized URL getTileUrl(int x, int y, int zoom) {
            // The moon tile coordinate system is reversed.  This is not normal.
            int reversedY = (1 << zoom) - y - 1;
            String s = String.format(Locale.US, MOON_MAP_URL_FORMAT, zoom, x, reversedY);
            URL url = null;
            try {
                url = new URL(s);
            } catch (MalformedURLException e) {
                throw new AssertionError(e);
            }
            return url;
        }
    };

    mMoonTiles = map.addTileOverlay(new TileOverlayOptions().tileProvider(tileProvider));
}

public void setFadeIn(View v) {
    if (mMoonTiles == null) {
        return;
    }
    mMoonTiles.setFadeIn(((CheckBox) v).isChecked());
}
}

what you should do is changing the Local in OnMapReady in Tile Provider

like image 131
Omid Heshmatinia Avatar answered Oct 11 '22 18:10

Omid Heshmatinia