How to Find the Latitude and Longitude from Address

How to get complete address from latitude and longitude?

Geocoder geocoder;
List<Address> addresses;
geocoder = new Geocoder(this, Locale.getDefault());

addresses = geocoder.getFromLocation(latitude, longitude, 1); // Here 1 represent max location result to returned, by documents it recommended 1 to 5

String address = addresses.get(0).getAddressLine(0); // If any additional address line present than only, check with max available address lines by getMaxAddressLineIndex()
String city = addresses.get(0).getLocality();
String state = addresses.get(0).getAdminArea();
String country = addresses.get(0).getCountryName();
String postalCode = addresses.get(0).getPostalCode();
String knownName = addresses.get(0).getFeatureName(); // Only if available else return NULL

For more info of available details, Look at Android-Location-Address

How can I find the latitude and longitude from address?

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;
} catch (IOException e) {
e.printStackTrace();
}
return null;
}

strAddress is a string containing the address. The address variable holds the converted addresses.

Get the particular address using latitude and longitude

From a Geocoder object, you can call the getFromLocation(double, double, int) method.

like :-

private String getAddress(double latitude, double longitude) {
StringBuilder result = new StringBuilder();
try {
Geocoder geocoder = new Geocoder(this, Locale.getDefault());
List<Address> addresses = geocoder.getFromLocation(latitude, longitude, 1);
if (addresses.size() > 0) {
Address address = addresses.get(0);
result.append(address.getLocality()).append("\n");
result.append(address.getCountryName());
}
} catch (IOException e) {
Log.e("tag", e.getMessage());
}

return result.toString();
}

Another best answer is here How to get city name from latitude and longitude coordinates in Google Maps?

Get latitude,longitude from address in Android

The Geocoder API provided in the Android Framework

I have previously worked with the Geocoding API which exists in the Android API but it does not work on all devices. In fact, as per my and others experiences, a lot of devices will return null when using the Geocoding API.

For that reason, I have chosen to use the Reverse Geocoder which works perfectly on all devices, but requires an additional overhead due to the HTTP request.

Retrieving the longitude and latitude from an address using the Reverse Geocoding API

To avoid this issue, it is a simple matter of using the Reverse Geocoding API which returns a JSON Object.

You can either use the API to find an address through latitude and longitude, or find latitude and longitude from an address:

http://maps.googleapis.com/maps/api/geocode/json?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA&sensor=true_or_false

Returns:

{
"results" : [
{
"address_components" : [
{
"long_name" : "1600",
"short_name" : "1600",
"types" : [ "street_number" ]
},
{
"long_name" : "Amphitheatre Pkwy",
"short_name" : "Amphitheatre Pkwy",
"types" : [ "route" ]
},
{
"long_name" : "Mountain View",
"short_name" : "Mountain View",
"types" : [ "locality", "political" ]
},
{
"long_name" : "Santa Clara",
"short_name" : "Santa Clara",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "California",
"short_name" : "CA",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "United States",
"short_name" : "US",
"types" : [ "country", "political" ]
},
{
"long_name" : "94043",
"short_name" : "94043",
"types" : [ "postal_code" ]
}
],
"formatted_address" : "1600 Amphitheatre Pkwy, Mountain View, CA 94043, USA",
"geometry" : {
"location" : {
"lat" : 37.42291810,
"lng" : -122.08542120
},
"location_type" : "ROOFTOP",
"viewport" : {
"northeast" : {
"lat" : 37.42426708029149,
"lng" : -122.0840722197085
},
"southwest" : {
"lat" : 37.42156911970850,
"lng" : -122.0867701802915
}
}
},
"types" : [ "street_address" ]
}
],
"status" : "OK"
}

It is hereby a simple matter of sending a HTTP request to the Reverse Geocoding API with an address entered by the user, and thereafter parsing the JSON Object to find the data you need.

A previous post describes how a JSON object can be parsed from an URL.

Hope this helps.

PHP - Get Latitude & Longitude from an address

Suppose you have hidden value for lat and long is
mapLat & mapLong and input field name is location
then:

<html>
<form>
<input type="hidden" name="mapLat">
<input type="hidden" name="mapLong">
<input type="text" name="location">
<input type="submit" name="submit" value="submit">
</form>
</html>



extract($_POST);
if($mapLat =='' && $mapLong ==''){
// Get lat long from google
$latlong = get_lat_long($location); // create a function with the name "get_lat_long" given as below
$map = explode(',' ,$latlong);
$mapLat = $map[0];
$mapLong = $map[1];
}


// function to get the address
function get_lat_long($address){

$address = str_replace(" ", "+", $address);

$json = file_get_contents("http://maps.google.com/maps/api/geocode/json?address=$address&sensor=false®ion=$region");
$json = json_decode($json);

$lat = $json->{'results'}[0]->{'geometry'}->{'location'}->{'lat'};
$long = $json->{'results'}[0]->{'geometry'}->{'location'}->{'lng'};
return $lat.','.$long;
}

Obtain coordinates from an address flutter

