Geocoder android studio java

Geocoder

A class for handling geocoding and reverse geocoding. Geocoding is the process of transforming a street address or other description of a location into a (latitude, longitude) coordinate. Reverse geocoding is the process of transforming a (latitude, longitude) coordinate into a (partial) address. The amount of detail in a reverse geocoded location description may vary, for example one might contain the full street address of the closest building, while another might contain only a city name and postal code. The Geocoder class requires a backend service that is not included in the core android framework. The Geocoder query methods will return an empty list if there no backend service in the platform. Use the isPresent() method to determine whether a Geocoder implementation exists.

Summary

Returns an array of Addresses that are known to describe the area immediately surrounding the given latitude and longitude.

Returns an array of Addresses that are known to describe the named location, which may be a place name such as «Dalvik, Iceland», an address such as «1600 Amphitheatre Parkway, Mountain View, CA», an airport code such as «SFO», etc..

Returns an array of Addresses that are known to describe the named location, which may be a place name such as «Dalvik, Iceland», an address such as «1600 Amphitheatre Parkway, Mountain View, CA», an airport code such as «SFO», etc..

Читайте также:  Python прочитать файл байтами

Causes a thread which is waiting on this object’s monitor (by means of calling one of the wait() methods) to be woken up.

Causes all threads which are waiting on this object’s monitor (by means of calling one of the wait() methods) to be woken up.

Causes the calling thread to wait until another thread calls the notify() or notifyAll() method of this object.

Causes the calling thread to wait until another thread calls the notify() or notifyAll() method of this object or until the specified timeout expires.

Causes the calling thread to wait until another thread calls the notify() or notifyAll() method of this object or until the specified timeout expires.

Public Constructors

public Geocoder (Context context, Locale locale)

Constructs a Geocoder whose responses will be localized for the given Locale.

Источник

Android-er

This example allow users to enter latitude and longitude, then find the addresses that are known to describe the area immediately surrounding the given latitude and longitude, using getFromLocation (double latitude, double longitude, int maxResults) method of android.location.Geocoder.

android.location.Geocoder is a class for handling geocoding and reverse geocoding. Geocoding is the process of transforming a street address or other description of a location into a (latitude, longitude) coordinate. Reverse geocoding is the process of transforming a (latitude, longitude) coordinate into a (partial) address. The amount of detail in a reverse geocoded location description may vary, for example one might contain the full street address of the closest building, while another might contain only a city name and postal code. The Geocoder class requires a backend service that is not included in the core android framework. The Geocoder query methods will return an empty list if there no backend service in the platform. Use the isPresent() method to determine whether a Geocoder implementation exists.


com.blogspot.android_er.androidlatlongtoaddress.MainActivity.java

package com.blogspot.android_er.androidlatlongtoaddress; import android.location.Address; import android.location.Geocoder; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.EditText; import android.widget.ListView; import android.widget.Toast; import java.io.IOException; import java.util.ArrayList; import java.util.List; public class MainActivity extends AppCompatActivity < Geocoder geocoder; @Override protected void onCreate(Bundle savedInstanceState) < super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); final EditText latText = (EditText)findViewById(R.id.latText); final EditText lonText = (EditText)findViewById(R.id.lonText); Button btnFind = (Button)findViewById(R.id.find); final ListView listResult = (ListView)findViewById(R.id.listResult); geocoder = new Geocoder(this); btnFind.setOnClickListener(new View.OnClickListener()< @Override public void onClick(View v) < String strLat = latText.getText().toString(); String strLon = lonText.getText().toString(); boolean parsable = true; Double lat = null, lon = null; try< lat = Double.parseDouble(strLat); >catch (NumberFormatException ex) < parsable = false; Toast.makeText(MainActivity.this, "Latitude does not contain a parsable double", Toast.LENGTH_LONG).show(); >try< lon = Double.parseDouble(strLon); >catch (NumberFormatException ex) < parsable = false; Toast.makeText(MainActivity.this, "Longitude does not contain a parsable double", Toast.LENGTH_LONG).show(); >if(parsable) < Toast.makeText(MainActivity.this, "find " + lat + " : " + lon, Toast.LENGTH_LONG).show(); ListgeoResult = findGeocoder(lat, lon); if(geoResult != null) < ListgeoStringResult = new ArrayList(); for(int i=0; i < geoResult.size(); i++)< Address thisAddress = geoResult.get(i); String stringThisAddress = ""; for(int a=0; a < thisAddress.getMaxAddressLineIndex(); a++) < stringThisAddress += thisAddress.getAddressLine(a) + "\n"; >stringThisAddress += "CountryName: " + thisAddress.getCountryName() + "\n" + "CountryCode: " + thisAddress.getCountryCode() + "\n" + "AdminArea: " + thisAddress.getAdminArea() + "\n" + "FeatureName: " + thisAddress.getFeatureName(); geoStringResult.add(stringThisAddress); > ArrayAdapter adapter = new ArrayAdapter(MainActivity.this, android.R.layout.simple_list_item_1, android.R.id.text1, geoStringResult); listResult.setAdapter(adapter); > > > >); > private List findGeocoder(Double lat, Double lon) < final int maxResults = 5; Listaddresses = null; try < addresses = geocoder.getFromLocation(lat, lon, maxResults); >catch (IOException e) < e.printStackTrace(); >catch (IllegalArgumentException e) < e.printStackTrace(); >return addresses; > > 

Источник

Geocoder

