how to find exact location using android.location.Address?

Put problem concerning MapActivities, MapViews, Overlays and GPS in this forum.

how to find exact location using android.location.Address?

Postby pranav09 » Thu Feb 04, 2010 2:47 pm

Hi all,

I want to display the particular location using the android.location.Address. I am able to enter city and can get latitude and longitude. But i want to get accuracy. If i enter full address of my home, it should give me the particular latitude and longitude. Is it possible to enter the particular alpha-numeric address or latitude and longitude with using of android.location.Address. Basically, i want accuracy. Weather i enter alpha-numeric address or latitude and longitude.

Here is my code where i can get latitude and longitude with enter the address.

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.         public void displayResult(){
  2.  
  3.                 Geocoder geoCoder = new Geocoder(this, Locale.getDefault());
  4.  
  5.         double latitude = 0;
  6.  
  7.         double longitude = 0;
  8.  
  9.         try {
  10.  
  11.                 List<Address> addresses = geoCoder.getFromLocationName(
  12.  
  13.                                 edSendAddress.getEditableText().toString(), 5);
  14.  
  15.                 if (addresses.size() > 0) {
  16.  
  17.                 latitude = addresses.get(0).getLatitude();
  18.  
  19.                 longitude = addresses.get(0).getLongitude();
  20.  
  21.                 System.out.println("S:Latitude & Longitude : "+latitude+" & "+longitude);
  22.  
  23.                 }
  24.  
  25.         } catch (IOException e) {
  26.  
  27.                 e.printStackTrace();
  28.  
  29.         }
  30.  
  31.         strDisplayLatitude = ""+latitude;
  32.  
  33.         strDisplayLongitude = ""+longitude;
  34.  
  35.         }
  36.  
  37.  
Parsed in 0.034 seconds, using GeSHi 1.0.8.4


Here, strDisplayLatitude and strDisplayLongitude are string.

Is anyone has any idea regarding my problem? Please help me out to resolve the problem.

Thanks,

Regards.
------------------
Pranav
User avatar
pranav09
Senior Developer
Senior Developer
 
Posts: 128
Joined: Mon Jan 19, 2009 12:01 pm
Location: Ahmedabad, India

Top

Postby azraeal » Wed Feb 10, 2010 8:00 am

Try to following to get the long/lat from address:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. /*pseudo-code*/
  3.  
  4. Address myAddress = geo.getLocationName("123 Main St. Any Town, USA",1).get(0);
  5.  
  6. double long =myAddress.getLongitude();
  7.  
  8. double lat = myAddress.getLatitude();
  9.  
  10.  
Parsed in 0.031 seconds, using GeSHi 1.0.8.4


This is best effort via a network lookup with Google.
azraeal
Experienced Developer
Experienced Developer
 
Posts: 71
Joined: Fri Sep 04, 2009 6:34 pm
Location: California

Nice

Postby pranav09 » Thu Feb 18, 2010 10:50 am

Hi azraeal,

Sorry for the late reply.

By the way, thanks a lot for helping me to out from that problem. It helps me a lot in my application and i got exactly as i want.

And also see http://www.anddev.org/store_contact_nam ... 11291.html.

Thanks again.

Regards,
__________
Pranav
User avatar
pranav09
Senior Developer
Senior Developer
 
Posts: 128
Joined: Mon Jan 19, 2009 12:01 pm
Location: Ahmedabad, India

Re: how to find exact location using android.location.Addres

Postby Dhr » Thu Sep 16, 2010 7:26 am

hey pranav,

I m new in android.
right now i m working on android map application.

Can you show me your code that will help me to learn.

Thanks in advanced.

~Dhr
User avatar
Dhr
Junior Developer
Junior Developer
 
Posts: 24
Joined: Thu Aug 19, 2010 11:59 am
Location: Ahd, Gujarat, India

Re: how to find exact location using android.location.Addres

Postby Dhr » Thu Sep 16, 2010 7:28 am

hey pranav,

I m new in android.
right now i m working on android map application.

Can you show me your code that will help me to learn.

Thanks in advanced.

~Dhr
User avatar
Dhr
Junior Developer
Junior Developer
 
