I have GPS code for finding latitude,longitude. TouchEvent is very useful.Is any other way for show latitude,longitude on maps application rather than TouchEvent?
Asked
Active
Viewed 980 times
2 Answers
1
Hello check if this helps you :)
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
LocationManager locationManager;
locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
Location location =locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
updateWithNewLocation(location);
}
private void updateWithNewLocation(Location location) {
String latLongString = "Unknown";
String addressString = "No address found";
TextView myLocationText;
DecimalFormat df = new DecimalFormat("##.00");
myLocationText = (TextView)findViewById(R.id.myLocationText);
if (location != null) {
double lat = location.getLatitude();
double lng = location.getLongitude();
latLongString = "Lat:" + df.format(lat) + "\nLong:" + df.format(lng);
Geocoder gc = new Geocoder(GPS1Activity.this, Locale.getDefault());
try {
List<Address> addresses = gc.getFromLocation(lat, lng, 1);
if (addresses.size() == 1) {
addressString="";
Address address = addresses.get(0);
addressString = addressString + address.getAddressLine(0) + "\n";
for (int i = 0; i < address.getMaxAddressLineIndex(); i++){
addressString = addressString + address.getAddressLine(i) + "\n";
}
addressString = addressString + address.getLocality()+ "\n";
addressString = addressString + address.getPostalCode()+ "\n";
addressString = addressString + address.getCountryName()+ "\n";
}
} catch (IOException ioe) {
Log.e("Geocoder IOException exception: ", ioe.getMessage());
}
}
myLocationText.setText("Your Current Position is:\n" + latLongString + "\n" + addressString);
}

Schewns
- 545
- 1
- 6
- 12
-
Schewns:I think this tutorial from Retomeier.I have this code only.but i didn't get any output on my emulator – karthi Aug 09 '12 at 09:57
-
Schewns: I used this code. Then i send gps coordinates via emulator control.But i didn't get any output from emulator – karthi Aug 09 '12 at 11:20
-
this piece of code worked with me, but i remember that at the time there was some problem with the emulator, cause u dont have gps on the emulator. try it with a device. – Schewns Aug 09 '12 at 13:30
-
let me know if it works for u, if not i can send u all the package, if u send me your email ;) – Schewns Aug 09 '12 at 14:04
-
karthi51@yahoo.co.in...This is my id, Schewns – karthi Aug 09 '12 at 14:12
-
sent, let me know how it went – Schewns Aug 09 '12 at 18:01