问题是谷歌地图保留了它第一次打开时的语言。例如,我有 2 个 Activity ( Activity A 和 Activity B)。 Activity A 由设置页面组成,我可以在其中更改该 Activity 的语言。请注意,这不是系统语言,而是使用 Locale.setDefault()
。这成功翻译了我的应用程序,除了谷歌地图语言外,文本没有问题。 Activity B 由我在其中显示 map 的 MapFragment 组成。
当我以英语 语言启动MapActivity 时, map 显示英语 语言。当我返回 Activity A 并更改语言,然后再次打开 MapActivity 时, map 保留了英语语言,就像从后台进程中保存其实例一样。
我的调查是,谷歌地图语言基于 map 呈现的第一种语言,除非应用完全关闭,否则它将保留。
谁能帮我解决这个问题,根据我的“设置”页面中设置的区域设置更改谷歌地图语言。到目前为止我尝试过的是清除缓存(我的应用程序和谷歌服务)并通过激活 SupportMapFragment
的 useViewLifeCycleInFragments
来销毁 fragment 并使用 fragment 生命周期。
谢谢。
更新
仍然无法翻译 Google map :
我尝试过的:
关闭 google play 服务进程 -> 不工作(不可能)
使用 2 个 map fragment -> 不工作,它将加载相同的 map
最佳答案
问题出在谷歌地图的 Tile 缓存上。 你应该做的是使用 Tile Overlays和 Tile Provider
Google 在我提供的链接中为您提供了一些示例。
您应该自己获取图 block 并使用 imageloader 或 picaso 库,然后在它们的设置中禁用缓存。
通过这样做,您总是会根据您提供的本地语言获得新的图 block
示例:
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());
}
}
你应该做的是改变 Tile Provider 中 OnMapReady 中的 Local
关于android - Google map v2 更改语言,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37132505/