Android implements GPS to locate code instances


Through GPS, one Location latitude and longitude can be converted to two Double latitude and longitude. Latitude: 23.223871812820435 Latitude: 113.58986039161628 Start by creating 1 TextView and 2 Button

<TextView
 android:id="@+id/text"
 android:layout_width="fill_parent"
  android:layout_height="wrap_content"  />

 <Button
  android:id="@+id/btnStart"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text=" positioning " />
 <Button
  android:id="@+id/btnStop"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text=" stop " />

Then add the code for master Activity Location is a type that stores latitude and longitude LocationManager is a location management service type

private Button btnStart;
private Button btnStop;
private TextView textView;
private Location mLocation;
private LocationManager mLocationManager;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{

 super.onCreate(savedInstanceState);
 setContentView(R.layout.main);

 btnStart = (Button)findViewById(R.id.btnStart);
 btnStop = (Button)findViewById(R.id.btnStop);
 textView = (TextView)findViewById(R.id.text);
 btnStart.setOnClickListener(btnClickListener); // To locate
 btnStop.setOnClickListener(btnClickListener); // End positioning button
}
gpsIsOpen Is oneself write view current GPS Whether open
getLocation  I wrote it myself 1 Methods for obtaining location information
mLocationManager.removeUpdates() Is to stop the current GPS Location to monitor
public Button.OnClickListener btnClickListener = new Button.OnClickListener()
{
 public void onClick(View v)
 {
  Button btn = (Button)v;
  if(btn.getId() == R.id.btnStart)
  {
   if(!gpsIsOpen())
    return;

  mLocation = getLocation();

   if(mLocation != null)
    textView.setText(" The dimension :" + mLocation.getLatitude() + "\n longitude :" + mLocation.getLongitude());
   else
    textView.setText(" Can't get the data ");
  }
  else if(btn.getId() == R.id.btnStop)
  {
   mLocationManager.removeUpdates(locationListener);
  }

 }
};
private boolean gpsIsOpen()
{
 boolean bRet = true;

 LocationManager alm = (LocationManager)this.getSystemService(Context.LOCATION_SERVICE);
 if(!alm.isProviderEnabled(LocationManager.GPS_PROVIDER))
 {
  Toast.makeText(this, " Did not open GPS", Toast.LENGTH_SHORT).show();
  bRet = false;
 }
 else
 {
  Toast.makeText(this, "GPS Has been open ", Toast.LENGTH_SHORT).show();
 }

 return bRet;
}
 Determine whether it is currently enabled GPS
private boolean gpsIsOpen()
{
 boolean bRet = true;

 LocationManager alm = (LocationManager)this.getSystemService(Context.LOCATION_SERVICE);
 if(!alm.isProviderEnabled(LocationManager.GPS_PROVIDER))
 {
  Toast.makeText(this, " Did not open GPS", Toast.LENGTH_SHORT).show();
  bRet = false;
 }
 else
 {
  Toast.makeText(this, "GPS Has been open ", Toast.LENGTH_SHORT).show();
 }

 return bRet;
}
 This method obtains the current latitude and longitude,   The first 1 Subfetch is always null
 From behind LocationListener Gets the changed location
mLocationManager.requestLocationUpdates() Is open 1 a LocationListener Wait position change
private Location getLocation()
{
 // Get location management services
 mLocationManager = (LocationManager)this.getSystemService(Context.LOCATION_SERVICE);

 // Find service information
 Criteria criteria = new Criteria();
 criteria.setAccuracy(Criteria.ACCURACY_FINE); // Positioning accuracy :  The highest
 criteria.setAltitudeRequired(false); // Altitude information: no
 criteria.setBearingRequired(false); // Azimuth information :  Don't need
 criteria.setCostAllowed(true);  // Is payment allowed?
 criteria.setPowerRequirement(Criteria.POWER_LOW); // Power consumption :  Low power consumption

 String provider = mLocationManager.getBestProvider(criteria, true); // To obtain GPS information

 Location location = mLocationManager.getLastKnownLocation(provider);

 mLocationManager.requestLocationUpdates(provider, 2000, 5, locationListener);

 return location;
}
 Instead, wait GPS You get the new latitude and longitude after you change your position
private final LocationListener locationListener = new LocationListener()
{
 public void onLocationChanged(Location location)
 {
  // TODO Auto-generated method stub
  if(location != null)
   textView.setText(" The dimension :" + location.getLatitude() + "\n longitude :"
      + location.getLongitude());
  else
   textView.setText(" Can't get the data " + Integer.toString(nCount));
 }

 public void onProviderDisabled(String provider)
 {
  // TODO Auto-generated method stub
 }

 public void onProviderEnabled(String provider)
 {
  // TODO Auto-generated method stub
 }

 public void onStatusChanged(String provider, int status, Bundle extras)
 {
  // TODO Auto-generated method stub

 }
};