Posts: 24
Joined: Thu Aug 19, 2010 11:59 am
Location: Ahd, Gujarat, India

Re: how to find exact location using android.location.Addres

Postby pranav09 » Fri Sep 17, 2010 5:53 am

Hi Dhr,

Exactly, what do you want? Any specific? Which type of application you are developing? It would be easy for you as well as me.

Regards,
Pranav
User avatar
pranav09
Senior Developer
Senior Developer
 
Posts: 128
Joined: Mon Jan 19, 2009 12:01 pm
Location: Ahmedabad, India

Top

Re: how to find exact location using android.location.Addres

Postby Dhr » Fri Sep 17, 2010 7:59 am

Hi Pranav,

Actually I am developing two kind of map applications.
1. In this application, I use Two EditText and One Button on my layout. The user can enter his latitude and longitude in these two EditText. After clicking on the Button, the Map shows the location and marker of the user's latitude and longitude.
I am able to run this application but the problem is that the marker is not visible and the zooming of the map is maximum everytime. So I have to zoom-out everytime.

2. In this application, I use One EditText and One Button. The user can enter his address in the EditText. After clicking on the Button, the map shows user's location and marker on that.
In this application, whenever I click on the button it generates an error of force close in my emulator.

And 3rd one, If I want to find my current location on map, what can I do?

Please suggest me what can I do?

Thanks in advanced.

~Dhr
User avatar
Dhr
Junior Developer
Junior Developer
 
Posts: 24
Joined: Thu Aug 19, 2010 11:59 am
Location: Ahd, Gujarat, India

Re: how to find exact location using android.location.Addres

Postby pranav09 » Mon Sep 20, 2010 4:18 pm

Hi ~Dhr,

The below link will help you out to solve your problem.

1) Display location with marker based on given latitude & longitude:

http://developer.android.com/resources/tutorials/views/hello-mapview.html

Set action on button which takes values of latitude and longitude which you have passed in EditText. On the base of it, start the MapActivity and than you will have a marker at your desire location(latitude & longitude).

For setting the zoom level, try below code.

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. MapView mapVIew = (MapView)findViewById(R.id.mapvIew);
  2. MapController mc = mapView.getController();
  3. mc.setZoom(10); // min-1 & max-21
Parsed in 0.035 seconds, using GeSHi 1.0.8.4


2) Display location based on given Address: (e.g., Nehrunagar, Ahmedabad, India)

Use the Geocoder to get the latitude-longitude from the address and pass those latitude and longitude to the GeoPoint as above link. Do the same to display marker on the desired location.

3) To get the current location on the map:

Follow below links.
http://www.devx.com/wireless/Article/39239/1954
http://www.firstdroid.com/2010/04/29/android-development-using-gps-to-get-current-location-2/

Hope, you will get your solutions. If still you have problem, let me know.

Thanks.
Pranav
User avatar
pranav09
Senior Developer
Senior Developer
 
Posts: 128
Joined: Mon Jan 19, 2009 12:01 pm
Location: Ahmedabad, India

Re: how to find exact location using android.location.Addres

Postby Dhr » Tue Sep 21, 2010 6:36 am

Hi Pranav,

Thanks for this.

I tried to develop the 3rd one application with my own but it shows "No Location Found " in my textview.

Can you tell me that what is the problem in that.
I can send my code.

~Dhr
User avatar
Dhr
Junior Developer
Junior Developer
 
Posts: 24
Joined: Thu Aug 19, 2010 11:59 am
Location: Ahd, Gujarat, India

Re: how to find exact location using android.location.Addres

Postby pranav09 » Tue Sep 21, 2010 7:04 am

Hi Dhr,

