Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 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);
like image 831
mohsin raza Avatar asked May 22 '13 07:05

mohsin raza


People also ask

How do you get latitude and longitude?

Start with your line of latitude, writing the degrees, then the minutes, then the seconds. Then, add the North or South as the direction. Then, write a comma followed by your line of longitude in degrees, then minutes, then seconds. Then, add East or West as the direction.

What latitude means?

Latitude measures the distance north or south of the equator. Latitude lines start at the equator (0 degrees latitude) and run east and west, parallel to the equator. Lines of latitude are measured in degrees north or south of the equator to 90 degrees at the North or South poles.

What are the 4 types of latitudes?

There are 5 types of latitudes- the Arctic Circle, the Antarctic Circle, and the Equator, the Tropic of Cancer, and the Tropic of Capricorn.


2 Answers

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 ...

like image 184
mohsin raza Avatar answered Sep 22 '22 07:09

mohsin raza


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!

like image 33
lopez.mikhael Avatar answered Sep 24 '22 07:09

lopez.mikhael