8/3/2019 Google Map Service Class
1/28
Bo co thc tp nhn thc GVHD : Trnh Cng
Duy
SVTH : Tr Page 1
A. PHN I :Nu cm nhn ca bn than v vic c tham quan, tip cn vi mi trng
doanh nghip, c th l cng ty Enclave.
I. Gii thiu v cng ty Enclave :
Cng ty Cng ngh phn mm Enclave l mttrong nhng cng ty gia cng phn mm 100% vn nc ngoi ng trn a bn
Nng (27 Hunh Thc Khng- Nng).Cng ty hot ng hon ton vi cng vic giacng phn mm cho khch hng.
Cng ty Cng ngh phn mm Enclave l mt trong nhng cng ty gia cng phnmm 100% vn nc ngoi ng trn a bn Nng (27 Hunh Thc Khng-
Nng).Cng ty hot ng hon ton vi cng vic gia cng phn mm cho khch hng.
Cng ty thng dnh Cc mng hp tc, h tr cho sinh vin:- T chc cc bui seminar, workshop cho sinh vin v ging vin khoa o to Quct v CNTT- Trao hc bng hng nm.- Tuyn dng sinh vin thc tp v tuyn dng vic lm.- Gp trin khai chng trnh o to v cc hot ng lin quan khc.
II. Cm nhn ca bn thn :Khi tham quan mi trng doanh nghip, tht s em nhn ra c nhiu iu.u tin em nhn thy cng ty Enclave l mt cng ty vi i ng nhn vin tr,nng
ng v ti nng.- Cng ty Enclave c t chc kh cht ch ,chia thnh nhiu phng vi cc chc
nng, nhim v khc nhau .Mi phng thc hin cc d n khc nhau.- Lnh o cng ty l nhng ngi c kinh nghim,ti nng nhng khng thiu snhit tnh trong cng vic.
- i ng nhn vin tr,y ti nng ,nhit tnh c th m ng v hon thnh ttcng vic.c bit l h c nhng k nng mm tt : lm vic nhm,ngoi ng..
- Mi trng lm vic thn thin vi nn tng ngoai ng tt.
8/3/2019 Google Map Service Class
2/28
Bo co thc tp nhn thc GVHD : Trnh Cng
Duy
SVTH : Tr Page 2
Mi trng lm vic ti cng ty tht khc so vi s tng tng ca em.Mt mi trnglm vic m khng c vn ngoai ng tt th kh c th ha nhp tt.
Sau khi c tip cn v trao i vi anh ch l nhn vin ca cng ty,em hiu c
rng lm tt cng vic th mnh khng ch hiu su mt ngn ng lp trnh,mt vn m phi hiu nhiu iu hn nh ngoi ng , kh nng lm nhm,...c bit phi c sthch ng nhanh vi mi trng.
B. PHN II :I. M t ti :
ti 39 :Nghin cu gi cng ngh Google Map API xy dng ng dng bn trc tuyn s dng ngn ng JAVA.
II. Cs l thuyt :1. Gii thiu :
Google Maps (thi gian trc cn gi l Google Local) l mt dch v ng dng v cngngh bn trc tuyn trn web min ph c cung cp bi Google v h tr nhiu dchv da vo bn nh Google Ride Finder v mt s c th dng nhng vo cc trangweb ca bn th ba thng qua Google Maps API. N cho php thy bn ng s,ng i cho xe p, cho ngi i b (nhng ng i ngn hn 6.2 dm) v xe hi, v
nhng a im kinh doanh trong khu vc cng nh khp ni trn th gii.
III. Cng ngh :1. Google map API l g?
The Google Maps API cung cp cc dch v web nh 1 giao din yu cu d liuMaps API t 1 dch v bn ngoi(dch v ca Google) v s dng n bn trong nhngng dng bn ca chng ta(cc trang web). Cc dch v ny c thit k s dngtrong s lin kt vi bn .
Cc dch v web ny s dng giao thc HTTP yu cu n cc URL c th, truyn cctham s URL nh l truyn cc i s n dch v. Ni chung cc dch v ny tr v dliu trong yu cu HTTP nh JSON hay XML truyn v/hay x l bi ng dng cachng ta.
Ex : 1 kiu yu cu dch v web ni chung c cu trc nh sau:
8/3/2019 Google Map Service Class
3/28
Bo co thc tp nhn thc GVHD : Trnh Cng
Duy
SVTH : Tr Page 3
http://maps.googleapis.com/maps/api/service/output?parameters
Trong :Service : Ch nh c th dch v c yu cu.Output : Ch nh kiu nh dng tr v (XML hay JSON)
2. Truy cp SSL:Bn khng nhng c th truy cp dch v web Google API thng qua giao thc
HTTPS. Ta c th thay i giao thc trong yu cu URL ca bn thnh https nh sau:
https://maps.googleapis.com/maps/api/service/output?parameters
HTTPS c ch cho ng dng rng n cha d liu dng nhy cm ca ngi dng, nhv tr ngi dng trong yu cu.
S dng Google Map API by gi yu cu rng bn ch nh ng dng ca bn c sdng 1 cm bin (nh nh v GPS) xc nh v tr ca ngi dng trn bt k th vinMap API no hay cc yu cu dch v. y l iu c bit quan trng vi thit b ding. Nu ng dng Google Map API ca bn s dng bt k dng no ca cm bin xc nh v tr ca thit b ang truy cp ng dng ca bn. bn phi khai bo n vi 1 gitr true vi tham s ca cm bin.Ch rng thm ch nu ng dng ca bn khng sdng 1 cm bin nh v, bn vn phi thit lp tham s sensor.
3. Xy dng URL hp l :Chng ta cn dch cc k t c bit bi v tt c cc URL cn ph hp vi cu trc
ch nh bi c t W3 Uniform Resource Identifier. N c ngha l cc URL ch ccha tp con c bit ca cc k t ASCII, cc biu tng va c ch va c s quenthuc v mt s k t d tr cho dng nh cc k t iu khin bn trong cc URL.Bng sau tng hp cc k t :
Set Characters URL usageAlphanumeric a b c d e f g h i j k l m n o p q r s t u v w x y z A
B C D E F G H I J K L M N O P Q R S T U VW X Y Z 0 1 2 3 4 5 6 7 8 9
Text strings, scheme
usage (http), port (8080),etc.Unreserved - _ . ~ Text stringsReserved ! * ' ( ) ; : @ & = + $ , / ? % # [ ] Control characters and/or
Text Strings
Tt c cc k t c m ha URL c m ha s 1 k t '%'v 2 k t gi tr thp lcphn tng ng vi cc k t UTF-8 . V d, + trong UTF-8 c chuyn m
8/3/2019 Google Map Service Class
4/28
Bo co thc tp nhn thc GVHD : Trnh Cng
Duy
SVTH : Tr Page 4
thnh %E4%B8%8A%E6%B5%B7%2B%E4%B8%AD%E5%9C%8B . Chui ? and the Mysterianschuyn thnh %3F+and+the+Mysterians.
4. S xc thc u tin Map API :Yu cu n cc dch v web Map API bi ng dng by gi yu cu ch k in t s
dng 1 kha c m ha m Google cung cp cho bn cho mc ch . Ch k x lkt hp 1 URL v kha ch k vi nhau s dng 1 thut ton m ha,to 1 ch k duynht. Ch k duy nht ny cho php sch v web ca chng ta xc thc rng bt c trangno sinh ra yu cu m s dng ID khch Map API ca bn c xc thc lm iu. Ch k khng nhng duy nht cho mi URL, m bo rng cc yu cu s dng IDkhch khng th c chnh sa m khng yu cu 1 ch k mi c to ra.
5. X l phn hi :Khi nh dng chnh xc ca cu tr li ring l vi mt yu cu dch v web l khngc bo m (mt s yu t c th b mt hay nhiu a im), bn khng nn gi nhrng cc nh dng c tr v cho bt k phn ng nht nh s l tng t cho cc truyvn khc nhau. Thay vo , bn nn x l cc phn ng v chn gi tr thch hp thngqua cc biu thc. Phn ny bn v lm th no trch xut cc gi tr t ng ng phvi dch v web.
Cc dch v Web ca Google cung cp cu tr li l d hiu, nhng khng hon ton thnthin ngi dng. Khi thc hin mt truy vn, thay v hin th mt tp hp cc d liu,
bn c th mun trch xut mt vi gi tr c th. Ni chung, bn s mun phn tch phnhi t cc dch v web v gii nn ch nhng gi tr m bn quan tm.
n phn tch c php m bn s dng ph thuc vo liu bn c tr v u ra l XMLhoc JSON. Phc p JSON, l c trong hnh thc ca cc i tng Javascript, c thc x l trong vng Javascript chnh n trn my khch, phc p XML phi c x
l bng cch s dng mt b x l XML v mt ngn ng truy vn XML gii quytcc yu t trong nh dng XML. Chng ti s dng XPath trong cc v d sau y, v nthng c h tr trong cc th vin x l XML.
IV. Cch xy dng chng trnh :
8/3/2019 Google Map Service Class
5/28
Bo co thc tp nhn thc GVHD : Trnh Cng
Duy
SVTH : Tr Page 5
1. nh hng :- Trc tin ta phi xy dng 2 gi h tr cho ng dng l :
+ Gi org.netbeans.saas : cha cc lp lm nhim v nh ngha cc kt nivi GooglemapServer trong lpRestConnection v nh ngha cch x l cc phc pt GooglemapServer trong lpRestResponse.
+ Gi org.netbeans.saas.google: cha lp GoogleMapService thc hinkt ni ti GooglemapServer, gi yu cu v nhn phc p t GooglemapServer.V 1
propertygooglemapservice cha API_KEY do Google cung cp.
- Tip theo ta to 1 file JSP hin th d liu do Google map API phc p lntrang web trc tuyn.
2. Thc thi :a. Xy dng gi org.netbeans.saas :+ To lpRestConnection:
package org.netbeans.saas;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.Authenticator;
import java.net.HttpURLConnection;
8/3/2019 Google Map Service Class
6/28
Bo co thc tp nhn thc GVHD : Trnh Cng
Duy
SVTH : Tr Page 6
import java.net.URL;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
public class RestConnection {
static {
//set the identification of the client
System.setProperty("http.agent", System.getProperty("user.name") + " (from
NetBeans IDE)");
}
private HttpURLConnection conn;
private String date;
/** Creates a new instance of RestConnection */
public RestConnection(String baseUrl) {
this(baseUrl, null, null);
}
/** Creates a new instance of RestConnection */
public RestConnection(String baseUrl, String[][] params) {
this(baseUrl, null, params);
}
/** Creates a new instance of RestConnection */
8/3/2019 Google Map Service Class
7/28
Bo co thc tp nhn thc GVHD : Trnh Cng
Duy
SVTH : Tr Page 7
public RestConnection(String baseUrl, String[][] pathParams, String[][] params) {
try {
String urlStr = baseUrl;
if (pathParams != null && pathParams.length > 0) {
urlStr = replaceTemplateParameters(baseUrl, pathParams);
}
URL url = new URL(encodeUrl(urlStr, params));
conn = (HttpURLConnection) url.openConnection();
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setUseCaches(false);
conn.setDefaultUseCaches(false);
conn.setAllowUserInteraction(true);
SimpleDateFormat format = new SimpleDateFormat("EEE, dd MMM yyyy
HH:mm:ss z");
date = format.format(new Date());
conn.setRequestProperty("Date", date);
} catch (Exception ex) {
Logger.getLogger(RestConnection.class.getName()).log(Level.SEVERE, null, ex);
}
}
public void setAuthenticator(Authenticator authenticator) {
Authenticator.setDefault(authenticator);
}
8/3/2019 Google Map Service Class
8/28
Bo co thc tp nhn thc GVHD : Trnh Cng
Duy
SVTH : Tr Page 8
public String getDate() {
return date;
}
public RestResponse get() throws IOException {
return get(null);
}
public RestResponse get(String[][] headers) throws IOException {
conn.setRequestMethod("GET");
return connect(headers, null);
}
public RestResponse head() throws IOException {
return get(null);
}
public RestResponse head(String[][] headers) throws IOException {
conn.setRequestMethod("HEAD");
return connect(headers, null);
}
public RestResponse put(String[][] headers) throws IOException {
return put(headers, (InputStream) null);
}
public RestResponse put(String[][] headers, String data) throws IOException {
InputStream is = null;
if(data != null)
8/3/2019 Google Map Service Class
9/28
Bo co thc tp nhn thc GVHD : Trnh Cng
Duy
SVTH : Tr Page 9
is = new ByteArrayInputStream(data.getBytes("UTF-8"));
return put(headers, is);
}
public RestResponse put(String[][] headers, InputStream is) throws IOException {
conn.setRequestMethod("PUT");
return connect(headers, is);
}
public RestResponse post(String[][] headers) throws IOException {
return post(headers, (InputStream) null);
}
public RestResponse post(String[][] headers, String data) throws IOException {
InputStream is = null;
if(data != null)
is = new ByteArrayInputStream(data.getBytes("UTF-8"));
return post(headers, is);
}
public RestResponse post(String[][] headers, InputStream is) throws IOException {
conn.setRequestMethod("POST");
return connect(headers, is);
}
public RestResponse post(String[][] headers, String[][] params) throws IOException {
conn.setRequestMethod("POST");
conn.setRequestProperty("ContentType", "application/x-www-form-urlencoded");
8/3/2019 Google Map Service Class
10/28
Bo co thc tp nhn thc GVHD : Trnh Cng
Duy
SVTH : Tr Page 10
String data = encodeParams(params);
return connect(headers, new ByteArrayInputStream(data.getBytes("UTF-8")));
}
public RestResponse delete(String[][] headers) throws IOException {
conn.setRequestMethod("DELETE");
return connect(headers, null);
private RestResponse connect(String[][] headers,
InputStream data) throws IOException {
try {
// Send data
setHeaders(headers);
String method = conn.getRequestMethod();
byte[] buffer = new byte[1024];
int count = 0;
if (method.equals("PUT") || method.equals("POST")) {
if (data != null) {
conn.setDoOutput(true);
OutputStream os = conn.getOutputStream();
while ((count = data.read(buffer)) != -1) {
8/3/2019 Google Map Service Class
11/28
Bo co thc tp nhn thc GVHD : Trnh Cng
Duy
SVTH : Tr Page 11
os.write(buffer, 0, count);
}
os.flush();
}
}
RestResponse response = new RestResponse();
InputStream is = conn.getInputStream();
while ((count = is.read(buffer)) != -1) {
response.write(buffer, 0, count);
}
response.setResponseCode(conn.getResponseCode());
response.setResponseMessage(conn.getResponseMessage());
response.setContentType(conn.getContentType());
response.setContentEncoding(conn.getContentEncoding());
response.setLastModified(conn.getLastModified());
return response;
} catch (Exception e) {
String errMsg = "Cannot connect to :" + conn.getURL();
try {
BufferedReader rd = new BufferedReader(new
InputStreamReader(conn.getErrorStream()));
String line;
StringBuffer buf = new StringBuffer();
8/3/2019 Google Map Service Class
12/28
Bo co thc tp nhn thc GVHD : Trnh Cng
Duy
SVTH : Tr Page 12
while ((line = rd.readLine()) != null) {
buf.append(line);
buf.append('\n');
}
errMsg = buf.toString();
} finally {
throw new IOException(errMsg);
}
}
}
private String replaceTemplateParameters(String baseUrl, String[][] pathParams) {
String url = baseUrl;
if (pathParams != null) {
for (int i = 0; i < pathParams.length; i++) {
String key = pathParams[i][0];
String value = pathParams[i][1];
if (value == null) {
value = "";
}
url = url.replace(key, value);
}
}
return url;
8/3/2019 Google Map Service Class
13/28
Bo co thc tp nhn thc GVHD : Trnh Cng
Duy
SVTH : Tr Page 13
}
private String encodeUrl(String baseUrl, String[][] params) {
String encodedParams = encodeParams(params);
if (encodedParams.length() > 0) {
encodedParams = "?"+ encodedParams;
}
return baseUrl + encodedParams;
}
private String encodeParams(String[][] params) {
String p = "";
if (params != null) {
for (int i = 0; i < params.length; i++) {
String key = params[i][0];
String value = params[i][1];
if (value != null) {
try {
p += key + "=" + URLEncoder.encode(value, "UTF-8") + "&";
} catch (UnsupportedEncodingException ex) {
Logger.getLogger(RestConnection.class.getName()).log(Level.SEVERE, null,
ex);
}
}
8/3/2019 Google Map Service Class
14/28
Bo co thc tp nhn thc GVHD : Trnh Cng
Duy
SVTH : Tr Page 14
}
if (p.length() > 0) {
p = p.substring(0, p.length() - 1);
}
}
return p;
}
private void setHeaders(String[][] headers) {
if (headers != null) {
for (int i = 0; i < headers.length; i++) {
conn.setRequestProperty(headers[i][0], headers[i][1]);
}
}
}
}
+ To lpRestResponse:
package org.netbeans.saas;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
8/3/2019 Google Map Service Class
15/28
Bo co thc tp nhn thc GVHD : Trnh Cng
Duy
SVTH : Tr Page 15
import java.io.StringReader;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import javax.xml.transform.stream.StreamSource;
public class RestResponse {
private ByteArrayOutputStream os;
private String contentType = "text/plain";
private String contentEncoding;
private int responseCode;
private String responseMsg;
private long lastModified;
public RestResponse() {
os = new ByteArrayOutputStream();
}
public RestResponse(byte[] bytes) throws IOException {
this();
byte[] buffer = new byte[1024];
8/3/2019 Google Map Service Class
16/28
Bo co thc tp nhn thc GVHD : Trnh Cng
Duy
SVTH : Tr Page 16
int count = 0;
ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
while ((count = bis.read(buffer)) != -1) {
write(buffer, 0, count);
}
}
public void setContentType(String contentType) {
this.contentType = contentType;
}
public String getContentType() {
return contentType;
}
public void setContentEncoding(String contentEncoding) {
this.contentEncoding = contentEncoding;
}
public void setResponseMessage(String msg) {
this.responseMsg = msg;
}
public String getResponseMessage() {
return responseMsg;
}
public void setResponseCode(int code) {
this.responseCode = code;
8/3/2019 Google Map Service Class
17/28
Bo co thc tp nhn thc GVHD : Trnh Cng
Duy
SVTH : Tr Page 17
}
public int getResponseCode() {
return responseCode;
}
public void setLastModified(long lastModified) {
this.lastModified = lastModified;
}
public long getLastModified() {
return lastModified;
}
public void write(byte[] bytes, int start, int length) {
os.write(bytes, start, length);
}
public byte[] getDataAsByteArray() {
return os.toByteArray();
}
public String getDataAsString() {
try {
return os.toString("UTF-8");
} catch (Exception ex) {
Logger.getLogger(RestConnection.class.getName()).log(Level.SEVERE, null, ex);
}
return null;
8/3/2019 Google Map Service Class
18/28
Bo co thc tp nhn thc GVHD : Trnh Cng
Duy
SVTH : Tr Page 18
}
public OutputStream getOutputStream() {
return os;
}
public T getDataAsObject(Class jaxbClass) throws JAXBException {
return getDataAsObject(jaxbClass, jaxbClass.getPackage().getName());
}
public T getDataAsObject(Class clazz, String packageName) throws
JAXBException {
JAXBContext jc = JAXBContext.newInstance(packageName);
Unmarshaller u = jc.createUnmarshaller();
Object obj = u.unmarshal(new StreamSource(new StringReader(getDataAsString())));
if (obj instanceof JAXBElement) {
return (T) ((JAXBElement) obj).getValue();
} else {
return (T) obj;
}
}
}
b. Xy dng gi org.netbeans.saas.google:+ To lpGoogleMapService:
package org.netbeans.saas.google;
8/3/2019 Google Map Service Class
19/28
Bo co thc tp nhn thc GVHD : Trnh Cng
Duy
SVTH : Tr Page 19
import java.io.IOException;
import java.net.URLEncoder;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.netbeans.saas.RestConnection;
import org.netbeans.saas.RestResponse;
public class GoogleMapService {
private static String apiKey;
private static final String PROP_FILE =
GoogleMapService.class.getSimpleName().toLowerCase() + ".properties";
static {
try {
Properties props = new Properties();
props.load(GoogleMapService.class.getResourceAsStream(PROP_FILE));
apiKey = props.getProperty("api_key");
} catch (IOException ex) {
Logger.getLogger(GoogleMapService.class.getName()).log(Level.SEVERE, null, ex);
}
}
public static String getApiKey() throws IOException {
if (apiKey == null || apiKey.length() == 0) {
8/3/2019 Google Map Service Class
20/28
Bo co thc tp nhn thc GVHD : Trnh Cng
Duy
SVTH : Tr Page 20
throw new IOException("Please specify your api key in the " + PROP_FILE + "
file.");
}
return apiKey;
}
public static RestResponse getGoogleMap(String address, Integer zoom, String iframe)
throws IOException {
try {
String key = getApiKey();
String encoded = URLEncoder.encode(address, "UTF-8");
GeoCoder coder = new GeoCoder(encoded, apiKey);
GeoCode code = coder.invoke();
String mapRep = "";
if(iframe != null && iframe.trim().equalsIgnoreCase("true")) {
mapRep +=
" \n"+
getMapScript(address, zoom, code.getLatitude(), code.getLongitude(), key)+
" \n"+
" loadScript();\n"+
" \n";
} else {
mapRep =
"\n"+
8/3/2019 Google Map Service Class
21/28
Bo co thc tp nhn thc GVHD : Trnh Cng
Duy
SVTH : Tr Page 21
"\n"+
" \n"+
" \n"+
" Google Maps JavaScript API Example\n"+
getMapScript(address, zoom, code.getLatitude(), code.getLongitude(), key)+
" \n"+
" \n"+
" \n"+
" \n"+
"";
}
return new RestResponse(mapRep.getBytes());
} catch (Exception ex) {
throw new IOException(ex.getMessage());
}
}
public static String getMapScript(String address, Integer zoom,
double latitude, double longitude, String key) {
String mapRep =
" \n"+
" //
8/3/2019 Google Map Service Class
22/28
Bo co thc tp nhn thc GVHD : Trnh Cng
Duy
SVTH : Tr Page 22
" if (GBrowserIsCompatible()) {\n"+
" var map = new GMap2(document.getElementById('map'));\n"+
" var point = new GLatLng("+latitude+", "+longitude+");\n"+
" map.addControl(new GSmallMapControl());\n"+
" map.addControl(new GMapTypeControl());\n"+
" map.setCenter(point, " + zoom + ");\n"+
" var marker = createMarker(point);\n"+
" map.addOverlay(marker);\n"+
" marker.openInfoWindowHtml(\"" + address + "\");\n"+
" }\n"+
" }\n"+
" function createMarker(point) {\n" +
" var marker = new GMarker(point);\n"+
" GEvent.addListener(marker, \"click\", function() {\n"+
" marker.openInfoWindowHtml(\"" + address + "\");\n"+
" });\n"+
" return marker;\n"+
" }\n"+
" function loadScript() {\n"+
" var script = document.createElement(\"script\");\n"+
" script.setAttribute(\"src\",
\"http://maps.google.com/maps?file=api&v=2.x&key="+key+"&async=2&callback=loadM
ap\");\n"+
" script.setAttribute(\"type\", \"text/javascript\");\n"+
8/3/2019 Google Map Service Class
23/28
Bo co thc tp nhn thc GVHD : Trnh Cng
Duy
SVTH : Tr Page 23
" document.documentElement.firstChild.appendChild(script);\n"+
" }\n"+
" //]]>\n"+
" \n";
return mapRep;
}
public static class GeoCoder {
public final static String GEOCODE_URL = "http://maps.google.com/maps/geo";
private String location;
private String key;
/** Creates a new instance of GeoCoder */
public GeoCoder(String location, String key) {
this.location = location;
this.key = key;
}
public GeoCode invoke() throws IOException {
String[][] params = new String[][] {
{"q", location},
{"output", "xml"},
{"key", key}
};
RestConnection cl = new RestConnection(GEOCODE_URL, params);
8/3/2019 Google Map Service Class
24/28
Bo co thc tp nhn thc GVHD : Trnh Cng
Duy
SVTH : Tr Page 24
RestResponse response = cl.get();
String codeStr = response.getDataAsString();
return new GeoCode(codeStr);
}
}
public static class GeoCode {
private double longitude ;
private double latitude;
/** Creates a new instance of GeoCode */
public GeoCode(String xmlStr) {
int ts = xmlStr.indexOf("");
int te = xmlStr.indexOf("");
String codeStr = "";
if(ts != -1 && te != -1)
codeStr = xmlStr.substring(ts+13, te);
String[] codes = codeStr.split(",");
if(codes.length>1) {
this.longitude = Double.parseDouble(codes[0]);
this.latitude = Double.parseDouble(codes[1]);
}
}
public double getLongitude() {
8/3/2019 Google Map Service Class
25/28
Bo co thc tp nhn thc GVHD : Trnh Cng
Duy
SVTH : Tr Page 25
return this.longitude;
}
public double getLatitude() {
return this.latitude;
}
}
}
+ To propertygooglemapservice:
c. To file index.jsp :
View the Googlemap Demo
Google map API service!
Address:
8/3/2019 Google Map Service Class
26/28
Bo co thc tp nhn thc GVHD : Trnh Cng
Duy
SVTH : Tr Page 26
V. ng dng minh ha :
8/3/2019 Google Map Service Class
27/28
Bo co thc tp nhn thc GVHD : Trnh Cng
Duy
SVTH : Tr Page 27
8/3/2019 Google Map Service Class
28/28
Bo co thc tp nhn thc GVHD : Trnh Cng
Duy
VI.
Ph lc tham kho :Googlemapservice class:
http://code.google.com/p/tripnetworkingsite/source/browse/trunk/AwesomeTrips/src/java/org/netbeans/saas/google/GoogleMapService.java?r=39
RestConnection class:
http://49ers.es/welcomehome/javadoc/org/netbeans/saas/RestConnection.html
http://code.google.com/p/personalcloudsyncfr/source/browse/DocumentWatcher/src/org/netbeans/saas/RestConnection.java?r=295
RestResponse class:http://49ers.es/welcomehome/javadoc/org/netbeans/saas/RestResponse.html
http://code.google.com/p/tripnetworkingsite/source/browse/trunk/AwesomeTrips/src/java/org/netbeans/saas/RestResponse.java?r=39
api_key: ABQIAAAAT-3oDobl9bEVE3K2BOBXEBQg2HBMA_uLzZJtX4XkuiGzyMfk8BSm5ir6AShkWVFZIr21ZfDsiTR6Pg