Rashid
Rashid

Reputation: 1740

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

Upvotes: 7

Views: 4010

Answers (2)

Omid Heshmatinia
Omid Heshmatinia

Reputation: 5236

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

Upvotes: 2

Teyam
Teyam

Reputation: 8082

First of all, you have to be aware of the supported languages and to take note of language codes used as detailed in Locale.

And to do it programmatically, solution was already given in this SO post - How to change language in android google maps v2 programmatically.

Please let us know if it work for you too. :)

Upvotes: 0

Related Questions