How to get latitude and longitude passing pincode in Android - android

How to get latitude and longitude by passing parameter as pincode.I have form with pincode. when entered some pincode number into text box,then how to get latitude and longitude.but not passing address how to get latitude and longitude only passing pincode as a parameter.
tried like this it's not working.can any one help me
java
final Geocoder geocoder = new Geocoder(getActivity());
pincode = pincode1.getText().toString();
try {
List<Address> addresses = geocoder.getFromLocationName(pincode, 1);
if (addresses != null && !addresses.isEmpty()) {
Address address = addresses.get(0);
// Use the address as needed
String message = String.format("Latitude: %f, Longitude: %f",
address.getLatitude(), address.getLongitude());
Toast.makeText(getActivity(), message, Toast.LENGTH_LONG).show();
} else {
// Display appropriate message when Geocoder services are not available
Toast.makeText(getActivity(), "Unable to geocode zipcode", Toast.LENGTH_LONG).show();
}
} catch (IOException e) {
// handle exception
}

Try this....
You can make http Get request for the following link.
http://maps.googleapis.com/maps/api/geocode/json?components=postal_code:638701&sensor=false
Demo result:
{
"results" : [
{
"address_components" : [
{
"long_name" : "638701",
"short_name" : "638701",
"types" : [ "postal_code" ]
},
{
"long_name" : "Tamil Nadu",
"short_name" : "TN",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "India",
"short_name" : "IN",
"types" : [ "country", "political" ]
}
],
"formatted_address" : "Tamil Nadu 638701, India",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 11.12035,
"lng" : 77.7041998
},
"southwest" : {
"lat" : 10.9142493,
"lng" : 77.4507146
}
},
"location" : {
"lat" : 11.0179181,
"lng" : 77.58486649999999
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : 11.12035,
"lng" : 77.7041998
},
"southwest" : {
"lat" : 10.9142493,
"lng" : 77.4507146
}
}
},
"place_id" : "ChIJl35xdEKfqTsRcr8NKDs37OM",
"types" : [ "postal_code" ]
}
],
"status" : "OK"
}
change the postal code Element According to your requirements
https://developers.google.com/maps/documentation/geocoding/intro?csw=1#JSON

Related

How to get place-id using geocode latlng address android

I am using MapclickListener onclick of map getting latlng I am geocoding them I want the place id from that geocoded address and the places details using that address.
mMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
#Override
public void onMapClick(LatLng latLng) {
mMap.clear();
mMap.addMarker(new MarkerOptions()
.position(latLng));
Geocoder geocoder = new Geocoder(context, Locale.ENGLISH);
try {
List<Address> addressList = geocoder.getFromLocation(latLng.latitude,latLng.longitude,1);
// Get geocode address
// ***https://maps.googleapis.com/maps/api/geocode/json?address=Address&key=GOOGLE_KEY***
// Get place_id using this address
// Get place details using the place_id
} catch (IOException e) {
e.printStackTrace();
}
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, DEFAULT_ZOOM));
}
});
You have to make two API calls
First would be -
https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=-33.8670522,151.1957362&radius=500&types=food&name=cruise&key=YOUR_API_KEY
Here you will get JSON:
{
"html_attributions" : [],
"results" : [
{
"geometry" : {
"location" : {
"lat" : -33.870775,
"lng" : 151.199025
}
},
...
"place_id" : "ChIJrTLr-GyuEmsRBfy61i59si0",
...
}
],
"status" : "OK"
}
After you get your json, your next step would be to call -
https://maps.googleapis.com/maps/api/geocode/json?place_id=ChIJrTLr-GyuEmsRBfy61i59si0&key=YOUR_API_KEY
Were you will get json like this:
{
"html_attributions" : [],
"result" : {
"address_components" : [
{
"long_name" : "5",
"short_name" : "5",
"types" : [ "floor" ]
}
],
"adr_address" : "5, \u003cspan class=\"street-address\"\u003e48 Pirrama Rd\u003c/span\u003e, \u003cspan class=\"locality\"\u003ePyrmont\u003c/span\u003e \u003cspan class=\"region\"\u003eNSW\u003c/span\u003e \u003cspan class=\"postal-code\"\u003e2009\u003c/span\u003e, \u003cspan class=\"country-name\"\u003eAustralia\u003c/span\u003e",
"formatted_address" : "5, 48 Pirrama Rd, Pyrmont NSW 2009, Australia",
"formatted_phone_number" : "(02) 9374 4000",
"geometry" : {
"location" : {
"lat" : -33.866651,
"lng" : 151.195827
},
"viewport" : {
"northeast" : {
"lat" : -33.8653881697085,
"lng" : 151.1969739802915
},
"southwest" : {
"lat" : -33.86808613029149,
"lng" : 151.1942760197085
}
}
},
"icon" : "https://maps.gstatic.com/mapfiles/place_api/icons/generic_business-71.png",
"id" : "4f89212bf76dde31f092cfc14d7506555d85b5c7",
"international_phone_number" : "+61 2 9374 4000",
"name" : "Google",
"place_id" : "ChIJN1t_tDeuEmsRUsoyG83frY4",
"rating" : 4.5,
"reference" : "CmRSAAAAjiEr2_A4yI-DyqGcfsceTv-IBJXHB5-W3ckmGk9QAYk4USgeV8ihBcGBEK5Z1w4ajRZNVAfSbROiKbbuniq0c9rIq_xqkrf_3HpZzX-pFJuJY3cBtG68LSAHzWXB8UzwEhAx04rgN0_WieYLfVp4K0duGhTU58LFaqwcaex73Kcyy0ghYOQTkg",
"reviews" : [
{
"author_name" : "Robert Ardill",
"author_url" : "https://www.google.com/maps/contrib/106422854611155436041/reviews",
"language" : "en",
"profile_photo_url" : "https://lh3.googleusercontent.com/-T47KxWuAoJU/AAAAAAAAAAI/AAAAAAAAAZo/BDmyI12BZAs/s128-c0x00000000-cc-rp-mo-ba1/photo.jpg",
"rating" : 5,
"relative_time_description" : "a month ago",
"text" : "Awesome offices. Great facilities, location and views. Staff are great hosts",
"time" : 1491144016
}
],
"scope" : "GOOGLE",
"types" : [ "point_of_interest", "establishment" ],
"url" : "https://maps.google.com/?cid=10281119596374313554",
"utc_offset" : 600,
"vicinity" : "5, 48 Pirrama Road, Pyrmont",
"website" : "https://www.google.com.au/about/careers/locations/sydney/"
},
"status" : "OK"
}
BUT: You can also use google geocode lib, where you should add a lot of code and google client inizialization.

