我正在尝试使用 gps service.bt 获取位置的示例代码,而不是 gettong 位置。下面是我的代码。-
public class MainActivity extends Activity implements LocationListener{
Location mLocation;
LocationManager mManager;
LocationListener mlistener;
Geocoder mcoder;
Context context;
TextView txtLat;
String lat;
String provider;
protected String latitude,longitude;
protected boolean gps_enabled,network_enabled;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
mManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
txtLat = (TextView) findViewById(R.id.tv);
}
public void getMyLocation(){
String addressString = "";
final Geocoder gc = new Geocoder(this, Locale.getDefault());
try
{
final List<Address> addresses = gc.getFromLocation(18.5203, 73.8567, 4);
final StringBuilder sb = new StringBuilder();
System.out.println("location..."+sb);
if ( addresses.size() > 0 )
{
final Address address = addresses.get(0);
for ( int i = 0; i < address.getMaxAddressLineIndex(); i++ )
{
sb.append(address.getAddressLine(i)).append("\n");
}
sb.append(address.getLocality()).append("\n");
sb.append(address.getPostalCode()).append("\n");
sb.append(address.getCountryName());
}
addressString = sb.toString();
}
catch ( final IOException e )
{
}
txtLat.setText("Your Current Position is:\n" +
"\n" + addressString);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public void onLocationChanged(Location location) {
getMyLocation();
}
@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
}
}