How to Obtain Latitude and Longitude in Android

隐身守侯 提交于 2019-12-06 10:01:35

问题


How to obtain Latitude and longitude from the mobile device if GPS is not available... My mobile has internet Connection through wifi and gprs connection.... Can we get the lats and long from this?


回答1:


edited : Network and also GPS Provider Location

Yes we cam...try this

LocationManager manager = (LocationManager) Context.getSystemService(Context.LOCATION_SERVICE);
manager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

// additionally (you have to implement LocationListener)
manager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0.0f, new LocationListener() ...)

After location you can use in locationlistener...

 @Override
    public void onLocationChanged(Location location) {                          
         Log.d("a","onLocationChanged: lat="+location.getLatitude());
         Log.d("a","onLocationChanged: lat="+location.getLongitude());                 
    }

hope it helps..




回答2:


Yes you can. Have a look at the documentation

When you have a Location from the system. you can use methods getLongitude() and getLatitude()

For a full example have a look at this code - lines 122-124 to set up receiving updates and lines 201-209 to get the Location.



来源:https://stackoverflow.com/questions/5496949/how-to-obtain-latitude-and-longitude-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!