Google Maps Service | Community
Skip to main content
robin_wadhwa
October 17, 2018
Solved

Google Maps Service

  • October 17, 2018
  • 15 replies
  • 6647 views

In java am trying to get Latitude and Longitude dynamically by passing the address.

GeoApiContext context = new GeoApiContext.Builder().apiKey("KEY") .build();

            String address = ""1600 Amphitheatre Parkway Mountain View, CA 94043";

            GeocodingResult[] results = GeocodingApi.geocode(context, address).await();

Added These two dependencies :

<dependency>

  <groupId>com.google.maps</groupId>

  <artifactId>google-maps-services</artifactId>

  <version>0.9.0</version>

</dependency>

<dependency>

  <groupId>org.slf4j</groupId>

  <artifactId>slf4j-simple</artifactId>

</dependency>

Note: There is no compilation error.

But AEM is not able to resolve these.

com.google.maps -- Cannot be resolved

com.google.maps.errors -- Cannot be resolved

com.google.maps.model -- Cannot be resolved

What all dependencies I need to add ?

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by smacdonald2008

I developed a small service with your Google MAP API code:

package com.adobe.aem.core;

import java.io.DataOutputStream;

import java.io.EOFException;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.ObjectInputStream;

import com.google.maps.DirectionsApi.RouteRestriction;

import com.google.maps.DistanceMatrixApi;

import com.google.maps.DistanceMatrixApiRequest;

import com.google.maps.GeoApiContext;

import com.google.maps.GeocodingApi;

import com.google.maps.errors.ApiException;

import com.google.maps.model.DistanceMatrix;

import com.google.maps.model.GeocodingResult;

import com.google.maps.model.LatLng;

import com.google.maps.model.TravelMode;

public class MapImpl {

public String getLat()

{

try{

GeoApiContext context = new GeoApiContext.Builder().apiKey("KEY") .build();

        String address = "1600 Amphitheatre Parkway Mountain View, CA 94043";

        GeocodingResult[] results = GeocodingApi.geocode(context, address).await();

}

catch(Exception e)

{

e.printStackTrace();

}

return "" ;

}

}

I added the dependencies you mentioned. Then built this OSGi bundle as a Maven 13 Archetype project. This now goes into ACTIVE state

The key was to wrap the Google MAP JAR into an OSGi bundle and deploy. Now my service that uses the GOOGLE MAP API can go into Active state. So we have 2 OSGi bundles here:

If you do not know how to use an Eclipse Plug-in project to wrap a JAR into an OSGi bundle - follow these instructions in this article (we wrap simple JSON JAR as an example):

Adobe Experience Manager Help | Submitting Adobe Experience Manager form data to Java Sling Servlets

15 replies

Peter_Puzanovs
Community Advisor
Community Advisor
October 17, 2018

Dear Rodin,

Please add following instruction to your apache felix plugin configuration:

<Embed-Dependency>google-maps-services;inline=true </Embed-Dependency>

to your pom.xml file.

Regards,

Peter

robin_wadhwa
October 17, 2018

Hi PuzanovsP

Thanks for the response.

After making changes as you suggested. I am getting -->

okhttp3 -- Cannot be resolved

okio -- Cannot be resolved

com.google.appengine.api.urlfetch -- Cannot be resolved

To solve these I added below dependencies but still its not getting resolved.

<!-- https://mvnrepository.com/artifact/com.google.appengine/appengine-tools-api -->

<dependency>

    <groupId>com.google.appengine</groupId>

    <artifactId>appengine-tools-api</artifactId>

    <version>1.7.0</version>

</dependency>

<!-- https://mvnrepository.com/artifact/com.squareup.okio/okio -->

<dependency>

    <groupId>com.squareup.okio</groupId>

    <artifactId>okio</artifactId>

    <version>1.14.0</version>

</dependency>

<!-- https://mvnrepository.com/artifact/com.squareup.okhttp3/okhttp -->

<dependency>

    <groupId>com.squareup.okhttp3</groupId>

    <artifactId>okhttp</artifactId>

    <version>3.11.0</version>

</dependency>

Peter_Puzanovs
Community Advisor
Community Advisor
October 17, 2018

OK,

Then you would need to work through the dependencies list and add dependencies that are relevant and needed for your code to run e.g.

<Embed-Dependency>appengine-tools-api,okio,okhttp,google-maps-services;inline=true </Embed-Dependency>

Note, there might be even more dependencies you would need to include. They will show up, once you include these ones.

Please add the one's that are needed for your code to run.

Documentation[1]

[1] Apache Felix - Apache Felix Maven Bundle Plugin (BND)

Regards,

Peter

smacdonald2008
Level 10
October 17, 2018