Here created an Input Text Widget and when user taps on it. This takes to google autocomplete screen, where user inputs the location.

TextFormField(
decoration: new InputDecoration(
border: InputBorder.none,
contentPadding: EdgeInsets.only(left: 15),
hintText: Strings.enter_your_house_number_street_etc,
hintStyle: TextStyle(
fontSize: 14,
color: AppColor.grey,
fontFamily: "Open Sans",
fontWeight: FontWeight.normal
)),
maxLines: 1,
controller: _address,
onTap: ()async{
// then get the Prediction selected
Prediction p = await PlacesAutocomplete.show(
context: context, apiKey: kGoogleApiKey,
onError: onError);
displayPrediction(p);
},
)

Here it is getting the lat and long of entered location.

Future<Null> displayPrediction(Prediction p) async {
if (p != null) {
PlacesDetailsResponse detail = await _places.getDetailsByPlaceId(p.placeId);

var placeId = p.placeId;
lat = detail.result.geometry.location.lat;
long = detail.result.geometry.location.lng;

var address =detail.result.formattedAddress;

print(lat);
print(long);
print(address);

setState(() {
_address.text = address;
});
}
}

import 'package:flutter_google_places/flutter_google_places.dart';

Get Latitude / Longitude from Address with Accuracy

It's true, businesses are excluded from Geocoding API. If you need a location for certain business just use the Places API.

E.g.
https://maps.googleapis.com/maps/api/place/textsearch/json?query=Shop%20No%2056%2C%20Sector%2014%2C%20Haryana%2C%20Gurgaon%2C%20IN&key=YOUR_API_KEY

Get latitude & longitude from given address name. NOT Geocoder

Albert, I think your concern is that you code is not working. Here goes, the code below works for me really well. I think you are missing URIUtil.encodeQuery to convert your string to a URI.

I am using gson library, download it and add it to your path.

To get the class's for your gson parsing, you need to goto jsonschema2pojo. Just fire http://maps.googleapis.com/maps/api/geocode/json?address=Sayaji+Hotel+Near+balewadi+stadium+pune&sensor=true on your browser, get the results and paste it on this site. It will generate your pojo's for you. You may need to also add a annotation.jar.

Trust me, it is easy to get working. Don't get frustrated yet.

try {
URL url = new URL(
"http://maps.googleapis.com/maps/api/geocode/json?address="
+ URIUtil.encodeQuery("Sayaji Hotel, Near balewadi stadium, pune") + "&sensor=true");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setRequestProperty("Accept", "application/json");

if (conn.getResponseCode() != 200) {
throw new RuntimeException("Failed : HTTP error code : " + conn.getResponseCode());
}
BufferedReader br = new BufferedReader(new InputStreamReader((conn.getInputStream())));

String output = "", full = "";
while ((output = br.readLine()) != null) {
System.out.println(output);
full += output;
}

PincodeVerify gson = new Gson().fromJson(full, PincodeVerify.class);
response = new IsPincodeSupportedResponse(new PincodeVerifyConcrete(
gson.getResults().get(0).getFormatted_address(),
gson.getResults().get(0).getGeometry().getLocation().getLat(),
gson.getResults().get(0).getGeometry().getLocation().getLng())) ;
try {
String address = response.getAddress();
Double latitude = response.getLatitude(), longitude = response.getLongitude();
if (address == null || address.length() <= 0) {
log.error("Address is null");
}
} catch (NullPointerException e) {
log.error("Address, latitude on longitude is null");
}
conn.disconnect();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

The Geocode http works, I just fired it, results below

{
"results" : [
{
"address_components" : [
{
"long_name" : "Pune",
"short_name" : "Pune",
"types" : [ "locality", "political" ]
},
{
"long_name" : "Pune",
"short_name" : "Pune",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "Maharashtra",
"short_name" : "MH",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "India",
"short_name" : "IN",
"types" : [ "country", "political" ]
}
],
"formatted_address" : "Pune, Maharashtra, India",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 18.63469650,
"lng" : 73.98948670
},
"southwest" : {
"lat" : 18.41367390,
"lng" : 73.73989109999999
}
},
"location" : {
"lat" : 18.52043030,
"lng" : 73.85674370
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : 18.63469650,
"lng" : 73.98948670
},
"southwest" : {
"lat" : 18.41367390,
"lng" : 73.73989109999999
}
}
},
"types" : [ "locality", "political" ]
}
],
"status" : "OK"
}

Edit

On 'answers do not contain enough detail`

From all the research you are expecting that google map have a reference to every combination of locality, area, city. But the fact remains that google map contains geo and reverse-geo in its own context. You cannot expect it to have a combination like Sayaji Hotel, Near balewadi stadium, pune. Web google maps will locate it for you since it uses a more extensive Search rich google backend. The google api's only reverse geo address received from their own api's. To me it seems like a reasonable way to work, considering how complex our Indian address system is, 2nd cross road can be miles away from 1st Cross road :)



Related Topics



Leave a reply



Submit