The Mapbox Search SDK for Android is the recommended way to access the Mapbox Geocoding API on the Android platform. If you’ve used the Java SDK’s Geocoder to integrate search functionality into an Android application, you should switch to Search SDK using the Migrate from Geocoder guide.

The Mapbox Geocoding API does two things: forward geocoding and reverse geocoding. Forward geocoding lets you convert location text into geographic coordinates, and reverse geocoding turns geographic coordinates into place names.

You’ll find the wrapper for the Mapbox Geocoding API in the mapbox-java-services module. MapboxGeocoding is used to request both forward and reverse geocoding information. Forward geocoding will take a String , such as a street address or point of interest, and transform it into a Point object. Reverse geocoding does the opposite, taking in a Point object and transforming it into an address. The amount of detail provided in the response varies. For example, one response might contain a full address while another response will only contain the city and country.

For more information about this API, including its pricing structure, see the Mapbox Geocoding API documentation. The API documentation contains all available parameters including some that are not listed in this guide.

Before using this wrapper, make sure you have included the correct permissions inside of your AndroidManifest.xml file if you plan to use this API inside of an Android application.

Geocoding request

Before making a geocoding request, you must build the MapboxGeocoding object by passing in two required parameters: a valid Mapbox access token and a location query (typically an address or description). Many other parameters are available to help bias and manipulate the response that you receive.

If you are using the Mapbox geocoder to find locations around the user’s location, you can use proximity() and pass in their location as a Point object to bias results to around their location.

Источник

Android Geocoding to Get Latitude Longitude for an Address

In this Android tutorial you will learn about Geocoding and how to find latitude/longitude for a give address location. In a previous tutorial we learnt about reverse geocoding to find address for a latitude/longitude. This tutorial is a reverse of that.

Android-Geocoding

Geocoding

Geocoding is converting the physical location address to latitude and longitude. Reverse geocoding is finding the location address based on the latitude and longitude. In this Android tutorial, we will be doing Geocoding to find the Latitude and Longitude of a given address location.

Android Geocoding Example Application

Step1: Define Permissions

We need location access permissions. Following lines should be added to the AndroidManifest.xml.

The complete AndroidManifest.xml :

Get User Input Address

Get user input for address location. Let us have an EditText and get address input. Following is the activity file named activity_geocoding.xml

Step 3: Android Geocoding Activity UI

Following is the Android Activity class GeocodingActivity

package com.javapapers.androidgeocoding; import android.app.Activity; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; public class GeocodingActivity extends Activity < Button addressButton; TextView addressTV; TextView latLongTV; @Override protected void onCreate(Bundle savedInstanceState) < super.onCreate(savedInstanceState); setContentView(R.layout.activity_geocoding); addressTV = (TextView) findViewById(R.id.addressTV); latLongTV = (TextView) findViewById(R.id.latLongTV); addressButton = (Button) findViewById(R.id.addressButton); addressButton.setOnClickListener(new View.OnClickListener() < @Override public void onClick(View arg0) < EditText editText = (EditText) findViewById(R.id.addressET); String address = editText.getText().toString(); GeocodingLocation locationAddress = new GeocodingLocation(); locationAddress.getAddressFromLocation(address, getApplicationContext(), new GeocoderHandler()); >>); > private class GeocoderHandler extends Handler < @Override public void handleMessage(Message message) < String locationAddress; switch (message.what) < case 1: Bundle bundle = message.getData(); locationAddress = bundle.getString("address"); break; default: locationAddress = null; >latLongTV.setText(locationAddress); > > >

Step 4: Geocoder API and Geocoding

We should run the external API access in a separate thread in order not to disturb the UI activity. We should instantiate the Geocoder class and use the method getFromLocationName to get the complete address which includes the latitude and longitude.

package com.javapapers.androidgeocoding; import android.content.Context; import android.location.Address; import android.location.Geocoder; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.util.Log; import java.io.IOException; import java.util.List; import java.util.Locale; public class GeocodingLocation < private static final String TAG = "GeocodingLocation"; public static void getAddressFromLocation(final String locationAddress, final Context context, final Handler handler) < Thread thread = new Thread() < @Override public void run() < Geocoder geocoder = new Geocoder(context, Locale.getDefault()); String result = null; try < ListaddressList = geocoder.getFromLocationName(locationAddress, 1); if (addressList != null && addressList.size() > 0) < Address address = addressList.get(0); StringBuilder sb = new StringBuilder(); sb.append(address.getLatitude()).append("\n"); sb.append(address.getLongitude()).append("\n"); result = sb.toString(); >> catch (IOException e) < Log.e(TAG, "Unable to connect to Geocoder", e); >finally < Message message = Message.obtain(); message.setTarget(handler); if (result != null) < message.what = 1; Bundle bundle = new Bundle(); result = "Address: " + locationAddress + "\n\nLatitude and Longitude :\n" + result; bundle.putString("address", result); message.setData(bundle); >else < message.what = 1; Bundle bundle = new Bundle(); result = "Address: " + locationAddress + "\n Unable to get Latitude and Longitude for this address location."; bundle.putString("address", result); message.setData(bundle); >message.sendToTarget(); > > >; thread.start(); > >

Example Geo coordinates Latitude and Longitude with respective location address:

Latitude, Longitude: 38.898748, -77.037684 Location Address: 1600 Pennsylvania Ave NW Washington DC 20502 Latitude, Longitude: 34.101509, -118.32691 Location Address: Hollywood Blvd & Vine St Los Angeles CA 90028

Download the Android Geocoding Application

Источник

Оцените статью