How to find latitude and longitude from an address?

I want to show the location of the address on Google Maps.

How do I get the latitude and longitude of an address using the Google Maps API?

+92
android google-maps google-geocoder
Aug 26 '10 at 11:31
source share
7 answers
public GeoPoint getLocationFromAddress(String strAddress){ Geocoder coder = new Geocoder(this); List<Address> address; GeoPoint p1 = null; try { address = coder.getFromLocationName(strAddress,5); if (address==null) { return null; } Address location=address.get(0); location.getLatitude(); location.getLongitude(); p1 = new GeoPoint((double) (location.getLatitude() * 1E6), (double) (location.getLongitude() * 1E6)); return p1; } } 

strAddress is a string containing an address. The address variable contains the translated addresses.

+119
Aug 26 '10 at 11:53 on
source share

Ud_an solution with updated API

Note The LatLng class is part of the Google Play Services.

Required :

 <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/> <uses-permission android:name="android.permission.INTERNET"/> 

Update. If you have a target SDK of 23 and above, make sure that you take care of allowing runtime for the location.

 public LatLng getLocationFromAddress(Context context,String strAddress) { Geocoder coder = new Geocoder(context); List<Address> address; LatLng p1 = null; try { // May throw an IOException address = coder.getFromLocationName(strAddress, 5); if (address == null) { return null; } Address location = address.get(0); p1 = new LatLng(location.getLatitude(), location.getLongitude() ); } catch (IOException ex) { ex.printStackTrace(); } return p1; } 
+58
Jan 08 '15 at 6:21
source share

If you want to put your address on google map then it will be easier for you to use the following

 Intent searchAddress = new Intent(Intent.ACTION_VIEW,Uri.parse("geo:0,0?q="+address)); startActivity(searchAddress); 

OR

if you need to get long long from your address, use Google Place Api after

create a method that returns a JSONObject with an HTTP call response as shown below

 public static JSONObject getLocationInfo(String address) { StringBuilder stringBuilder = new StringBuilder(); try { address = address.replaceAll(" ","%20"); HttpPost httppost = new HttpPost("http://maps.google.com/maps/api/geocode/json?address=" + address + "&sensor=false"); HttpClient client = new DefaultHttpClient(); HttpResponse response; stringBuilder = new StringBuilder(); response = client.execute(httppost); HttpEntity entity = response.getEntity(); InputStream stream = entity.getContent(); int b; while ((b = stream.read()) != -1) { stringBuilder.append((char) b); } } catch (ClientProtocolException e) { } catch (IOException e) { } JSONObject jsonObject = new JSONObject(); try { jsonObject = new JSONObject(stringBuilder.toString()); } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } return jsonObject; } 

now pass that JSONObject to the getLatLong () method, for example, of the following

 public static boolean getLatLong(JSONObject jsonObject) { try { longitute = ((JSONArray)jsonObject.get("results")).getJSONObject(0) .getJSONObject("geometry").getJSONObject("location") .getDouble("lng"); latitude = ((JSONArray)jsonObject.get("results")).getJSONObject(0) .getJSONObject("geometry").getJSONObject("location") .getDouble("lat"); } catch (JSONException e) { return false; } return true; } 

Hope this helps you and others. !! Thank..!!

+51
Nov 25 '11 at 9:35
source share

The following code will work for google apiv2:

 public void convertAddress() { if (address != null && !address.isEmpty()) { try { List<Address> addressList = geoCoder.getFromLocationName(address, 1); if (addressList != null && addressList.size() > 0) { double lat = addressList.get(0).getLatitude(); double lng = addressList.get(0).getLongitude(); } } catch (Exception e) { e.printStackTrace(); } // end catch } // end if } // end convertAddress 

Where the address is the String (123 Testing Rd City State zip) that you want to convert to LatLng.

+6
May 21 '14 at 9:32
source share

Here's how you can find the latitude and longitude of where we click on the map.

 public boolean onTouchEvent(MotionEvent event, MapView mapView) { //---when user lifts his finger--- if (event.getAction() == 1) { GeoPoint p = mapView.getProjection().fromPixels( (int) event.getX(), (int) event.getY()); Toast.makeText(getBaseContext(), p.getLatitudeE6() / 1E6 + "," + p.getLongitudeE6() /1E6 , Toast.LENGTH_SHORT).show(); } return false; } 

It works well.

To get the location address, we can use the geocoder class.

+3
Aug 26 '10 at 12:21
source share

The answer to the Kandhi problem above is:

It throws a "java.io.IOException service is unavailable" I already gave these permissions and turned on the library ... I can get a map view ... it throws this IOException on geocoding ...

I just added catch IOException after trying and solved the problem

  catch(IOException ioEx){ return null; } 
+1
Dec 25 '14 at 16:48
source share
 Geocoder coder = new Geocoder(this); List<Address> addresses; try { addresses = coder.getFromLocationName(address, 5); if (addresses == null) { } Address location = addresses.get(0); double lat = location.getLatitude(); double lng = location.getLongitude(); Log.i("Lat",""+lat); Log.i("Lng",""+lng); LatLng latLng = new LatLng(lat,lng); MarkerOptions markerOptions = new MarkerOptions(); markerOptions.position(latLng); googleMap.addMarker(markerOptions); googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(latLng,12)); } catch (IOException e) { e.printStackTrace(); } 
0
Apr 21 '16 at 9:02
source share



All Articles