GVKun编程网logo

Android Geocoder getFromLocationName始终返回null(android getlocationonscreen)

11

对于想了解AndroidGeocodergetFromLocationName始终返回null的读者,本文将提供新的信息,我们将详细介绍androidgetlocationonscreen,并且为您提

对于想了解Android Geocoder getFromLocationName始终返回null的读者,本文将提供新的信息,我们将详细介绍android getlocationonscreen,并且为您提供关于android BluetoothDevice.getName()返回null、Android Geocoder.getFromLocationName停止使用边界、android getlastknownlocation在模拟器中始终为null、Android getlastknownlocation返回null的有价值信息。

本文目录一览:

Android Geocoder getFromLocationName始终返回null(android getlocationonscreen)

Android Geocoder getFromLocationName始终返回null(android getlocationonscreen)

我一直在尝试对字符串进行地址解析以获取其坐标,但是我的程序总是崩溃,因为每次尝试使用getFromLocationName()它时,它都会返回null。我已经尝试修复了几个小时,但没有任何反应。这是我的代码

public class MainActivity extends Activity {    private GoogleMap mMap;    List<Address> addresses;    MarkerOptions miami;    String myLocation = "Miami,Florida";    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        if (mMap == null) {            mMap = ((MapFragment) getFragmentManager().findFragmentById(                    R.id.map)).getMap();        }        if (mMap != null) {            Geocoder geocoder = new Geocoder(this);            double latitude = 0;            double longitude = 0;            while(addresses==null){            try {                addresses = geocoder.getFromLocationName(myLocation, 1);            } catch (IOException e) {                // TODO Auto-generated catch block                e.printStackTrace();            }            }            Address address = addresses.get(0);            if (addresses.size() > 0) {                latitude = address.getLatitude();                longitude = address.getLongitude();            }            LatLng City = new LatLng(latitude, longitude);            miami = new MarkerOptions().position(City).title("Miami");            mMap.addMarker(miami);            mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(City, 15));        }}

答案1

小编典典

Geocoder并不总是返回值。您可以尝试在for循环中发送3次请求。我应该至少可以返回一次。如果不是这样,则可能是连接问题,也可能是其他问题,例如服务器不回复您的请求。尝试看看这些线程:

Geocoder并不总是返回值,而geocoder.getFromLocationName仅返回null

更新:

我也有一个while循环,但是我曾经尝试最多10次。有时,即使连接到互联网,它也从不返回任何内容。然后,我使用这种可靠得多的方法来每次获取地址:

public JSONObject getLocationInfo() {        HttpGet httpGet = new HttpGet("http://maps.google.com/maps/api/geocode/json?latlng="+lat+","+lng+"&sensor=true");        HttpClient client = new DefaultHttpClient();        HttpResponse response;        StringBuilder stringBuilder = new StringBuilder();        try {            response = client.execute(httpGet);            HttpEntity entity = response.getEntity();            InputStream stream = entity.getContent();            int b;            while ((b = stream.read()) != -1) {                stringBuilder.append((char) b);            }        } catch (ClientProtocolException e) {            } catch (IOException e) {        }        JSONObject jsonObject = new JSONObject();        try {            jsonObject = new JSONObject(stringBuilder.toString());        } catch (JSONException e) {            e.printStackTrace();        }        return jsonObject;    }

我这样称呼它:

JSONObject ret = getLocationInfo(); JSONObject location;String location_string;try {    location = ret.getJSONArray("results").getJSONObject(0);    location_string = location.getString("formatted_address");    Log.d("test", "formattted address:" + location_string);} catch (JSONException e1) {    e1.printStackTrace();}

希望这可以帮助。我也厌倦了依赖地理编码器。这对我有用。如果用经纬度坐标替换URL,并在Web浏览器中看到返回的JSON对象。您会看到刚刚发生的事情。

android BluetoothDevice.getName()返回null

android BluetoothDevice.getName()返回null

有时,BluetoothDevice.getName()返回null.我该如何解决?
remoteDeviceName在以下代码中可能为null.我需要通过remoteDeviceName来区分我的设备和其他设备.

BluetoothAdapter.getDefaultAdapter().startLeScan(new LeScanCallback() {
            @Override
            public void onLeScan(final BluetoothDevice device,final int RSSi,byte[] scanRecord) {
                    String remoteDeviceName = device.getName();
                  Log.d("Scanning","scan device " + remoteDeviceName);
            });

解决方法

最后,我找到了解决方案:

1.对于连接的设备:

从服务org.bluetooth.service.generic_access的gatt特性org.bluetooth.characteristic.gap.device_name读取设备名称.

2.对于未连接的设备:

/**
     * Get device name from ble advertised data
     */
    private LeScanCallback mScanCb = new LeScanCallback() {
        @Override
        public void onLeScan(final BluetoothDevice device,byte[] scanRecord) {
            final BleAdvertisedData badata = bleutil.parseAdertisedData(scanRecord);
            String deviceName = device.getName();
            if( deviceName == null ){
                deviceName = badata.getName();
            }
    }


////////////////////// Helper Classes: bleutil and BleAdvertisedData ///////////////
    final public class bleutil {        
        private final static String TAG=bleutil.class.getSimpleName();
        public static BleAdvertisedData parseAdertisedData(byte[] advertisedData) {      
            List<UUID> uuids = new ArrayList<UUID>();
            String name = null;
            if( advertisedData == null ){
                return new BleAdvertisedData(uuids,name);
            }

            ByteBuffer buffer = ByteBuffer.wrap(advertisedData).order(ByteOrder.LITTLE_ENDIAN);
            while (buffer.remaining() > 2) {
                byte length = buffer.get();
                if (length == 0) break;

                byte type = buffer.get();
                switch (type) {
                    case 0x02: // Partial list of 16-bit UUIDs
                    case 0x03: // Complete list of 16-bit UUIDs
                        while (length >= 2) {
                            uuids.add(UUID.fromString(String.format(
                                    "%08x-0000-1000-8000-00805f9b34fb",buffer.getShort())));
                            length -= 2;
                        }
                        break;
                    case 0x06: // Partial list of 128-bit UUIDs
                    case 0x07: // Complete list of 128-bit UUIDs
                        while (length >= 16) {
                            long lsb = buffer.getLong();
                            long msb = buffer.getLong();
                            uuids.add(new UUID(msb,lsb));
                            length -= 16;
                         }
                        break;
                    case 0x09:
                        byte[] nameBytes = new byte[length-1];
                        buffer.get(nameBytes);
                        try {
                            name = new String(nameBytes,"utf-8");
                        } catch (UnsupportedEncodingException e) {
                            e.printstacktrace();
                        }
                        break;
                    default:
                        buffer.position(buffer.position() + length - 1);
                        break;
                    }
                }
            return new BleAdvertisedData(uuids,name);
        }
    }


    public class BleAdvertisedData {
        private List<UUID> mUuids;
        private String mName;
        public BleAdvertisedData(List<UUID> uuids,String name){
            mUuids = uuids;
            mName = name;
        }

        public List<UUID> getUuids(){
            return mUuids;
        }

        public String getName(){
            return mName;
        }
    }

Android Geocoder.getFromLocationName停止使用边界

Android Geocoder.getFromLocationName停止使用边界

我使用Geocoder.getFromLocationName来验证用户插入的地址.我检查返回的ArrayList,如果它是exaclty一个结果一切都很好.我也使用边界,所以我只得到某个区域内的结果( Geocoder).在上周某个时候突然停止工作之前,这种情况很好.返回的结果现在总是相同的,并且始终与我作为地址名称传递的字符串无关…

当我删除边界时,它工作正常.有没有人遇到过类似的问题?谷歌是否改变了该功能的实施方式?

解决方法

我们最近也开始体验这一点.我发现有人在两天前在AOSP问题跟踪器上提交了这个错误:

https://code.google.com/p/android/issues/detail?id=75575

我还创建了一个小样本开源项目来演示这个问题:

https://github.com/barbeau/GeocoderDemo

正如您所说,如果使用边界框,无论搜索项是什么,它似乎总是返回相同的通用结果:

如果没有边界框,它会正确返回特定于搜索项的结果,尽管这些结果是全局的,没有进一步过滤也没有多大用处.

这里的主要问题是历史上谷歌将Android Geocoder issues on the AOSP issue tracker标记为“错误的论坛”,所以我对那里的支持并不过分乐观.

我发布到Android开发者论坛:

https://groups.google.com/forum/#!topic/android-developers/KuZDVRXyTc0

…和谷歌希望在那里提出问题:

https://plus.google.com/+SeanBarbeau/posts/Mm5YwzeUoZV

编辑

截至2014年10月,似乎是this issue is resolved.

android getlastknownlocation在模拟器中始终为null

android getlastknownlocation在模拟器中始终为null

这似乎是一个常见的问题,但是即使我读了很多类似的问题,我也真的不明白为什么会这样。

我正在模拟器设备上使用基本的位置类,我设置了所有内容-
权限(FINE和COARSE),在DDMS中设置了坐标,我也尝试使用telnet,然后尝试,但是无论什么总是崩溃,都会导致nullpointer与getlastknownlocation有关的异常,这里有什么主意吗?

public class MainActivity extends Activity implements LocationListener {



private static LocationManager ok;
private Location L;

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



    ok=(LocationManager)this.getSystemService(ok.GPS_PROVIDER);
    L=ok.getLastKnownLocation(ok.GPS_PROVIDER);












}

@Override
public void onLocationChanged(Location Loc) {


    try {


        double latop=Loc.getLatitude();
        double longe=Loc.getLongitude();

        Log.i("OK","and"+longe+""+latop);


    } catch (NullPointerException e)

    {

    }




}

Android getlastknownlocation返回null

Android getlastknownlocation返回null

我正在为GPS提供商调用getLastKNownLocation,它返回null.文档说如果没有启用提供程序,可能会发生这种情况,但我知道它是.如果不存在最后已知位置,则提供程序是否可以返回null?我没有看到文档说“如果不存在最后一个已知位置,则可能返回null”

解决方法:

Is it possible for the provider to return null if no last kNown location exists?

是.事实上,在大多数情况下,它将返回null,因为没有任何因素导致GPS获取修复. GPS通常断电以节省电池寿命.有关查找位置的配方,请参阅Obtaining User Location.

I don’t see where the docs say “may return null if no last kNown location exists”

文档有其缺陷.

关于Android Geocoder getFromLocationName始终返回nullandroid getlocationonscreen的介绍现已完结,谢谢您的耐心阅读,如果想了解更多关于android BluetoothDevice.getName()返回null、Android Geocoder.getFromLocationName停止使用边界、android getlastknownlocation在模拟器中始终为null、Android getlastknownlocation返回null的相关知识,请在本站寻找。

本文标签: