We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

How to use Google Maps API to show Google Maps in Xamarin.Forms SfMaps control

Platform: Xamarin.Forms |
Control: SfMaps

Description

This article describes how to use Google Maps API to show Google Maps using ImageryLayer of the Xamarin Maps(SfMaps) control.

Solution:

You can show the Google Maps using the RequestTileUri event of ImageryLayer.

Step 1: Subscribe the RequestTileUri event of ImageryLayer as demonstrated in the following code example.

XAML:

 
<maps:SfMaps>
   <maps:SfMaps.Layers>
         <maps:ImageryLayer RequestTileUri="ImageryLayer_RequestTileUri">
          <maps:ImageryLayer.MarkerSettings>
                <maps:MapMarkerSetting MarkerIcon="Image" ImageSource="pin.png" IconSize="15"/>
           </maps:ImageryLayer.MarkerSettings>
                <maps:ImageryLayer.Markers>
                    <maps:MapMarker Latitude="38.8833" Longitude= "-77.0167"/>
                    <maps:MapMarker Latitude="-15.7833" Longitude= "-47.8667"/>
                    <maps:MapMarker  Latitude="21.0000" Longitude= "78.0000"/>
                    <maps:MapMarker Latitude="35.0000" Longitude= "103.0000" />
                    <maps:MapMarker Latitude="-4.0383" Longitude= "21.7586" />
             </maps:ImageryLayer.Markers>
           </maps:ImageryLayer>
    </maps:SfMaps.Layers>
</maps:SfMaps>
 

 

Step 2: In the RequestTileUri event, assign the Google Maps API Uri link (or any tile maps provider) to corresponding x, y, and zoom level of the Uri property of TileUriArgs as demonstrated in the following code example.

C#

/// <summary>
/// This event is used to pass the uri of desired maps tile provider.
/// </summary>
/// <param name="sender">The imagery layer</param>
/// <param name="e">The Tile uri arguments</param>
private void ImageryLayer_RequestTileUri(object sender, Syncfusion.SfMaps.XForms.TileUriArgs e)
        {
            var link = "http://mt1.google.com/vt/lyrs=y&x=" + e.X.ToString() + "&y=" + e.Y.ToString() + "&z=" + e.ZoomLevel.ToString();
            e.Uri = link;
           
        }

 

Google maps using xamarin SfMaps

 

You can download the demo sample from the following link

2X faster development

The ultimate Xamarin UI toolkit to boost your development speed.
ADD COMMENT
You must log in to leave a comment
Comments
Shane Coufreur
Jan 03, 2019

Everything added to the imagelayer on line 7 of MainPage will be removed by line 16 in CustomMapRenderer.

This solution completely removes the ability to add any custom markers on the map.

Reply
Sheik Syed Abthaheer M [Syncfusion]
Jan 04, 2019

Before clearing the Layers collection from maps, you have to assign the old imagery layer properties to new extended  imager layer(ImageryLayerExt). For example, you have to assign old layer markers collection to the ImageryLayerExt’s markers collection to display the marker  as in below code. 


Please use the following code in the CustomMapRenderer.

 

void AddLayer()

{

ImageryLayerExt layer = new ImageryLayerExt();

NativeMap.SfMaps nativeMap = (Control as NativeMap.SfMaps);

var nativeLayer = nativeMap.Layers[0] as NativeMap.ImageryLayer;

if(nativeLayer != null)

  layer.Markers = nativeLayer.Markers;

nativeMap.Layers.Clear();

nativeMap.Layers.Add(layer as NativeMap.ImageryLayer);

}

 

Reply
Sheik Syed Abthaheer M [Syncfusion]
Apr 05, 2019

From version 17.1.0.38, we have provided direct API as RequestTileUri to achieve this supportwithout using custom renderer for each platform. We have modified the sample and code based on new API.

Reply
Karl
Feb 28, 2022

How would you go about it if you had a openstreet maps tile map instead of google http://a.tile.opentopomap.org/{z}/{x}/{y}.png ? As I didn't not under stand this part In the RequestTileUri event, assign the Google Maps API Uri link (or any tile maps provider) to corresponding x, y, and zoom level of the Uri property of TileUriArgs as demonstrated in the following code example.

Reply

Please sign in to access our KB

This page will automatically be redirected to the sign-in page in 10 seconds.

Up arrow icon

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon

Live Chat Icon For mobile