Try to do as below way,

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. .....
  2. public void findLocaiton()
  3. {
  4.         String strProvider = "", strContext = "";
  5.         strContext = Context.LOCATION_SERVICE;
  6.         LocationManager locationManager = (LocationManager)getSystemService(strContext);
  7.        
  8.         Log.i("HomePage : ","Defining Criteria.");
  9.        
  10.         Criteria criteria = new Criteria();
  11.         criteria.setAccuracy(Criteria.ACCURACY_FINE);
  12.         criteria.setPowerRequirement(Criteria.POWER_MEDIUM);
  13.         criteria.setSpeedRequired(false);
  14.         criteria.setCostAllowed(false);
  15.         criteria.setAltitudeRequired(false);
  16.         criteria.setBearingRequired(false);
  17.  
  18.         Log.i("HomePage : ","Defining Provider.");
  19.         strProvider = locationManager.getBestProvider(criteria, true);
  20.         if(strProvider == null)
  21.         {
  22.                 Log.e("HomePage : ", "Provider is not available.");
  23.         }
  24.         else if(strProvider.equals(LocationManager.GPS_PROVIDER))
  25.         {
  26.                 location = locationManager.getLastKnownLocation(strProvider);
  27.                 updateWithNewLocation(location);
  28.                 locationManager.requestLocationUpdates(strProvider, 30000, 0, locationListener);
  29.         }
  30.         else if(strProvider.equals(LocationManager.NETWORK_PROVIDER))
  31.         {
  32.                 location = locationManager.getLastKnownLocation(strProvider);
  33.                 updateWithNewLocation(location);
  34.                 locationManager.requestLocationUpdates(strProvider, 30000, 0, locationListener);
  35.         }
  36. }
  37.  
  38. public void updateWithNewLocation(Location location)
  39. {
  40.                 if(location != null)
  41.                 {
  42.                         double latitude = location.getLatitude();
  43.                         double longitude = location.getLongitude();
  44.                 }
  45.                 else
  46.                 {
  47.                         Log.d("HomePage", "No Location Found!");
  48.                 }
  49. }
  50.  
  51. private class MyLocationListener implements LocationListener
  52. {
  53.         public void onLocationChanged(Location location)
  54.         {
  55.                 updateWithNewLocation(location);
  56.         }
  57.  
  58.         public void onProviderDisabled(String provider) {}
  59.  
  60.         public void onProviderEnabled(String provider) {}
  61.  
  62.         public void onStatusChanged(String provider, int status, Bundle extras) {}
  63. }    
  64.  
  65. ......
Parsed in 0.045 seconds, using GeSHi 1.0.8.4


Call this method in onCreate(..).

Hope it helps you.
Thanks.
Pranav
User avatar
pranav09
Senior Developer
Senior Developer
 
Posts: 128
Joined: Mon Jan 19, 2009 12:01 pm
Location: Ahmedabad, India

Re: how to find exact location using android.location.Addres

Postby Dhr » Tue Sep 21, 2010 1:35 pm

Hello Pranav,

Thanks a lot Pranav for this.
Its really a working.

You are a genius.

Thanks again.

~Dhr
User avatar
Dhr
Junior Developer
Junior Developer
 
Posts: 24
Joined: Thu Aug 19, 2010 11:59 am
Location: Ahd, Gujarat, India

Re: how to find exact location using android.location.Addres

Postby Archukrish » Sun Feb 20, 2011 6:38 pm

Hi Pranav,
I'm new to Android. Can you please tell me where we provide our address location in the above code you have given on sep 21st?
Please Explain me. I'm need of it. Please help me.

Thanks in advance.
Archukrish
Once Poster
Once Poster
 
Posts: 1
Joined: Sun Feb 20, 2011 6:08 pm

Re: how to find exact location using android.location.Addres

Postby malar » Sat Feb 26, 2011 9:31 am

hi i am new to android, i have tried to find the location using latitude and longitude i used above code but i could see map rather i could see only grey grid. even i gave a correct apikey and gave permissions too.can any one give suggessions?


Thanks in advance
malar
Freshman
Freshman
 
Posts: 6
Joined: Thu Jan 06, 2011 6:46 am

how to find distance between two places...

Postby sandeshk » Fri May 18, 2012 8:58 am

Hi All,

I m new to android and I m trying to develop one application which includes two edittext and one button....
In two edittexts I Will enter the name of two places and when I click on the button I need to get the distance between those two places....
Can anyone help me in this....

Thanks in advance....


Regards,
Sandesh
sandeshk
Once Poster
Once Poster
 
Posts: 1
Joined: Fri May 18, 2012 8:37 am

Top

Return to Map Problems

Who is online

Users browsing this forum: No registered users and 6 guests