How to check if latitude and longitude of a specific address belongs to lat and long of a city?

I offer to users to create an event at some specific place, I use Google places API to auto complete address. So when user types "New Yor" it might offer him "New York, Central Park". If he chooses that, the whole address is saved in database + latitude and longitude of the address.
The problem comes now, when another user wants to see all events from the New York.
Since service I am using doesn't support wild card search, I am looking for a way to see if New York latitude and longitude includes New York, Central Park latitude and longitude.
Any idea?
Some code:
uri = "https://www.maps.google.com/maps/api/geocode/json?address=" +
URLEncoder.encode(params[0].toString(), "UTF-8") +
"&sensor=false";
When you save the results of just the "New York" search, be sure to save the latitude/longitude bounds as well.
http://maps.google.com/maps/api/geocode/json?address=new%20york
The bounds are in the JSON under results->geometry->bounds
{
"results" : [
{
"address_components" : [
{
"long_name" : "New York",
"short_name" : "NY",
"types" : [ "locality", "political" ]
},
{
"long_name" : "New York",
"short_name" : "NY",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "United States",
"short_name" : "US",
"types" : [ "country", "political" ]
}
],
"formatted_address" : "New York, NY, USA",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 40.91525559999999,
"lng" : -73.70027209999999
},
"southwest" : {
"lat" : 40.4913686,
"lng" : -74.25908989999999
}
},
"location" : {
"lat" : 40.7127837,
"lng" : -74.0059413
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : 40.91525559999999,
"lng" : -73.70027209999999
},
"southwest" : {
"lat" : 40.4913686,
"lng" : -74.25908989999999
}
}
},
"types" : [ "locality", "political" ]
}
],
"status" : "OK"
}
Now when you want to see if "New York Central Park" is within "New York", do a simple check to see if central park's lat/long is greater than the southwest boundary, but less than the northeast boundary.

Storing longitude and latitude data in android

I have to store longitude and latitude of all cities of a country in android application
when user will select a city i have to return the longitude and latitude of selected city.
what will be the most efficient way to do this?
or is there any resources where i enter a country and it will show all cities with their longitude and latitude..?
You can use Google Geocoding API : https://developers.google.com/maps/documentation/geocoding/
For example :
https://maps.googleapis.com/maps/api/geocode/json?address=Berlin&sensor=true
And parse output using GSON
{
"results" : [
{
"address_components" : [
{
"long_name" : "Berlin",
"short_name" : "Berlin",
"types" : [ "locality", "political" ]
},
{
"long_name" : "Berlin",
"short_name" : "Berlin",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "Niemcy",
"short_name" : "DE",
"types" : [ "country", "political" ]
}
],
"formatted_address" : "Berlin, Niemcy",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 52.6754542,
"lng" : 13.7611176
},
"southwest" : {
"lat" : 52.33962959999999,
"lng" : 13.0911663
}
},
"location" : {
"lat" : 52.52000659999999,
"lng" : 13.404954
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : 52.6754542,
"lng" : 13.7611176
},
"southwest" : {
"lat" : 52.33962959999999,
"lng" : 13.0911663
}
}
},
"types" : [ "locality", "political" ]
}
],
"status" : "OK"
}

Geting json response from Google places API in android

My simple android application uses google places api to get the latitude and longitude of a place. I am getting the json response from the google api as below
{
"results" : [
{
"address_components" : [
{
"long_name" : "Chennai",
"short_name" : "Chennai",
"types" : [ "locality", "political" ]
},
{
"long_name" : "Chennai",
"short_name" : "Chennai",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "Tamil Nadu",
"short_name" : "TN",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "India",
"short_name" : "IN",
"types" : [ "country", "political" ]
}
],
"formatted_address" : "Chennai, Tamil Nadu, India",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 13.23408510,
"lng" : 80.33229129999999
},
"southwest" : {
"lat" : 12.94592670,
"lng" : 80.14878270
}
},
"location" : {
"lat" : 13.0604220,
"lng" : 80.2495830
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : 13.14736150,
"lng" : 80.37764240
},
"southwest" : {
"lat" : 12.97345190,
"lng" : 80.12152360
}
}
},
"types" : [ "locality", "political" ]
}
],
"status" : "OK"
}
Now i want to get the this part in the json response,
"location" : {
"lat" : 13.0604220,
"lng" : 80.2495830
}
I only get the whole "geometry" part as string through this method
JSONObject e = address.getJSONObject(0);
e.getString("geometry")
How to use the only location values. Any help is appreciated. I'm not sure what is going wrong.
JSONObject e = address.getJSONObject(0);
JSONObject jsonLocation = e.getJSONObject("geometry").getJSONObject("location");
and use
String lat = jsonLocation.getString("lat");
String lng = jsonLocation.getString("lng");
to get latitude and longitude.
You can also get location values as follows:
For Longitude:
Double lon = ((JSONArray)jsonObject.get("results")).getJSONObject(0)
.getJSONObject("geometry").getJSONObject("location")
.getDouble("lng");
For Latitude:
Double lat = ((JSONArray)jsonObject.get("results")).getJSONObject(0)
.getJSONObject("geometry").getJSONObject("location")
.getDouble("lat");
Try this. It worked for me.
JSONObject result = jsonObject.getJSONObject("result");
Double lat = result.getJSONObject("geometry").getJSONObject("location").getDouble("lat");
Double lng = result.getJSONObject("geometry").getJSONObject("location").getDouble("lng");

Google Places JSON Throws a Parsing Error in Android

I'm writing an android app that takes your current location and returns a list of the stores near you using the Google Places web service. I'm able to get back (what I think is) valid JSON, but when I go to parse the JSON using the Android JSONObject library, I get a parsing error.
Here is my class:
public class StoresNearMe {
private String result = "";
private String googleAPIKey = "Omitted for this example";
private String searchRadius = "50";
private Location myLocation;
final String TAG = getClass().getSimpleName();
public StoresNearMe(Location location){
myLocation = location;
}//StoresNearMe
public String getStoresNearMe(){
String result = "Nothing";
result = callGoogleWebService(buildURLForGooglePlaces(myLocation));
convertJSONtoArray(result);
return result;
}//getStoresNearMe
private String callGoogleWebService(String url){
HttpClient httpclient = new DefaultHttpClient();
HttpGet request = new HttpGet(url);
//request.addHeader("deviceId", deviceId);
ResponseHandler<String> handler = new BasicResponseHandler();
try {
result = httpclient.execute(request, handler);
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
httpclient.getConnectionManager().shutdown();
return result;
}//callWebService
private String buildURLForGooglePlaces(Location location){
String baseUrl = "https://maps.googleapis.com/maps/api/place/search/json?";
String lat = String.valueOf(location.getLatitude());
String lon = String.valueOf(location.getLongitude());
String url = baseUrl + "location=" + lat + "," + lon + "&" +
"radius=" + searchRadius + "&" + "sensor=false" +
"&" + "key=" + googleAPIKey;
Log.d(TAG,url);
return url;
}//buildURLForGooglePlaces
private void convertJSONtoArray(String rawJSON){
try {
JSONObject completeJSONObj = new JSONObject(rawJSON);
String json = completeJSONObj.toString();
Log.d(TAG,json);
JSONObject results = completeJSONObj.getJSONObject("results");
} catch (JSONException e) {
Log.d(TAG,"JSON parsing error - fix it:" + e.getMessage());
}
}//convertJSONtoArray
}//StoresNearMe
Here is the JSON returned by my call:
{
"html_attributions" : [
"Listings by \u003ca href=\"http://www.yellowpages.com.au/\"\u003eYellow Pages\u003c/a\u003e"
],
"results" : [
{
"geometry" : {
"location" : {
"lat" : -33.86694910,
"lng" : 151.19549870
}
},
"icon" : "http://maps.gstatic.com/mapfiles/place_api/icons/geocode-71.png",
"id" : "96760d4544ecdaaf2e87565915638238ca960f20",
"name" : "Pirrama Rd",
"reference" : "CnRqAAAAL0xKaK8vGlVlD-E5j-88K9YTuCKlOFfoBxhBUwMIBGxNQDC8UdkULJiiC0ICBRxpddCV09wyc-HtShEpquP-drX4sj4tI_tbfveDIbzfSAUaNQjYhhfjQ-eVi0VeWBXdXH0A8Z_-nKXKMo1fvCHtBhIQlAeLnFtotWnYonMUpU1DqxoUcI1weYioNnJPtnhXCThmx0W0ubU",
"types" : [ "route" ],
"vicinity" : "Pyrmont"
},
{
"geometry" : {
"location" : {
"lat" : -33.8669710,
"lng" : 151.1958750
}
},
"icon" : "http://maps.gstatic.com/mapfiles/place_api/icons/generic_business-71.png",
"id" : "4f89212bf76dde31f092cfc14d7506555d85b5c7",
"name" : "Google Sydney",
"reference" : "CnRkAAAAqvesgoigCyiZfDUwqjbravZHH5zsXrmskbQVhkFk1JMnnrnKNGQyoHBBYofTML_zZqaAUAKJeJCvReUn62oGzSgPgNSjWaXUVorKGDXAAjhGyeh2pRrJFwuC5qsgBYdyN4kY8kspJGu_bMo3xrranxIQ-mFIK_28TAqiagOAzY7RiBoUrZTvFJYSn0OXimLRRb_SQ3OKW44",
"types" : [ "establishment" ],
"vicinity" : "Pirrama Road, Pyrmont"
},
{
"geometry" : {
"location" : {
"lat" : -33.8670840,
"lng" : 151.195950
}
},
"icon" : "http://maps.gstatic.com/mapfiles/place_api/icons/generic_business-71.png",
"id" : "27e84d1acda19c14b9c7f5271242796891bc9c9e",
"name" : "Accenture",
"reference" : "CoQBfQAAAOpBriMi1e4itUquHQnv_jQyEOibrEnbWfAuJBJqBUzcTqNvY6luCcgiqHr3ensEO81bcTkt4fXGHFLxM7pERmxVvFnRnLsLy5xyQtVxIp8eQnYP6AZxLZyxbfGceG87r6A1lHCOQTFxK85HVFq7pYik9ud_ExxEtwHT3gO54pihEhDYEZ59AYpLMDmlRHk5__q_GhQdTS2wSOU2sAxy0j4J7EYwCuq6Iw",
"types" : [ "establishment" ],
"vicinity" : "Pirrama Road, Pyrmont"
},
{
"geometry" : {
"location" : {
"lat" : -33.8670520,
"lng" : 151.1957360
}
},
"icon" : "http://maps.gstatic.com/mapfiles/place_api/icons/generic_business-71.png",
"id" : "3ef986cd56bb3408bc1cf394f3dad9657c1d30f6",
"name" : "Doltone House - Darling Island Wharf",
"reference" : "CoQBewAAAExUq-YlS-bahqAJeJbfyGfkUrqyuKjZgCPxHznMZsHg1aHLWoFk8s_04DxDtEFAlSyRP-ZHNsceha6MB8JD7zFMjbYOKNJLSm9OcgFUOFB-4SZP3ZeRyYzna2032gayzUU1_8N-qtb_r2kfllGYAmgnaU7h6RC8haaf-dFfvdiiEhBUdWHpEeL7qwtB91BgNYdPGhTAjf0Xa7-MKHJRRCQu_g7XL80dzQ",
"types" : [ "establishment" ],
"vicinity" : "Pirrama Road, Pyrmont"
},
{
"geometry" : {
"location" : {
"lat" : -33.8671110,
"lng" : 151.1958750
}
},
"icon" : "http://maps.gstatic.com/mapfiles/place_api/icons/generic_business-71.png",
"id" : "258f23471a33cfa2828170660d94cd5797edb18c",
"name" : "Workplace6",
"reference" : "CnRhAAAAi_hm5c3gpntkNvC7NbQtIe5tcmosCIHHbzqhLURaH7mQ6inEOqg47_cltBrtFBEPSIqCuPQDC3UA-suLBfRslIwUNytKjC87Lt1ZrGXF6ZmnOR2rVcdCGdoWrdi977RHruBO21zbjzVWE-A3ZDn2NBIQ1Vu2-nr3E02DuJAtXL8x4BoUEBRWEHReK2LO3IoldvyyvSKb0Qs",
"types" : [ "establishment" ],
"vicinity" : "Pirrama Rd, Pyrmont"
},
{
"geometry" : {
"location" : {
"lat" : -33.8670520,
"lng" : 151.1957360
}
},
"icon" : "http://maps.gstatic.com/mapfiles/place_api/icons/generic_business-71.png",
"id" : "fe9136d6406ed2d35660dea37a0be34043c1a4f2",
"name" : "Doltone House",
"reference" : "CnRjAAAAo4zx1lM-veFEPBBeIRFETZoH8QRMmpHIBVtnmfOZ-Sh1-8p4vxKj-146pHGW6wPCZxIV0iIuAGow9OSsHubvY_kSnlBe5OB99ZSxHQsm0LrLh45c05Gf7C8jwp_9D7keDVDAC47MN65niSZW71fOrRIQxODppLktQZFWbf-V1N4gHxoUPetbj0zbU1kQj96YrRLzLKrbMoE",
"types" : [ "establishment" ],
"vicinity" : "Pirrama Road, Pyrmont"
},
{
"geometry" : {
"location" : {
"lat" : -33.8666470,
"lng" : 151.195650
}
},
"icon" : "http://maps.gstatic.com/mapfiles/place_api/icons/generic_business-71.png",
"id" : "2722b46120160ec0a322e0a13cb0717ed41a696b",
"name" : "Biaggio",
"reference" : "CmRdAAAAp7wiB1b9cK-26FjhO2DVAGA-5uzuZxppHqCkR75dQxoMJiLD5G7H90_dKtNVDlRAdKSOx7hWlUoHGuwkbBA4ZhNIDrPdLWQ1a41sEN9gZJvU-t7uxoRzziZpTVSCU-AXEhDoQ36icc3OBaMH1xxnPgsEGhTB08nD6Q67ojrGYMJWrd4PojJKsw",
"types" : [ "establishment" ],
"vicinity" : "Pirrama Rd, Pyrmont"
},
{
"geometry" : {
"location" : {
"lat" : -33.86954560,
"lng" : 151.19454040
}
},
"icon" : "http://maps.gstatic.com/mapfiles/place_api/icons/geocode-71.png",
"id" : "40840d476baa531e0227a353b1bef70262f66e7e",
"name" : "Pyrmont",
"reference" : "CnRnAAAA7aHZwg2JyjTI-xotS1q4XKgbg3eCUwOZm3Nz55Xf5Q2w-nZpBrrd2VAEl60On0puVl8bX7UaJTIECELr3KY2mL4s7zsIznItOiJEG-9QMlnreRSmxSBSUAkPghKWoq4OFbCr0dwNtZSKJFazcbCc1BIQSz5BZfCm-iyMZ8YOgvc4tBoU1CdnwsHS5PhB69jpYHsOvfWXEgo",
"types" : [ "locality", "political" ]
}
],
"status" : "OK"
}
Any help is appreciated - I'm not sure what is going wrong.
Thanks!
The problem is that you're trying to read an array in as an object.
Change
JSONObject results = completeJSONObj.getJSONObject("results");
to
JSONArray results = completeJSONObj.getJSONArray("results");
and the parse exception goes away.
your JSON should be Valid : example :
{\"key\":\"value\" }

Categories

Resources