3

I'm currently working on a project where I need to add a Google My Maps map to a fragment, but I've been searching for a while, and I haven't been able to find a way to introduce the URL. My guess is that it should be a method somewhere in the GoogleMap class. Also, I have noticed that there are some methods like addPolygon which could help achieve a similar result as the MyMaps map. However, that would be a pretty messy approach, as I'd have to add many coordinates, and it would become really time consuming.

Thanks!

Andrii Omelchenko
  • 13,183
  • 12
  • 43
  • 79
eOf
  • 393
  • 1
  • 3
  • 11

2 Answers2

4

Im not sure about process of not saving the KML file and showing in Fragment.

But by manually downloading the file as shown below , we can include the same in Fragment.


enter image description here

Now create folder raw inside res folder.

Paste the downloaded KML file in raw folder.

Update the dependency of google maps as shown.

api 'com.google.maps.android:android-maps-utils:0.5'

Inside your fragment do the following.

KmlLayer kmlFile;

public void onMapReady(GoogleMap googleMap) {
   ...............
   ...............
   try {
            kmlFile = new KmlLayer(googleMap, R.raw.my_map, getContext());
            kmlFile.addLayerToMap();
        } catch (XmlPullParserException e) {
            Log.e(TAG,""+e.toString());
        } catch (IOException e) {
            Log.e(TAG,""+e.toString());
        }
}

I have used below to display normal google map. This map loading is as usual procedure for showing map in Fragment

<com.google.android.gms.maps.MapView
                android:id="@+id/placeOrderMap"
                android:layout_width="match_parent"
                android:layout_height="200dp"
>
</com.google.android.gms.maps.MapView>

Now once the Fragment is loaded, swipe to the location were you created your Google My Maps and you can see the created map Pin / Icon etc.

The output will be as shown below according to your created elements.

enter image description here

Sreehari
  • 5,621
  • 2
  • 25
  • 59
  • Hello, your method seems to work pretty fine when exporting to a KML. If instead I use the KMZ version, in order to keep the icons, I get an error from the XmlParser: org.xmlpull.v1.XmlPullParserException: unterminated entity ref (position:TEXT @1:213 in java.io.InputStreamReader@df5ffa7) . If I extract the KML from the generated KMZ, it works, but without the icons' images, of course. Any idea if I need to use something special for kmz? – eOf Jun 27 '18 at 16:45
  • I have no clue on those lines. Probably you can post this specific question as a different thread. As its more specific than your question **Add MyMaps map to an Android Fragment** – Sreehari Jun 28 '18 at 07:15
4

You don't need GoogleMap object for My Maps visualization. Just add WebView to your fragment and load your My Map into it via shared URL:

private WebView mMyMapWebView;
...

mMyMapWebView = (WebView) findViewById(R.id.mymap_webview);
mMyMapWebView.getSettings().setJavaScriptEnabled(true);
mMyMapWebView.setWebViewClient(new WebViewClient());
mMyMapWebView.loadUrl(<your_mymap__url_e.g. https://drive.google.com/open?id=...>);

in .xml WebView can be described like that:

<WebView
    android:id="@+id/mymap_webview"
    android:layout_width="match_parent"
    android:layout_height="match_parent"></WebView>

Sharing URL for your My Map you can get by press Share button on My Maps control panel:

My Maps Main view

and in on window with shared URL you also should change access rights from private to "Anyone who has link" or public:

My Maps Sharing settings

and than, on Android device you get something like that:

My Maps on Android device

Andrii Omelchenko
  • 13,183
  • 12
  • 43
  • 79