05-19-2015 9:31 PM
HI Expert,
I need your help on one development,
i want to develop a program in SAP ABAP to get latitude & longitude of a particular place,
I have a street name, city, pincode & country which i pass it to http://itouchmap.com/latlong.html
and get Latitude & longitude of the User but I am performing this activity manually and i want to do it through ABAP program.
i need a way to fetch Lat Long from web link & show as a output of the ABAP report.
Please check the attcahed file which contain manual process of my current activity.
Thanks & Regards,
Subhanshu Shrivastava
05-20-2015 7:54 PM
Hi Subhanshu,
Getting Latitude and Longitude is possible using the website
http://maps.google.com/maps/api/geocode/xml?address=jeddah
This result is returned in xml format.
You have to parse the xml and take out Latitude and Longitude.
You can check the below code which is written by using the source
------SAP and ABAP Tutorials: Simple Code to consume Web service using SAP ABAP
*&---------------------------------------------------------------------*
*& Selection Screen
*&---------------------------------------------------------------------*
PARAMETERS : p_cnt TYPE t005t-landx .
*&---------------------------------------------------------------------*
*& Types and Data
*&---------------------------------------------------------------------*
DATA: http_client TYPE REF TO if_http_client ,
http_url TYPE string ,
p_content TYPE string .
*&---------------------------------------------------------------------*
*& Start of Selection
*&---------------------------------------------------------------------*
START-OF-SELECTION .
CONCATENATE 'http://maps.google.com/maps/api/geocode/xml?address='
p_cnt
INTO http_url .
* Creation of new IF_HTTP_Client object
CALL METHOD cl_http_client=>create_by_url
EXPORTING
url = http_url
IMPORTING
client = http_client
EXCEPTIONS
argument_not_found = 1
plugin_not_active = 2
internal_error = 3
OTHERS = 4.
http_client->request->set_header_field( name = '~request_method' value = 'GET' ).
* Send the request
http_client->send( ).
* Reterive the result
CALL METHOD http_client->receive
EXCEPTIONS
http_communication_failure = 1
http_invalid_state = 2
http_processing_failed = 3
OTHERS = 4.
p_content = http_client->response->get_cdata( ).
*&---------------------------------------------------------------------*
*& Processing string
*&---------------------------------------------------------------------*
DATA : moff TYPE syst-tabix ,
moff1 TYPE syst-tabix ,
len TYPE syst-tabix,
lat TYPE C LENGTH 20,
lng TYPE C LENGTH 20.
DO .
FIND '<location>' IN SECTION OFFSET moff OF p_content IGNORING CASE MATCH OFFSET moff .
IF sy-subrc = 0 .
moff = moff + 10 .
FIND '</location>' IN SECTION OFFSET moff OF p_content IGNORING CASE MATCH OFFSET moff1 .
len = moff1 - moff .
* ---------------find latitude
FIND '<lat>' IN SECTION OFFSET moff OF p_content IGNORING CASE MATCH OFFSET moff .
IF sy-subrc = 0 .
moff = moff + 5 .
FIND '</lat>' IN SECTION OFFSET moff OF p_content IGNORING CASE MATCH OFFSET moff1 .
len = moff1 - moff .
lat = p_content+moff(len) .
WRITE : / 'Longitude', lat.
ENDIF.
* ---------------find longitude
FIND '<lng>' IN SECTION OFFSET moff OF p_content IGNORING CASE MATCH OFFSET moff .
IF sy-subrc = 0 .
moff = moff + 5 .
FIND '</lng>' IN SECTION OFFSET moff OF p_content IGNORING CASE MATCH OFFSET moff1 .
len = moff1 - moff .
lng = p_content+moff(len) .
WRITE : / 'Latitude', lng.
ENDIF.
ELSE.
EXIT.
ENDIF.
ENDDO .
05-19-2015 10:00 PM
You can call the Google Maps API for the Geocoding. You would need the API key from google. If it is for a client, you should get the client account for more usages. Once you have the key, you can try to do the HTTP call.
Refer for Geo coding call:
The Google Geocoding API - Google Maps Geocoding API &mdash; Google Developers
In ABAP, make the HTTP call using the CL_HTTP_CLIENT. There are quite a few examples around the web for this, but you can reference
for HTTP calls.
CALL METHOD cl_http_client=>create_by_url
EXPORTING
url = url
IMPORTING
client = http_client
EXCEPTIONS
argument_not_found = 1
plugin_not_active = 2
internal_error = 3
OTHERS = 4.
IF sy-subrc = 0.
http_client->send( ).
http_client->receive( ).
content = http_client->response->get_data( ).
http_client->close( ).
ENDIF.
Pass the URL like this.
https://maps.googleapis.com/maps/api/geocode/xml?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA&key=API_KEY
Once you receive the content back, parse the content to get the desired values.
Regards,
Naimesh Patel
05-19-2015 10:18 PM
Hi ,
I have tried this but in content = http_client->response->get_data( ). i am not getting Latitude & longitude.
I have passed 'http://maps.google.com/maps/api/geocode/xml?address=3373 US HIGHWAY 1 STE 54A LAWRENCEVILLE 08648-2414 NJ&sensor=false' in URL.
but not getting Latitude & longitude.
05-20-2015 3:44 PM
You need to replace the space with + in the address. Also, there should be comma after street, city, zip code and state.
So something like this:
I tried to run on my system but it gave me REQUEST_DENIED return code with message "Requests to this API must be over SSL.". So you need to send the request over HTTPS. Make sure your system is setup correctly to handle the HTTPS response.
Regards,
Naimesh Patel
05-20-2015 7:54 PM
Hi Subhanshu,
Getting Latitude and Longitude is possible using the website
http://maps.google.com/maps/api/geocode/xml?address=jeddah
This result is returned in xml format.
You have to parse the xml and take out Latitude and Longitude.
You can check the below code which is written by using the source
------SAP and ABAP Tutorials: Simple Code to consume Web service using SAP ABAP
*&---------------------------------------------------------------------*
*& Selection Screen
*&---------------------------------------------------------------------*
PARAMETERS : p_cnt TYPE t005t-landx .
*&---------------------------------------------------------------------*
*& Types and Data
*&---------------------------------------------------------------------*
DATA: http_client TYPE REF TO if_http_client ,
http_url TYPE string ,
p_content TYPE string .
*&---------------------------------------------------------------------*
*& Start of Selection
*&---------------------------------------------------------------------*
START-OF-SELECTION .
CONCATENATE 'http://maps.google.com/maps/api/geocode/xml?address='
p_cnt
INTO http_url .
* Creation of new IF_HTTP_Client object
CALL METHOD cl_http_client=>create_by_url
EXPORTING
url = http_url
IMPORTING
client = http_client
EXCEPTIONS
argument_not_found = 1
plugin_not_active = 2
internal_error = 3
OTHERS = 4.
http_client->request->set_header_field( name = '~request_method' value = 'GET' ).
* Send the request
http_client->send( ).
* Reterive the result
CALL METHOD http_client->receive
EXCEPTIONS
http_communication_failure = 1
http_invalid_state = 2
http_processing_failed = 3
OTHERS = 4.
p_content = http_client->response->get_cdata( ).
*&---------------------------------------------------------------------*
*& Processing string
*&---------------------------------------------------------------------*
DATA : moff TYPE syst-tabix ,
moff1 TYPE syst-tabix ,
len TYPE syst-tabix,
lat TYPE C LENGTH 20,
lng TYPE C LENGTH 20.
DO .
FIND '<location>' IN SECTION OFFSET moff OF p_content IGNORING CASE MATCH OFFSET moff .
IF sy-subrc = 0 .
moff = moff + 10 .
FIND '</location>' IN SECTION OFFSET moff OF p_content IGNORING CASE MATCH OFFSET moff1 .
len = moff1 - moff .
* ---------------find latitude
FIND '<lat>' IN SECTION OFFSET moff OF p_content IGNORING CASE MATCH OFFSET moff .
IF sy-subrc = 0 .
moff = moff + 5 .
FIND '</lat>' IN SECTION OFFSET moff OF p_content IGNORING CASE MATCH OFFSET moff1 .
len = moff1 - moff .
lat = p_content+moff(len) .
WRITE : / 'Longitude', lat.
ENDIF.
* ---------------find longitude
FIND '<lng>' IN SECTION OFFSET moff OF p_content IGNORING CASE MATCH OFFSET moff .
IF sy-subrc = 0 .
moff = moff + 5 .
FIND '</lng>' IN SECTION OFFSET moff OF p_content IGNORING CASE MATCH OFFSET moff1 .
len = moff1 - moff .
lng = p_content+moff(len) .
WRITE : / 'Latitude', lng.
ENDIF.
ELSE.
EXIT.
ENDIF.
ENDDO .
09-16-2015 11:03 AM
Hi Abdul,
I have tried your suggested code but not getting output.
I passed "http://maps.google.com/maps/api/geocode/xml?address=bangalore".
in http_url .
* Creation of new IF_HTTP_Client object
CALL METHOD cl_http_client=>create_by_url
EXPORTING
url = http_url
IMPORTING
client = http_client
EXCEPTIONS
argument_not_found = 1
plugin_not_active = 2
internal_error = 3
OTHERS = 4.
http_client->request->set_header_field( name = '~request_method' value = 'GET' ).
* Send the request
http_client->send( ).
* Reterive the result
CALL METHOD http_client->receive
EXCEPTIONS
http_communication_failure = 1
http_invalid_state = 2
http_processing_failed = 3
OTHERS = 4.
p_content = http_client->response->get_cdata( ).
But i got in p_content
"<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">##<html>##<head>##<meta content="text/html; charset=ISO-8859-1"## http-equiv="content-type">##<title>Proxy Warning</title>##<link rel="StyleSheet"## href="http://blocked-sites.cso.att.com/".
I have checked manually the same URL(http://maps.google.com/maps/api/geocode/xml?address=bangalore".) , it is working .
Please check the below XML.
<type>political</type>
<formatted_address>Bengaluru, Karnataka, India</formatted_address>
<short_name>Bengaluru</short_name>
<type>locality</type>
<type>political</type>
</address_component>
<short_name>Bangalore Urban</short_name>
<type>administrative_area_level_2</type>
<type>political</type>
</address_component>
<short_name>KA</short_name>
<type>administrative_area_level_1</type>
<type>political</type>
</address_component>
<short_name>IN</short_name>
<type>country</type>
<type>political</type>
</address_component>
<lng>77.5945627</lng>
</location>
<location_type>APPROXIMATE</location_type>
<lng>77.3791981</lng>
</southwest>
<lng>77.8826809</lng>
</northeast>
</viewport>
<lng>77.3791981</lng>
</southwest>
<lng>77.8826809</lng>
</northeast>
</bounds>
</geometry>
<place_id>ChIJbU60yXAWrjsR4E9-UejD3_g</place_id>
</result>
</GeocodeResponse>
Can you please suggest , exactly what i need to do?
Regards,
Subhanshu Shrivastava
05-22-2016 11:40 PM