Here's a quick PoC (that's Proof of Concept, folks, not Piece of... never mind...) function module that takes a Postcode and turns it into LatLngs that can be used for dropping pins on your Google Maps mashup, or just used to work out what's nearby and what's not.
I also tested it with everyone's favourite American Zip Code (90210) and it returned Beverly Hills, so I'm guessing whatever your location, Google is clever enough to know where you are.
The HTTP class could be used for all sorts of cool "get something from the internet" applications. Again, I know there are better ways of doing this, but since what I'm bringing back is so simple (Lat field, Lng field...) I'm happy to have the minor overhead of the string operations in this case.
FUNCTION YPD_GOOGLE_GEO .
*"----------------------------------------------------------------------
*"*"Local Interface:
*" IMPORTING
*" REFERENCE(I_PSTLZ) TYPE PSTLZ DEFAULT 'NE1 5JB'
*" EXPORTING
*" REFERENCE(O_LAT) TYPE CHAR20
*" REFERENCE(O_LNG) TYPE CHAR20
*"----------------------------------------------------------------------
"http://maps.googleapis.com/maps/api/geocode/xml?address=
data: url_stem type string value 'http://maps.googleapis.com/maps/api/geocode/xml?address='.
data: url type string.
data: client type REF TO IF_HTTP_CLIENT.
data: ts_dados_temp type table of string.
data: l_dados_temp type string.
DATA: p_content TYPE string.
data: l_counter type int4.
data: l_trash type string.
data: l_keep type string.
data: l_pstlz type char10.
concatenate url_stem i_pstlz into url.
clear p_content.
CALL METHOD cl_http_client=>create_by_url "Create a client object with which to play.
EXPORTING "based on the URL.
url = url
IMPORTING
client = client
EXCEPTIONS
OTHERS = 1.
client->request->set_header_field( name = '~request_method' value = 'GET' ). "Set the Request type to GET
client->send( ). "Make the call
****Receive the Response Object
CALL METHOD client->receive
EXCEPTIONS
http_communication_failure = 1
http_invalid_state = 2
http_processing_failed = 3
OTHERS = 4.
p_content = client->response->get_cdata( ).
SPLIT p_content AT '<lat>' into l_trash l_keep.
split l_keep at '</lat>' into o_lat l_keep.
SPLIT l_keep AT '<lng>' into l_trash l_keep.
SPLIT l_keep AT '</lng>' into o_lng l_trash.
CALL METHOD client->close( ). " prevents 'No Memory for processing HTTP, HTTPS or SMTP queries'
ENDFUNCTION.
No comments:
Post a Comment