Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/195.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
反向地理编码在某些android设备中不起作用?_Android_Google Maps_Google Geocoder - Fatal编程技术网

反向地理编码在某些android设备中不起作用?

反向地理编码在某些android设备中不起作用?,android,google-maps,google-geocoder,Android,Google Maps,Google Geocoder,我正在地图上开发一个应用程序,我无法在这个手机上获得地址,它的android版本是4.3,如下所示-- 但它在我的手机上运行良好,它的版本是4.1.2,如下所示-- 它在一些棒棒糖版本中运行良好 final Geocoder gc = new Geocoder(this, Locale.getDefault()); try { List<Address> addresses = gc.getFromLocation(lat, lng, 1);

我正在地图上开发一个应用程序,我无法在这个手机上获得地址,它的android版本是4.3,如下所示--

但它在我的手机上运行良好,它的版本是4.1.2,如下所示--

它在一些棒棒糖版本中运行良好

    final Geocoder gc = new Geocoder(this, Locale.getDefault());
    try {
        List<Address> addresses = gc.getFromLocation(lat, lng, 1);
        StringBuilder sb = new StringBuilder();
        if (addresses.size() > 0) {
            Address address = addresses.get(0);
            for (int i = 0; i < address.getMaxAddressLineIndex(); i++) {
                if (address.getAddressLine(i).equals("null")) {

                } else {
                    sb.append(address.getAddressLine(i)).append("\n");
                    sb.append(address.getLocality()).append("\n");
                    //sb.append(address.getPostalCode()).append("\n");
                    //sb.append(address.getCountryName());
                }
            }
           // Toast.makeText(RegistrationTest.this, "Text Address is " + sb.toString(), Toast.LENGTH_SHORT).show();
            text_address = sb.toString();
        }
    } catch (Exception e) {
        //Toast.makeText(RegistrationTest.this, "exception " + e, Toast.LENGTH_SHORT).show();
    }
final Geocoder gc=new Geocoder(这个,Locale.getDefault());
试一试{
列表地址=gc.getFromLocation(lat,lng,1);
StringBuilder sb=新的StringBuilder();
如果(地址.size()>0){
地址=地址。获取(0);
对于(int i=0;i
对不起我的英语,谢谢你的时间和帮助


请帮帮我,我被困在这里了

并非所有制造商都使用地理编码器。这可能是它无法在特定设备上工作的原因之一。从文件中:

Geocoder类需要一个后端服务,该服务不包含在核心android框架中。如果平台中没有后端服务,Geocoder查询方法将返回空列表

或者可能有一个请求配额


相反,您可以使用远程服务,如(也可以使用配额)。

地理编码器在某些设备中不起作用。因此,我们需要创建一个自定义地理编码器。您可以检查Geocoder是否由设备制造商使用,但该方法不可靠。我们不能保证地理编码器是用它来实现的

对于自定义地理编码器,您可以使用以下类,其用法与使用地理编码器完全相同:

import android.location.Address;
import android.util.Log;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class MyGeocoder {

  public static final String TAG = MyGeocoder.class.getSimpleName();

  static OkHttpClient client = new OkHttpClient();

  public static List<Address> getFromLocation(double lat, double lng, int maxResult) {

    String address = String.format(Locale.US,
        "https://maps.googleapis.com/maps/api/geocode/json?latlng=%1$f,%2$f&sensor=false&language="
            + Locale.getDefault().getCountry(), lat, lng);
    Log.d(TAG, "address = " + address);
    Log.d(TAG, "Locale.getDefault().getCountry() = " + Locale.getDefault().getCountry());

    return getAddress(address, maxResult);

  }

  public static List<Address> getFromLocationName(String locationName, int maxResults)  {

    String address = null;
    try {
      address = "https://maps.google.com/maps/api/geocode/json?address=" + URLEncoder.encode(locationName,
          "UTF-8") + "&ka&sensor=false";
      return getAddress(address, maxResults);
    } catch (UnsupportedEncodingException e) {
      e.printStackTrace();
    }
    return null;
  }

  private static List<Address> getAddress(String url, int maxResult) {
    List<Address> retList = null;

    Request request = new Request.Builder().url(url)
        .header("User-Agent", "OkHttp Headers.java")
        .addHeader("Accept", "application/json; q=0.5")
        .build();
    try {
      Response response = client.newCall(request).execute();
      String responseStr = response.body().string();
      JSONObject jsonObject = new JSONObject(responseStr);

      retList = new ArrayList<Address>();

      if ("OK".equalsIgnoreCase(jsonObject.getString("status"))) {
        JSONArray results = jsonObject.getJSONArray("results");
        if (results.length() > 0) {
          for (int i = 0; i < results.length() && i < maxResult; i++) {
            JSONObject result = results.getJSONObject(i);
            Address addr = new Address(Locale.getDefault());

            JSONArray components = result.getJSONArray("address_components");
            String streetNumber = "";
            String route = "";
            for (int a = 0; a < components.length(); a++) {
              JSONObject component = components.getJSONObject(a);
              JSONArray types = component.getJSONArray("types");
              for (int j = 0; j < types.length(); j++) {
                String type = types.getString(j);
                if (type.equals("locality")) {
                  addr.setLocality(component.getString("long_name"));
                } else if (type.equals("street_number")) {
                  streetNumber = component.getString("long_name");
                } else if (type.equals("route")) {
                  route = component.getString("long_name");
                }
              }
            }
            addr.setAddressLine(0, route + " " + streetNumber);

            addr.setLatitude(
                result.getJSONObject("geometry").getJSONObject("location").getDouble("lat"));
            addr.setLongitude(
                result.getJSONObject("geometry").getJSONObject("location").getDouble("lng"));
            retList.add(addr);
          }
        }
      }
    } catch (IOException e) {
      Log.e(TAG, "Error calling Google geocode webservice.", e);
    } catch (JSONException e) {
      Log.e(TAG, "Error parsing Google geocode webservice response.", e);
    }

    return retList;
  }
}
导入android.location.Address;
导入android.util.Log;
导入java.io.IOException;
导入java.io.UnsupportedEncodingException;
导入java.net.urlcoder;
导入java.util.ArrayList;
导入java.util.List;
导入java.util.Locale;
导入okhttp3.OkHttpClient;
导入okhttp3.请求;
导入okhttp3.响应;
导入org.json.JSONArray;
导入org.json.JSONException;
导入org.json.JSONObject;
公共类MyGeocoder{
public static final String TAG=MyGeocoder.class.getSimpleName();
静态OkHttpClient=new OkHttpClient();
公共静态列表getFromLocation(双lat、双lng、int maxResult){
字符串地址=String.format(Locale.US,
"https://maps.googleapis.com/maps/api/geocode/json?latlng=%1$f,%2$f和传感器=错误和语言=”
+Locale.getDefault().getCountry(),lat,lng);
Log.d(标签,“地址=”+地址);
Log.d(标记“Locale.getDefault().getCountry()=”+Locale.getDefault().getCountry());
返回getAddress(地址,maxResult);
}
公共静态列表getFromLocationName(字符串locationName,int-maxResults){
字符串地址=空;
试一试{
地址=”https://maps.google.com/maps/api/geocode/json?address=“+URLEncoder.encode(位置名称,
“UTF-8”+“&ka&sensor=false”;
返回getAddress(地址,maxResults);
}捕获(不支持的编码异常e){
e、 printStackTrace();
}
返回null;
}
私有静态列表getAddress(字符串url,int-maxResult){
List-retList=null;
请求=新请求.Builder().url(url)
.header(“用户代理”,“OkHttp Headers.java”)
.addHeader(“接受”、“应用程序/json;q=0.5”)
.build();
试一试{
Response=client.newCall(request.execute();
String responsest=response.body().String();
JSONObject JSONObject=新的JSONObject(responsest);
retList=newarraylist();
if(“OK”.equalsIgnoreCase(jsonObject.getString(“status”)){
JSONArray results=jsonObject.getJSONArray(“结果”);
如果(results.length()>0){
对于(int i=0;i