Skip to content Skip to sidebar Skip to footer

Getting Latitude & Longitude From Address In Android

i am try to get latitude & longitude from address , problem is that .. when i give only city name than it give me correct latitude & longitude and when i give complete ad

Solution 1:

i don it ... :)

only sequence is incorrect ...

first give street address than city name and than state ... it give me correct latitude and longitude from address .. :)

and change

GeocodergeoCoder=newGeocoder(MapClass.this, Locale.getDefault());

to

GeocodergeoCoder=newGeocoder(MapClass.this);

thanks, all of you for your time ...

Solution 2:

This is my solution :

privatevoidcreateMarkerWithLocation() {
    /* Use the LocationManager class to obtain GPS locations */LocationManagermlocManager= (LocationManager)getSystemService(Context.LOCATION_SERVICE);
    mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 300000, 40, this);

    Locationlocation= mlocManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); 
    /*check both providers even for lastKnownLocation*/if (location == null)
        location = mlocManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
    if(location != null) {

        doublelatitude= location.getLatitude();
        doublelongitude= location.getLongitude();

        LatLngcurrentLatLng=newLatLng(latitude, longitude);

        if(isConnected(this)) {
            GeocodergCoder=newGeocoder(ChoiceDestinationActivity.this);
            List<Address> addresses = gCoder.getFromLocation(latitude, longitude, 1);
            Stringaddress= addresses.get(0).getAddressLine(0);
            Stringcity= addresses.get(0).getAddressLine(1);
            Stringcountry= addresses.get(0).getAddressLine(2);
            Toast.makeText(this, country + ", " + city + ", " + address, Toast.LENGTH_SHORT).show();

            marker = map.addMarker(newMarkerOptions()
            .position(currentLatLng)
            .title(city)
            .snippet(address)
            .icon(BitmapDescriptorFactory.fromResource(R.drawable.marker)));
        }
    }
}

publicstaticbooleanisConnected(Context context) {
    NetworkInfoni= ((ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE)).getActiveNetworkInfo();
    return (ni!=null && ni.isAvailable() && ni.isConnected());
}

I use it to add a marker on google map. It allows you to retrieve all the information regarding the location of the user.

I hope you have helped!

Post a Comment for "Getting Latitude & Longitude From Address In Android"