Getting latitude & longitude from address in android
Asked Answered
B

2

11

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 address (like state , city name , street No.) than it do not give me correct latitude & longitude ...

thanks for your cooperative response ...

my code is ..

//String addressStr = "faisalabad";/// this give me correct address
        String addressStr = "Sainta Augustine,FL,4405 Avenue A";
          Geocoder geoCoder = new Geocoder(MapClass.this, Locale.getDefault());

          try {
              List<Address> addresses =
          geoCoder.getFromLocationName(addressStr, 1); 
              if (addresses.size() >  0) {
                 latitude = addresses.get(0).getLatitude(); longtitude =
          addresses.get(0).getLongitude(); }

          } catch (IOException e) { // TODO Auto-generated catch block
          e.printStackTrace(); }


        pos = new LatLng(latitude, longtitude);
        googleMap.addMarker(new MarkerOptions().icon(
                BitmapDescriptorFactory
                        .defaultMarker(BitmapDescriptorFactory.HUE_RED))
                .position(pos));
        googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(pos, 15));
        googleMap.animateCamera(CameraUpdateFactory.zoomTo(10), 2000, null);
Bitter answered 22/5, 2013 at 7:58 Comment(5)
Is it throwing some error?Ave
no error , default latitude and longitude values is 0.0 , marker put in ocean...Bitter
What locale is your getDefault() ?Dromous
nothing it is defalult valueBitter
Do you have the option of using a different geocoder?Losse
B
6

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

Geocoder geoCoder = new Geocoder(MapClass.this, Locale.getDefault());

to

Geocoder geoCoder = new Geocoder(MapClass.this);

thanks, all of you for your time ...

Bitter answered 22/5, 2013 at 11:14 Comment(4)
how and where to give street address then city name and state?Cheiro
in my question there is {String addressStr = "xxxx your address is here xxxx"} === your address must be street address and than city and than state and than country ..Bitter
what do you mean by state ... not all countries have state.. can i substitute it with country name?Fae
you leave state ,, only put your address simpleBitter
S
1

This is my solution :

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

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

        double latitude = location.getLatitude();
        double longitude = location.getLongitude();

        LatLng currentLatLng = new LatLng(latitude, longitude);

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

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

public static boolean isConnected(Context context) {
    NetworkInfo ni = ((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!

Sfumato answered 22/5, 2013 at 8:38 Comment(1)
thanks for replay ,, but i am getting address from user , than i want to put marker on that point ... not getting address from current location ...Bitter

© 2022 - 2024 — McMap. All rights reserved.