当前位置的最大细节.
码:
public class GPSTracker extends Service implements LocationListener {private final Context mContext;// flag for GPS statusboolean isGPSEnabled = false;// flag for network statusboolean isNetworkEnabled = false;// flag for GPS statusboolean canGetLocation = false;Location location; // locationdouble latitude; // latitudedouble longitude; // longitudeString country;// The minimum distance to change Updates in metersprivate static final long MIN_disTANCE_CHANGE_FOR_UPDATES = 10; // 10 meters// The minimum time between updates in millisecondsprivate static final long MIN_TIME_BW_UPDATES = 1000 * 60 * 1; // 1 minute// Declaring a Location Managerprotected LocationManager locationManager;public GPSTracker(Context context) { this.mContext = context; getLocation();}public Location getLocation() { try { locationManager = (LocationManager) mContext .getSystemService(LOCATION_SERVICE); // getting GPS status isGPSEnabled = locationManager .isProvIDerEnabled(LocationManager.GPS_PROVIDER); // getting network statusisNetworkEnabled = locationManager .isProvIDerEnabled (LocationManager.NETWORK_PROVIDER); if (!isGPSEnabled && !isNetworkEnabled) { // no network provIDer is enabled } else { this.canGetLocation = true; if (isNetworkEnabled) { locationManager.requestLocationUpdates( LocationManager.NETWORK_PROVIDER,MIN_TIME_BW_UPDATES,MIN_disTANCE_CHANGE_FOR_UPDATES,this); Log.d("Network","Network"); if (locationManager != null) { location = locationManager .getLastKNownLocation (LocationManager.NETWORK_PROVIDER); if (location != null) { latitude = location.getLatitude(); longitude = location.getLongitude(); } } } // if GPS Enabled get lat/long using GPS Services if (isGPSEnabled) { if (location == null) { locationManager.requestLocationUpdates( LocationManager.GPS_PROVIDER,this); Log.d("GPS Enabled","GPS Enabled"); if (locationManager != null) { location = locationManager.getLastKNownLocation (LocationManager.GPS_PROVIDER); if (location != null) { latitude = location.getLatitude(); longitude = location.getLongitude(); } } } } } } catch (Exception e) { e.printstacktrace(); } return location; }解决方法 这是 Get Current location and City name using GPS的教程
一旦你获得GPS坐标
通过使用Geocoder类,您可以获得每个细节
请参阅下面的代码片段以获取当前城市相同的方式您也可以获取其他详细信息.
private class MyLocationListener implements LocationListener { @OverrIDe public voID onLocationChanged(Location loc) { editLocation.setText(""); pb.setVisibility(VIEw.INVISIBLE); Toast.makeText( getBaseContext(),"Location changed : Lat: " + loc.getLatitude() + " Lng: " + loc.getLongitude(),Toast.LENGTH_SHORT).show(); String longitude = "Longitude: " + loc.getLongitude(); Log.v(TAG,longitude); String latitude = "Latitude: " + loc.getLatitude(); Log.v(TAG,latitude); /*----------to get City-name from coordinates ------------- */ String cityname = null; Geocoder gcd = new Geocoder(getBaseContext(),Locale.getDefault()); List<Address> addresses; try { addresses = gcd.getFromLocation(loc.getLatitude(),loc.getLongitude(),1); if (addresses.size() > 0) System.out.println(addresses.get(0).getLocality()); cityname = addresses.get(0).getLocality(); } catch (IOException e) { e.printstacktrace(); } String s = longitude + "\n" + latitude + "\n\nMy Currrent City is: " + cityname; editLocation.setText(s); } @OverrIDe public voID onProvIDerDisabled(String provIDer) { // Todo auto-generated method stub } @OverrIDe public voID onProvIDerEnabled(String provIDer) { // Todo auto-generated method stub } @OverrIDe public voID onStatusChanged(String provIDer,int status,Bundle extras) { // Todo auto-generated method stub }}
更新(根据评论)
如果您想通过短信将此详细信息发送给任何联系人.
我会说this页可能会帮到你
以上是内存溢出为你收集整理的android – 如何使用GPS和位置管理器获取当前位置的完整详细信息全部内容,希望文章能够帮你解决android – 如何使用GPS和位置管理器获取当前位置的完整详细信息所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)