0
votes

How to find Longitude and Latitude of Particular location?

the location is entered by user in edittext & click on search button then the location is display in googlemap.

i used following code for that but this is give error "Service not Available"


Geocoder geoCoder = new Geocoder(this, Locale.getDefault());
try {
     address=geoCoder.getFromLocationName(txtlocation.getText().toString(), 1).get(0);
     double longi=address.getLongitude();
     double latit=address.getLatitude();
     System.out.println("longitude:--- " +longi);
     System.out.println("latitude:---" +latit);

} catch (IOException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
     Toast.makeText(MapRouteActivity.this, e.toString(), Toast.LENGTH_LONG).show();
}

2
why don't you use locationManager.Stuti
In my Application, i used locationmanager but not displayed here.Dipak Keshariya
First look up at the night sky and locate the North star.... ;)Doug Chamberlain

2 Answers

2
votes

try to use

LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);  Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER); double longitude = location.getLongitude(); double latitude = location.getLatitude(); 

The call to getLastKnownLocation() doesn't block - which means it will return null if no position is currently available - so you probably want to have a look at passing a LocationListener to the requestLocationUpdates() method instead, which will give you asynchronous updates of your location.

private final LocationListener locationListener = new LocationListener() {     public void onLocationChanged(Location location) {         longitude = location.getLongitude();         latitude = location.getLatitude();     } } 

lm.requestLocationUpdates(LocationManager.GPS, 2000, 10, locationListener); 

You'll need to give your application the ACCESS_FINE_LOCATION permission if you want to use GPS.

You may also want to add the ACCESS_COARSE_LOCATION permission for when GPS isn't available and select your location provider with the getBestProvider() method.

0
votes

try this code

public static String getLatLng(Context context,String addr){
    Geocoder geocoder = new Geocoder(context, Locale.getDefault());
    String add = "";
    try{
        List<Address> addresses = geocoder.getFromLocationName(addr, 5);

        for(int i=0;i<1;i++){
            Address obj = addresses.get(i);
            for(int j=0;j<obj.getMaxAddressLineIndex();j++){
                add = obj.getAddressLine(j);
                add = add + "\nCountryName " + obj.getCountryName();
                add = add + "\nCountryCode " + obj.getCountryCode();
                add = add + "\nAdminArea " + obj.getAdminArea();
                add = add + "\nPostalCode " + obj.getPostalCode();
                add = add + "\nSubAdminArea " + obj.getSubAdminArea();
                add = add + "\nFeatureName " + obj.getFeatureName();
                add = add + "\nLocality " + obj.getLocality();
                add = add + "\n" + obj.getSubThoroughfare();
                add = add + "\nurl " + obj.getUrl();
                add = add + "\nLatitude " + obj.getLatitude();
                add = add + "\nLongitude " + obj.getLongitude();
            }
            add = add+"\n";
        }

        Log.v("IGA", "Address" + add);

    }catch(Exception e){
        e.printStackTrace();
        add = e.toString();
    }
    return add;
}