开发者

Android GPS Fix & getFromLocation

I have two issues, the first is that I have a String Builder that obtains an address and prints it to a edittext:

  Geocoder mGC = new Geocoder(context,Locale.getDefault());
    address = mGC.getFromLocation(lat, lng, 1);

    if (address !=null){
     Address currentAddr = address.get(0);
     mSB = new StringBuilder();
     for (int i=0; i<currentAddr.getMaxAddressLineIndex(); i++){
     mSB.append(currentAddr.getAddressLine(i)).append(", ");
    }
   outputText.setText(mSB.toString());

}

The problem is randomly the address = mGC.getFromLocation(lat, lng, 1); line returns a null pointer exception. Sometimes it works开发者_如何学Python for days...then suddenly it has a null pointer exception; anyone know why?

Also my second issue is my GPS fix takes some time, I am using the GPS Satellite for it; is there a way I can use Network provided info first and then the GPS Satellite for a faster fix?


About your second issue: You need to have two location listeners, one for network and the other one for GPS. Then you should use onLocationChanged on each listener to do your logic, in this case first use the network location, and once you get an update for the GPS one you use that one instead.


For the first problem, it is possible that for the given, lat,lon there maybe no address. Also this Geocoder service needs internet to be active.

GPS will always take time to get a fix. For a cold start it is about 20 minutes. For a warm start it can be 20 seconds to 1 minute. You see, it scans throughout the spectrum for signs of satellite visibility, and then calculates the doppler shift among other things. If you have internet on your phone it will shorten this time due to AGPS servers assisting your phone. The NetworkProvider is highly inaccurate. This is the best way of juggling them.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