如何在android中本地化Google地图,并使搜索URL接受其他语言的参数

时间:2018-07-08 12:26:47

标签: android google-maps google-api localization

我在我的应用上创建了由Google提供支持的autoCompleteFragment搜索,该搜索实现了所有Google Maps api。

我目前居住在塞浦路斯,我想测试我的申请。我的问题是,每当我发送requestURL时,由于语言差异,我都会得到一种奇怪的编码。我所在的国家/地区具有土耳其语而不是英语的位置地址,当我请求URL时,JSON返回FATAL EXCEPTION,并且我的应用由于该奇怪的编码而崩溃:

我该如何解决? :) 也许是一种获取requestURL以土耳其语发送的方法?

非常感谢你们,你是最好的。

首先是我的方法

String requestApi = null;
    try {
        requestApi = "https://maps.googleapis.com/maps/api/directions/json?" +
                "mode=driving&" +
                "transit_routing_preference=less_driving&" +
                "origin=" + currentPosition.latitude + "," + currentPosition.longitude + "&" +
                "destination=" + destination + "&" + "key=" + getResources().getString(R.string.google_direction_api);
        Log.d("TAG", "Current URL is="+requestApi);
        mService.getPath(requestApi)
                .enqueue(new Callback<String>()

这是奇怪的编码:

https://maps.googleapis.com/maps/api/directions/json?mode=driving&transit_routing_preference=less_driving&origin=35.3366855,33.27188&destination=+G+i+r+n+e+ + C + d + + N + o +:+ 1 +,+ + L + e + f + k + o +ş+ a +&key =“ MyGoogleAPIKEY”

而不是像这样 https://maps.googleapis.com/maps/api/directions/json?mode=driving&transit_routing_preference=less_driving&origin=35.3366855,33.27188&destination=+G+i+r+n+e+K+a+p+ı+s+ı+L+e+f+k+o+ş+a+&key=“ MyGoogleAPIKEY”

最后,我的幻想

07-08 15:02:40.294 29913-29913/com.example.karam.nlgcs D/KARAM: 
https://maps.googleapis.com/maps/api/directions/json? 
mode=driving&transit_routing_preference=less_driving&
origin=35.3366855,33.27188&destination=+G+i+r+n+e+ +C+d+ +N+o+:+1+,+ 
+L+e+f+k+o+ş+a+&key=MYGOOGLEAPIKEY ;)
07-08 15:02:41.042 29913-29913/com.example.karam.nlgcs D/AndroidRuntime: 
Shutting down VM
07-08 15:02:41.043 29913-29913/com.example.karam.nlgcs E/AndroidRuntime: 
FATAL EXCEPTION: main
Process: com.example.karam.nlgcs, PID: 29913
java.lang.IllegalStateException: no included points
at 
com.google.android.gms.common.internal.Preconditions.checkState(Unknown 
Source:8)
at com.google.android.gms.maps.model.LatLngBounds$Builder.build(Unknown 
Source:10)
at com.example.karam.nlgcs.Welcome$4.onResponse(Welcome.java:284)
at retrofit2.ExecutorCallAdapterFactory$ExecutorCallbackCall$1$1.run
(ExecutorCallAdapterFactory.java:70)
at android.os.Handler.handleCallback(Handler.java:789)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6938)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)

编辑 2018年7月10日

URL的建议编码会引发错误,因为google maps不接受编码URL字符串。我试图对其进行解码,并通过一种方法删除特殊字符的重复,但这也不起作用。

编辑 7/14/2018 我尝试对应用程序进行本地化,并使用其他语言发送目标变量,但仍然存在相同的错误。

1 个答案:

答案 0 :(得分:0)

URLEncoder应该是要走的路。您可以在创建Directions API URL时对目标变量的文本进行URL编码:

requestApi = "https://maps.googleapis.com/maps/api/directions/json?" +
            "mode=driving&" +
            "transit_routing_preference=less_driving&" +
            "origin=" + currentPosition.latitude + "," + currentPosition.longitude + "&" +
            "destination=" + URLEncoder.encode(destination, "UTF-8") + "&" + "key=" + getResources().getString(R.string.google_direction_api);

我希望这会有所帮助!