File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Google MAp Api Inside Java Classes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Google MAp Api Inside Java Classes " Watch "Google MAp Api Inside Java Classes " New topic
Author

Google MAp Api Inside Java Classes

nagraj patel
Greenhorn

Joined: Feb 16, 2013
Posts: 18
hey Experts,


Anyone Working on Google Map Api ? .One of my apps i want to use google api in normal java classes how I do this.

public static Location GeoCode(String address){
}

public static String ReverseGeoCode(Location location){
} these are the verbs I want to use my GPSUtil Class


thanks and regards ,

Nagendra Patel
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41057
    
  43
What, exactly, is the problem you're facing? In other words, what do you have so far, and where are you stuck making progress?

By the way, those aren't "verbs", they're methods. Although -having an uppercase first letter- they look like they were written by an unexperienced programmer. Are you sure these are part of the Google Maps API?

This doesn't seem to have anything to do with Tomcat, so I'll move it to a more appropriate forum.


Ping & DNS - my free Android networking tools app
nagraj patel
Greenhorn

Joined: Feb 16, 2013
Posts: 18
Hi Ulf Dittmer,



I am really did mistake Actually I copied documentation code .I want to calculate exact location for reverseGeoCode(){},and geoCode(){}.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41057
    
  43
So what is the exact problem you're facing? Have you downloaded the Google Maps client library? Have you worked through all the extensive online documentation that is available? (And by "worked through" I don't mean "read", I mean "compiled and successfully run some example codes".)
nagraj patel
Greenhorn

Joined: Feb 16, 2013
Posts: 18
public class GettingAddress {

public String getAddress(double latitude, double longitude) {
int flag=0;
String strAddress = "";
String url="http://maps.googleapis.com/maps/api/geocode/json?latlng="+ latitude+","+ longitude+"&sensor=true";
//28.554952, 77.191650

//String url="http://maps.googleapis.com/maps/api/geocode/json?latlng=28.519180,77.213010&sensor=false";
HttpPost httppost = new HttpPost(url);
HttpClient httpclient = new DefaultHttpClient();
String result = "";

try
{
HttpResponse response = httpclient.execute(httppost);
HttpEntity resEntityGet = response.getEntity();
if (resEntityGet != null) {
result = EntityUtils.toString(resEntityGet);

}


} catch (Exception e) {

System.out.println("Exception:" + e);
}


System.out.println("Result: " + result);

try
{
if (result != null) {


JSONObject json=new JSONObject(result);

JSONArray ja = json.getJSONArray("results");

for (int i = 0; i < 1; i++) {

JSONObject jo = ja.getJSONObject(i);
JSONArray jaa = jo.getJSONArray("address_components");
for (int j = 0; j < jaa.length(); j++) {

JSONObject jotwo = jaa.getJSONObject(j);
strAddress=jotwo.getString("long_name");
System.out.println(" " +strAddress);
}

}

if(strAddress.length()>0)
{
flag =1;
}

//System.err.println("%%%%%%%: " + strAddress );
}

}catch (Exception e) {

}

return strAddress;
}



public static void main(String arg[])
{
double latitude,longitude;
String decider="y";
Scanner sc;
GettingAddress ga = new GettingAddress();
do
{
sc= new Scanner(System.in);

System.out.println("\nEnter Latitude:");
latitude=sc.nextDouble();
System.out.println("\nEnter Longitude:");
longitude=sc.nextDouble();
String str = ga.getAddress(latitude,longitude);
System.err.println("Hello I am beautiful: " + str);
System.out.println("whant more address");
}

while("y".equals(decider));

}
}
i am using This http url api and i am getting many address but i want to get exact location ?




---------------------------------------------------------------------------------
this is output i am getting ................
Result: {
"results" : [
{
"address_components" : [
{
"long_name" : "Najafgarh Dhansa Road",
"short_name" : "Najafgarh Dhansa Road",
"types" : [ "route" ]
},
{
"long_name" : "New Delhi",
"short_name" : "New Delhi",
"types" : [ "locality", "political" ]
},
{
"long_name" : "Delhi",
"short_name" : "Delhi",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "Delhi",
"short_name" : "DL",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "India",
"short_name" : "IN",
"types" : [ "country", "political" ]
},
{
"long_name" : "110073",
"short_name" : "110073",
"types" : [ "postal_code" ]
}
],
"formatted_address" : "Najafgarh Dhansa Road, New Delhi, Delhi 110073, India",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 28.57610540,
"lng" : 76.87860689999999
},
"southwest" : {
"lat" : 28.56929650,
"lng" : 76.8752820
}
},
"location" : {
"lat" : 28.57271140,
"lng" : 76.87690780
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : 28.57610540,
"lng" : 76.87860689999999
},
"southwest" : {
"lat" : 28.56929650,
"lng" : 76.8752820
}
}
},
"types" : [ "route" ]
},
{
"address_components" : [
{
"long_name" : "Ujwa",
"short_name" : "Ujwa",
"types" : [ "sublocality", "political" ]
},
{
"long_name" : "New Delhi",
"short_name" : "New Delhi",
"types" : [ "locality", "political" ]
},
{
"long_name" : "South West Delhi",
"short_name" : "South West Delhi",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "Delhi",
"short_name" : "DL",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "India",
"short_name" : "IN",
"types" : [ "country", "political" ]
}
],
"formatted_address" : "Ujwa, New Delhi, Delhi, India",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 28.58697120,
"lng" : 76.93783519999999
},
"southwest" : {
"lat" : 28.54931850,
"lng" : 76.89198010
}
},
"location" : {
"lat" : 28.56526910,
"lng" : 76.91244650
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : 28.58697120,
"lng" : 76.93783519999999
},
"southwest" : {
"lat" : 28.54931850,
"lng" : 76.89198010
}
}
},
"types" : [ "sublocality", "political" ]
},
{
"address_components" : [
{
"long_name" : "110073",
"short_name" : "110073",
"types" : [ "postal_code" ]
},
{
"long_name" : "India",
"short_name" : "IN",
"types" : [ "country", "political" ]
}
],
"formatted_address" : "110073, India",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 28.61800010,
"lng" : 76.97399990
},
"southwest" : {
"lat" : 28.50070,
"lng" : 76.83959990
}
},
"location" : {
"lat" : 28.55897450,
"lng" : 76.90668289999999
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : 28.61800010,
"lng" : 76.97399990
},
"southwest" : {
"lat" : 28.50070,
"lng" : 76.83959990
}
}
},
"types" : [ "postal_code" ]
},
{
"address_components" : [
{
"long_name" : "South West Delhi",
"short_name" : "South West Delhi",
"types" : [ "administrative_area_level_2", "political" ]
},
{
"long_name" : "Delhi",
"short_name" : "DL",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "India",
"short_name" : "IN",
"types" : [ "country", "political" ]
}
],
"formatted_address" : "South West Delhi, Delhi, India",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 28.67055780,
"lng" : 77.21002570
},
"southwest" : {
"lat" : 28.48702460,
"lng" : 76.83906560
}
},
"location" : {
"lat" : 28.57829320,
"lng" : 76.98734770
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : 28.67055780,
"lng" : 77.21002570
},
"southwest" : {
"lat" : 28.48702460,
"lng" : 76.83906560
}
}
},
"types" : [ "administrative_area_level_2", "political" ]
},
{
"address_components" : [
{
"long_name" : "New Delhi",
"short_name" : "New Delhi",
"types" : [ "locality", "political" ]
},
{
"long_name" : "Delhi",
"short_name" : "DL",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "India",
"short_name" : "IN",
"types" : [ "country", "political" ]
}
],
"formatted_address" : "New Delhi, Delhi, India",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 28.88225350,
"lng" : 77.34169930
},
"southwest" : {
"lat" : 28.42219740,
"lng" : 76.85382849999999
}
},
"location" : {
"lat" : 28.6353080,
"lng" : 77.224960
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : 28.88225350,
"lng" : 77.34169930
},
"southwest" : {
"lat" : 28.42219740,
"lng" : 76.85382849999999
}
}
},
"types" : [ "locality", "political" ]
},
{
"address_components" : [
{
"long_name" : "Delhi",
"short_name" : "DL",
"types" : [ "administrative_area_level_1", "political" ]
},
{
"long_name" : "India",
"short_name" : "IN",
"types" : [ "country", "political" ]
}
],
"formatted_address" : "Delhi, India",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 28.8813380,
"lng" : 77.34845780000001
},
"southwest" : {
"lat" : 28.4125930,
"lng" : 76.83806899999999
}
},
"location" : {
"lat" : 28.70405920,
"lng" : 77.10249019999999
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : 28.8813380,
"lng" : 77.34845780000001
},
"southwest" : {
"lat" : 28.4125930,
"lng" : 76.83806899999999
}
}
},
"types" : [ "administrative_area_level_1", "political" ]
},
{
"address_components" : [
{
"long_name" : "India",
"short_name" : "IN",
"types" : [ "country", "political" ]
}
],
"formatted_address" : "India",
"geometry" : {
"bounds" : {
"northeast" : {
"lat" : 35.50434040,
"lng" : 97.3955550
},
"southwest" : {
"lat" : 6.747138899999999,
"lng" : 68.16238590
}
},
"location" : {
"lat" : 20.5936840,
"lng" : 78.962880
},
"location_type" : "APPROXIMATE",
"viewport" : {
"northeast" : {
"lat" : 35.50434040,
"lng" : 97.3955550
},
"southwest" : {
"lat" : 6.747138899999999,
"lng" : 68.16279560
}
}
},
"types" : [ "country", "political" ]
}
],
"status" : "OK"
}

Hello I am beautiful: 110073
Najafgarh Dhansa Road
New Delhi
Delhi
Delhi
India
110073
nagraj patel
Greenhorn

Joined: Feb 16, 2013
Posts: 18
package com.s5.server.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Serializable;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.util.Arrays;
import java.util.Scanner;



import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONObject;

import sun.management.counter.StringCounter;


import com.google.android.maps.GeoPoint;
import com.google.gson.FieldNamingPolicy;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

public class GettingAddress1 {

private String jsonCoord(String address) throws IOException {
URL url = new URL("http://maps.googleapis.com/maps/api/geocode/json?address=" + address + "&sensor=false");
URLConnection connection = url.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String inputLine;
String jsonResult = "";
while ((inputLine = in.readLine()) != null) {
jsonResult += inputLine;
}
in.close();
return jsonResult;
}



public AddressComponent [] getAddress1(double latitude, double longitude) {
int flag=0;
String strAddress = "";
String formateAddrs="";

String url="http://maps.googleapis.com/maps/api/geocode/json?latlng="+ latitude+","+ longitude+"&sensor=true";
AddressComponent [] addressComponents= new AddressComponent[10];
//String url="http://maps.googleapis.com/maps/api/geocode/json?latlng=28.519180,77.213010&sensor=false";
HttpPost httppost = new HttpPost(url);
HttpClient httpclient = new DefaultHttpClient();
String result = "";

try
{
HttpResponse response = httpclient.execute(httppost);
HttpEntity resEntityGet = response.getEntity();
if (resEntityGet != null) {
result = EntityUtils.toString(resEntityGet);

}


} catch (Exception e) {

System.out.println("Exception:" + e);
}


// System.out.println("Result: " + result);

try
{
if (result != null) {

/**
*GSON by default ignores all json attributes that do not have corresponding Java fields.
*
*
***/

Gson gson = new GsonBuilder().setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES).create();
Response response = gson.fromJson(result, Response.class);
addressComponents = response.getResults()[1].getAddressComponents();
String longname = response.getResults()[0].getAddressComponents()[0].getLongName();
String shortname = response.getResults()[0].getAddressComponents()[0].getShortName();
String [] typesOfAddress= response.getResults()[1].getAddressComponents()[1].getTypes();

System.out.println("Long Name Address>>>>>>>>>>>>>>>>>>>>>>>>>>>"+longname);
System.out.println("Short Name Address>>>>>>>>>>>>>>>>>>>>>>>>>>>"+shortname);
System.out.println("Type Name Address>>>>>>>>>>>>>>>>>>>>>>>>>>>"+Arrays.toString(typesOfAddress));
formateAddrs = response.getResults()[0].getFormattedAddress();
System.out.println("formateAddrs>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"+formateAddrs);
System.out.println("Address Component>>>>>>>>>>>>>>>"+Arrays.toString(addressComponents));


if(strAddress.length()>0)
{
flag =1;
}

Gson gson1 = new Gson();
Response response1 = gson1.fromJson(jsonCoord(URLEncoder.encode(result, "UTF-8")),

Response.class);
// double lat = Double.parseDouble(response1.getResults()[1].getAddressComponents()[1].getTypes());

//double lng = Double.parseDouble(response1.getResults()[1].getAddressComponents()[1].getTypes());




}

}catch (Exception e) {

}

return addressComponents;
}



public static void main(String arg[])
{


double latitude,longitude;
String decider="y";
Scanner sc;
GettingAddress1 ga = new GettingAddress1();
do
{
sc= new Scanner(System.in);

System.out.println("\nEnter Latitude:");
latitude=sc.nextDouble();
System.out.println("\nEnter Longitude:");
longitude=sc.nextDouble();
AddressComponent [] addresses = ga.getAddress1(latitude,longitude);

System.out.println("From latitude and longitude address EQUIVALENT"+Arrays.toString(addresses));
}

while("y".equals(decider));

}


}
class AddressComponent implements Serializable{
public String[] getTypes() {
return types;
}
public void setTypes(String[] types) {
this.types = types;
}


/**
*
*/

private String longName;
private String shortName;
private String [] types;

public String getLongName() {
return longName;
}
public void setLongName(String longName) {
this.longName = longName;
}
public String getShortName() {
return shortName;
}
public void setShortName(String shortName) {
this.shortName = shortName;
}


public String toString() {
String outString = new String();
outString+="longName>>>"+longName+">>"+"shortName>>>>>>"+shortName+">>"+"types>>>>>>>>"+Arrays.toString(types);
outString += "\n";
return outString;
}



}
class Result implements Serializable {

/**
*
*/

private AddressComponent[] addressComponents;
private String formattedAddress;

public String getFormattedAddress() {
return formattedAddress;
}
public void setFormattedAddress(String formattedAddress) {
this.formattedAddress = formattedAddress;
}

public AddressComponent[] getAddressComponents() {
return addressComponents;
}
public void setAddressComponents(AddressComponent[] addressComponents) {
this.addressComponents = addressComponents;
}

public String toString() {
String outString = new String();
outString+="addressComponents>>>>>>>>>>>>>"+Arrays.toString(addressComponents)+"formatted_address>>>>>>>"+formattedAddress+">>";
outString += "\n";
return outString;
}

}
class Response implements Serializable{
/**
*
*/

private Result [] results;

public Result[] getResults() {
return results;
}

public void setResults(Result[] results) {
this.results = results;
}



public String toString() {
String outString = new String();
outString+="results>>>>>>>>>"+Arrays.toString(results);
outString += "\n";
return outString;
}

}
class Type implements Serializable {
private String administrativearealevel1, political;



public String getAdministrativearealevel1() {
return administrativearealevel1;
}

public void setAdministrativearealevel1(String administrativearealevel1) {
this.administrativearealevel1 = administrativearealevel1;
}

public String getPolitical() {
return political;
}

public void setPolitical(String political) {
this.political = political;
}

public String toString() {
String outString = new String();
outString+="administrative_area_level_1>>>>>>>>>"+administrativearealevel1+"political>>>>>>>>>"+political;
outString += "\n";
return outString;
}


}

Hello Experts I have problem In Google Map API this ss class that I am using, I want to get all component off type[] array that i can store all information in my been class Address Component ( like Country , Street name , State,Pincode);

And This is My OUTPUT://


Enter Latitude:
28.55495

Enter Longitude:
77.19165
Long Name Address>>>>>>>>>>>>>>>>>>>>>>>>>>>Hauz Khas Tank
Short Name Address>>>>>>>>>>>>>>>>>>>>>>>>>>>Hauz Khas Tank
Type Name Address>>>>>>>>>>>>>>>>>>>>>>>>>>>[locality, political]
formateAddrs>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>Hauz Khas Tank, Hauz Khas, New Delhi, Delhi 110016, India
Address Component>>>>>>>>>>>>>>>[longName>>>Hauz Khas>>shortName>>>>>>Hauz Khas>>types>>>>>>>>[sublocality, political]
, longName>>>New Delhi>>shortName>>>>>>New Delhi>>types>>>>>>>>[locality, political]
, longName>>>South Delhi>>shortName>>>>>>South Delhi>>types>>>>>>>>[administrative_area_level_2, political]
, longName>>>Delhi>>shortName>>>>>>DL>>types>>>>>>>>[administrative_area_level_1, political]
, longName>>>India>>shortName>>>>>>IN>>types>>>>>>>>[country, political]
]
From latitude and longitude address EQUIVALENT[longName>>>Hauz Khas>>shortName>>>>>>Hauz Khas>>types>>>>>>>>[sublocality, political]
, longName>>>New Delhi>>shortName>>>>>>New Delhi>>types>>>>>>>>[locality, political]
, longName>>>South Delhi>>shortName>>>>>>South Delhi>>types>>>>>>>>[administrative_area_level_2, political]
, longName>>>Delhi>>shortName>>>>>>DL>>types>>>>>>>>[administrative_area_level_1, political]
, longName>>>India>>shortName>>>>>>IN>>types>>>>>>>>[country, political]
]

Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41057
    
  43
Please edit your post so that the code tags are properly applied: https://www.coderanch.com/how-to/java/UseCodeTags. As it is, the code is pretty much unreadable (and, IMO; way too long - I doubt anyone wants to try to understand all that).
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41057
    
  43
No, don't post your code again, EDIT your existing post:

And you need to use CODE tags, not QUOTE tags.
 
 
subject: Google MAp Api Inside Java Classes
 
Similar Threads
configuring global exception page
Google driving directions in java application
Generalize instantiation of a data class
Eclipse not create a location(string) object. Why?
Pick Google Maps location from Google Maps and store this location in database