Expand my Community achievements bar.

SOLVED

Google Maps Service

Avatar

Level 1

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 ?

1 Accepted Solution

Avatar

Correct answer by
Level 10

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

OSSi.png

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:

OSGi2.png

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

View solution in original post

15 Replies

Avatar

Community Advisor

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

Avatar

Level 1

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>

Avatar

Community Advisor

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

Avatar

Level 10

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.

Avatar

Level 1

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

Avatar

Level 10

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.

Avatar

Community Advisor

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

Avatar

Level 10

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

GoogleDep.png

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

Avatar

Level 1

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.

Avatar

Correct answer by
Level 10

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

OSSi.png

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:

OSGi2.png

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

Avatar

Community Advisor

smacdonald2008​ would be still quite cautious about adding pretty much random Jar's into your main OSGi run time. Would consider it's much safer to include needed jar's to your project classpath using <Embed-Dependency call as it would avoid problems in long term.

Avatar

Level 10

Adding a JAR (from Maven repo) to the AEM Service container is simply another way to get the required Java packages into AEM so a OSGi bundle that needs them can find the Java packages.

I have found that this method as talked about here works

CQ-OPS - How to Turn a JDBC Driver JAR into an OSGi Bundle... 

In this example - Jayan wraps a driver file

Avatar

Community Advisor

Sir, I don't doubt that it works. It does work. All I'm highlighting here, is that when developers follow this approach, Adobe customer can end up with lot's of Jar's, that were probably better be embedded only in places where they are actually needed and not exposed to entire OSGi system.

Regards,

Peter

Avatar

Level 10

that is a great point! So there are 2 choices here - wrap the JAR that exposes the Java packages (sometimes you need to follow this in case of driver files) or use:

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

                 

I am still trying to figure out how to get the Google MAP API to work using the   Embed-Dependency way as opposed to wrapping the API JAR.  As i solved some with <Embed-Dependency> then new ones showed up. This became a dependency issue. Sometimes its just easier to wrap the JAR API into an OSGi bundle - like this Google MAP API. If there are only a few easy ones to add, then go the  <Embed-Dependency> route.

Avatar

Level 1

HI Robin

I was also facing the same issue with pervious version of google map service.

But with newer version the issue has been resolved now.

You can directly add dependencies in your core pom file like below.

 

<!-- https://mvnrepository.com/artifact/com.google.maps/google-maps-services -->
<dependency>
<groupId>com.google.maps</groupId>
<artifactId>google-maps-services</artifactId>
<version>2.2.0</version>
</dependency>

 

 

And after maven clean process it will automatically resolved the dependency.