Another option is to get the JAR file from Maven - Maven Repository: com.google.maps » google-maps-services » 0.9.0

Then use an eclipse plug-in project to wrap that JAR into an OSGi bundle and deploy to AEM. This will ensure these packages are part of the OSGi service container.

robin_wadhwa
October 17, 2018

Thanks for the response,

After adding (as google-maps-services is dependent on these 2 dependencies )-->

<dependency>

    <groupId>com.squareup.okio</groupId>

    <artifactId>okio</artifactId>

    <version>2.1.0</version>

</dependency>

<!-- https://mvnrepository.com/artifact/com.squareup.okhttp3/okhttp -->

<dependency>

    <groupId>com.squareup.okhttp3</groupId>

    <artifactId>okhttp</artifactId>

    <version>3.11.0</version>

</dependency>

I am getting-->

android.os -- Cannot be resolved

android.util -- Cannot be resolved

I am not sure why its showing android ?

PuzanovsP

smacdonald2008
Level 10
October 17, 2018

Did you try to download the JAR and place it into an OSGi bundle and deploy that - i am going to see if this works or if this leads to only more dependency errors.

Peter_Puzanovs
Community Advisor
Community Advisor
October 17, 2018

Dear Robin,

In your example you most likely want to avoid getting android libs. Therefore, you could add following rule:

<Import-Package>!android.os,!android.util, your other rules</Import-Package>

You need to look which part of google library you want to utilize,

Looking at functionality you want to use and configure Apache plugin to your needs[1]

[1] Apache Felix - Apache Felix Maven Bundle Plugin (BND)

Regards

Peter

smacdonald2008
Level 10
October 17, 2018

I wrapped the Google API JAR into an OSGi bundle and it now exports these packages-

Can you post the full Java file with import statements of the code you are using. I will try to get it running in AEM

robin_wadhwa
October 17, 2018

In my code I am trying to read an excel file and storing the data into nodes.

As in excel latitude and longitude is not available but address is there(city , street address, zip code). Here google API plays its role.

import Statements for google API:-

import com.google.maps.GeoApiContext;

import com.google.maps.GeocodingApi;

import com.google.maps.errors.ApiException;

import com.google.maps.model.GeocodingResult;

Code I am using to get Latitude and Longitude :-

            String address = fileImportModal.getAddress()+" "

                    +fileImportModal.getCity()+" "

                    +fileImportModal.getState()+" "

                    +fileImportModal.getZipCode();

 

            GeoApiContext context = new GeoApiContext.Builder()

                    .apiKey("KEY")

                    .build();

            GeocodingResult[] results =  GeocodingApi.geocode(context,

                    address).await();

            double latitude = results[0].geometry.location.lat;

            double longitude = results[0].geometry.location.lng;

          

            hcpNode.setProperty(RestoreConstants.DOC_LATITUDE, latitude);

            hcpNode.setProperty(RestoreConstants.DOC_LONGITUDE, longitude);

Thanks.

smacdonald2008
smacdonald2008Accepted solution
Level 10
October 17, 2018

I developed a small service with your Google MAP API code:

package com.adobe.aem.core;

import java.io.DataOutputStream;

import java.io.EOFException;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.ObjectInputStream;

import com.google.maps.DirectionsApi.RouteRestriction;

import com.google.maps.DistanceMatrixApi;

import com.google.maps.DistanceMatrixApiRequest;

import com.google.maps.GeoApiContext;

import com.google.maps.GeocodingApi;

import com.google.maps.errors.ApiException;

import com.google.maps.model.DistanceMatrix;

import com.google.maps.model.GeocodingResult;

import com.google.maps.model.LatLng;

import com.google.maps.model.TravelMode;

public class MapImpl {

public String getLat()

{

try{

GeoApiContext context = new GeoApiContext.Builder().apiKey("KEY") .build();

        String address = "1600 Amphitheatre Parkway Mountain View, CA 94043";

        GeocodingResult[] results = GeocodingApi.geocode(context, address).await();

}

catch(Exception e)

{

e.printStackTrace();

}

return "" ;

}

}

I added the dependencies you mentioned. Then built this OSGi bundle as a Maven 13 Archetype project. This now goes into ACTIVE state

The key was to wrap the Google MAP JAR into an OSGi bundle and deploy. Now my service that uses the GOOGLE MAP API can go into Active state. So we have 2 OSGi bundles here:

If you do not know how to use an Eclipse Plug-in project to wrap a JAR into an OSGi bundle - follow these instructions in this article (we wrap simple JSON JAR as an example):

Adobe Experience Manager Help | Submitting Adobe Experience Manager form data to Java Sling Servlets