Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

How to get latitude & longitude from Google map or itouch map into ABAP ?

Former Member
0 Kudos

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

1 ACCEPTED SOLUTION

Former Member
0 Kudos

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
*&---------------------------------------------------------------------*
DATAhttp_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 .

6 REPLIES 6

naimesh_patel
Active Contributor
0 Kudos

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 &amp;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

0 Kudos

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.

0 Kudos

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:


url = `http://maps.googleapis.com/maps/api/geocode/xml?address=3373+US+HIGHWAY+1,+STE+54A,+LAWRENCEVILLE,+0...`.

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

Former Member
0 Kudos

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
*&---------------------------------------------------------------------*
DATAhttp_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 .

0 Kudos

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.

<GeocodeResponse>

<status>OK</status>

<result>

<type>locality</type>

<type>political</type>

<formatted_address>Bengaluru, Karnataka, India</formatted_address>

<address_component>

<long_name>Bengaluru</long_name>

<short_name>Bengaluru</short_name>

<type>locality</type>

<type>political</type>

</address_component>

<address_component>

<long_name>Bangalore Urban</long_name>

<short_name>Bangalore Urban</short_name>

<type>administrative_area_level_2</type>

<type>political</type>

</address_component>

<address_component>

<long_name>Karnataka</long_name>

<short_name>KA</short_name>

<type>administrative_area_level_1</type>

<type>political</type>

</address_component>

<address_component>

<long_name>India</long_name>

<short_name>IN</short_name>

<type>country</type>

<type>political</type>

</address_component>

<geometry>

<location>

<lat>12.9715987</lat>

<lng>77.5945627</lng>

</location>

<location_type>APPROXIMATE</location_type>

<viewport>

<southwest>

<lat>12.7342888</lat>

<lng>77.3791981</lng>

</southwest>

<northeast>

<lat>13.1737060</lat>

<lng>77.8826809</lng>

</northeast>

</viewport>

<bounds>

<southwest>

<lat>12.7342888</lat>

<lng>77.3791981</lng>

</southwest>

<northeast>

<lat>13.1737060</lat>

<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

Former Member
0 Kudos

This message was moderated.