开发者

Android latitude and longitude

i m working on one application which has to find the lat and 开发者_开发技巧long of the user's location and then display on the map. But when internet is there on the device its working fine. but when i try after disconnected the internet its getting lat and long null.

Queries:

  1. Is if i want to find lat and long programmaticly the internet connectivity should be there?
  2. Without connected to internet can i find lat and long programmaticaly?


The fact is, there are two ways for Android to determine your longitude/latitude.

  • If the GPS is activated, it uses the GPS
  • If not, it uses the network (or wifi) to determine that, based on the cell where you are, or the ip address.

So, if GPS is deactivated (or unable to catch a signal, due to being indoors or to bad meteorological conditions), and network too, Android is unable to compute your coordinates.


You can determine device location programatically by implementing LocationListener and something like this:

LocationManager locmgr = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locmgr.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this); //"this" implements LocationListener

@Override
public void onLocationChanged(Location location) {
  //do whatever with "location"
}

No need for internet connection at all, you just need a location provider (A-GPS, GPS)


If you want to display it on the map, use MyLocationOverlay. Its way easier then implementing your own locationListeners. Call enableMyLocation onResume() and disableMyLocation onPause().

You can also subclass MyLocationOverlay to change the icon, do custom stuff on location changes, etc.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