Click here to Skip to main content
Click here to Skip to main content

Google Maps for Windows Phone 7 using Bing Map Control

By , 3 Feb 2011
 
main.PNG

Introduction

Microsoft launched Bing Maps in recent times which is a direct competitor to Google Maps. Bing Maps being relatively new has of course less data and lesser depth in Maps than Google Maps which is in this field for quite a few years. Often, developers want to incorporate Google Maps in their Windows Phone application which seems quite impossible as the Map Control provided by Microsoft is compatible with Bing Maps.

But the thing that we don't know is that Map Control is so flexible that we could display almost any Map on it (thanks to Microsoft for providing us the flexibility). So in this manner we are not bound to show just Bing Maps but we could also use any other map like Google Maps which is covered in this article.

Background

Basically Map Control is shipped with default imagery of Bing Maps for which we only need to provide Developer Credentials available from Bing Website and we are ready to use the Bing Maps. Map Control has all the basic functionality already built in like zooming and navigating into the Map. All we need is to just drop the control, provide credentials and here we go.

Using the Code

For Google Maps, we need to first understand the way Map Control works, well it basically continuously calls a method Uri GetUri(int x, int y, int zoomLevel) which returns a Uri which contains the image of the tile to be displayed. A tile is an image of 256x256 defined by its X and Y position in the grid forming the map of the world on a specific zoom level.

Uri GetUri(int x, int y, int zoomLevel)

All we have to do is to write a class that inherits from Microsoft.Phone.Controls.Maps.TileSource that contains a overridden method Uri GetUri(int x, int y, int zoomLevel) which will display our custom tile which in our case is Google Maps Tile.

First, we need to define an Enum that contains all the modes of maps that Google Maps supports. We will use this Enum later when we will inspect the mode of our Map.

public enum GoogleTileTypes
    {
        Hybrid,
        Physical,
        Street,
        Satellite,
        WaterOverlay
    }

Next we move on towards writing the class that we discussed above that will contain Uri GetUri(int x, int y, int zoomLevel) and some utility methods that we have created for our convenience.

public class GoogleTile : Microsoft.Phone.Controls.Maps.TileSource
    {
        private int _server;
        private char _mapmode;
        private GoogleTileTypes _tiletypes;

        public GoogleTileTypes TileTypes
        {
            get { return _tiletypes; }
            set
            {
                _tiletypes = value;
                MapMode = MapModeConverter(value);
            }
        }

        public char MapMode
        {
            get { return _mapmode; }
            set { _mapmode = value; }
        }

        public int Server
        {
            get { return _server; }
            set { _server = value; }
        }

        public GoogleTile()
        {
            UriFormat = @"http://mt{0}.google.com/vt/lyrs={1}&z={2}&x={3}&y={4}";
            Server = 0;
        }

        public override Uri GetUri(int x, int y, int zoomLevel)
        {
            if (zoomLevel > 0)
            {
                var Url = string.Format(UriFormat, Server, MapMode, zoomLevel, x, y);
                return new Uri(Url);
            }
            return null;
        }

        private char MapModeConverter(GoogleTileTypes tiletype)
        {
            switch (tiletype)
            {
                case GoogleTileTypes.Hybrid:
                    {
                        return 'y';
                    }
                case GoogleTileTypes.Physical:
                    {
                        return 't';
                    }
                case GoogleTileTypes.Satellite:
                    {
                        return 's';
                    }
                case GoogleTileTypes.Street:
                    {
                        return 'm';
                    }
                case GoogleTileTypes.WaterOverlay:
                    {
                        return 'r';
                    }
            }
            return ' ';
        }
    }

We use the following URL as our Google Tile Source:

UriFormat = @"http://mt{0}.google.com/vt/lyrs={1}&z={2}&x={3}&y={4}";

Where we have the following parameters:

  • {0} refers to the server number which we had kept y to 1
  • {1} refers to the character code of the mode of the map
  • {2} refers to the zoom level of the map
  • {3} refers to the x cordinate of the map
  • {4} refers to y cordinate of the map

Now, we move on towards the XAML part where we actually define the Map Control and its layers. First, we need the following two namespaces to MainPage.xaml:

xmlns:GoogleTileSource="clr-namespace:googlemaps;assembly=googlemaps"
xmlns:MSPCMCore="clr-namespace:Microsoft.Phone.Controls.Maps.Core;
	assembly=Microsoft.Phone.Controls.Maps"

We will keep or Map Mode to Mercator which tells the control not to load any default imagery for the Map. Next, we place layers on the Map Control on which Tiles of Google Maps will be displayed. We have used 5 layers as we want to change the Mode of Map as per user's selection of RadioButtons. One could also show more than one layer at the same time by playing around with the Opacity property of different layers. If you want to keep the Mode static, you will only be needing one layer. Each layer has its Tile Source which in this case is the class GoogleTile.

<Microsoft_Phone_Controls_Maps:Map Name="googlemap" Margin="0,161,0,0" 
	CopyrightVisibility="Collapsed" LogoVisibility="Collapsed" 
	ScaleVisibility="Visible" CredentialsProvider=
		"ApBXPZf5IR94SLXE8nh5FYsb5WHKrH1XPY7428-EqQudseivcWhCROIJvGmtnkAV">
    <Microsoft_Phone_Controls_Maps:Map.Mode>
        <MSPCMCore:MercatorMode/>
    </Microsoft_Phone_Controls_Maps:Map.Mode>
    <Microsoft_Phone_Controls_Maps:MapTileLayer Name="street" Margin="0,0,0,32">
        <Microsoft_Phone_Controls_Maps:MapTileLayer.TileSources>
            <GoogleTileSource:GoogleTile TileTypes="Street"/>
        </Microsoft_Phone_Controls_Maps:MapTileLayer.TileSources>
    </Microsoft_Phone_Controls_Maps:MapTileLayer>
    <Microsoft_Phone_Controls_Maps:MapTileLayer Visibility="Collapsed" 
		Name="wateroverlay" Margin="0,0,0,32">
        <Microsoft_Phone_Controls_Maps:MapTileLayer.TileSources>
            <GoogleTileSource:GoogleTile TileTypes="WaterOverlay"/>
        </Microsoft_Phone_Controls_Maps:MapTileLayer.TileSources>
    </Microsoft_Phone_Controls_Maps:MapTileLayer>
    <Microsoft_Phone_Controls_Maps:MapTileLayer Visibility="Collapsed" 
		Name="hybrid" Margin="0,0,0,32">
        <Microsoft_Phone_Controls_Maps:MapTileLayer.TileSources>
            <GoogleTileSource:GoogleTile TileTypes="Hybrid"/>
        </Microsoft_Phone_Controls_Maps:MapTileLayer.TileSources>
    </Microsoft_Phone_Controls_Maps:MapTileLayer>
    <Microsoft_Phone_Controls_Maps:MapTileLayer Visibility="Collapsed" 
		Name="satellite" Margin="0,0,0,32">
        <Microsoft_Phone_Controls_Maps:MapTileLayer.TileSources>
            <GoogleTileSource:GoogleTile TileTypes="Satellite"/>
        </Microsoft_Phone_Controls_Maps:MapTileLayer.TileSources>
    </Microsoft_Phone_Controls_Maps:MapTileLayer>
    <Microsoft_Phone_Controls_Maps:MapTileLayer Visibility="Collapsed" 
		Name="physical" Margin="0,0,0,32">
        <Microsoft_Phone_Controls_Maps:MapTileLayer.TileSources>

            <GoogleTileSource:GoogleTile TileTypes="Physical"/>
        </Microsoft_Phone_Controls_Maps:MapTileLayer.TileSources>
    </Microsoft_Phone_Controls_Maps:MapTileLayer>
</Microsoft_Phone_Controls_Maps:Map>

Finally to provide users with some basic functionality of changing Mode and Zooming in and out, we wire up some event handlers.

private void Button_Click(object sender, RoutedEventArgs e)
        {
            if (h.IsChecked == true)
            {
                hybrid.Visibility = Visibility.Visible;
                satellite.Visibility = Visibility.Collapsed;
                street.Visibility = Visibility.Collapsed;
                physical.Visibility = Visibility.Collapsed;
                wateroverlay.Visibility = Visibility.Collapsed;
            }
            else if (st.IsChecked == true)
            {
                hybrid.Visibility = Visibility.Collapsed;
                satellite.Visibility = Visibility.Collapsed;
                street.Visibility = Visibility.Visible;
                physical.Visibility = Visibility.Collapsed;
                wateroverlay.Visibility = Visibility.Collapsed;
            }
            else if (sl.IsChecked == true)
            {
                hybrid.Visibility = Visibility.Collapsed;
                satellite.Visibility = Visibility.Visible;
                street.Visibility = Visibility.Collapsed;
                physical.Visibility = Visibility.Collapsed;
                wateroverlay.Visibility = Visibility.Collapsed;
            }
            else if (p.IsChecked == true)
            {
                hybrid.Visibility = Visibility.Collapsed;
                satellite.Visibility = Visibility.Collapsed;
                street.Visibility = Visibility.Collapsed;
                physical.Visibility = Visibility.Visible;
                wateroverlay.Visibility = Visibility.Collapsed;
            }
            else
            {
                hybrid.Visibility = Visibility.Collapsed;
                satellite.Visibility = Visibility.Collapsed;
                street.Visibility = Visibility.Collapsed;
                physical.Visibility = Visibility.Collapsed;
                wateroverlay.Visibility = Visibility.Visible;
            }
        }

        private void ButtonZoomIn_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            googlemap.ZoomLevel++;
        }

        private void ButtonZoomOut_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            googlemap.ZoomLevel--;
        }

So we are ready to take off with just a few lines of code, we are now able to use Google Maps with Bing Map Control provided by Microsoft in Windows Phone Developer tools (thanks to Microsoft for flexible and generic nature of the Map Control).

hybrid.PNG physical.PNG wateroverlay.PNG

street.PNG satellite.PNG

Points of Interest

In future, I plan to provide a more detailed tutorial containing all the features that Google Map provides including directions and routes.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Naveed tejani
Student Unikrew Solutions
Pakistan Pakistan
Member
A student of Computer Sciences at NUCES-FAST karachi, Pakistan.
 
Having interst in mostly Microsoft related tools and technologies like :
 
.Net Framework
Expresison Blend
Windows Phone 7
SQL Server
Asp.net
WPF
Silverlight

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
QuestionGoogle Mapmembersameer_r21 May '13 - 9:45 
How can I center the map to an x/y coord on an onclick event from a textbox?
GeneralMy vote of 4memberdanyloid3 Dec '12 - 22:56 
thanks for the article. nice approach.
GeneralMy vote of 4memberPham Anh Khoa5 Nov '12 - 14:57 
Hi Naveed,your post is wonderful for me. But i don't know how to get direction with your app.
GeneralMy vote of 5mvpKanasz Robert21 Sep '12 - 3:29 
Not bad
QuestionClose but no cigar!!!!memberMember 165621426 Aug '12 - 10:20 
Google maps for Windows Phone 7 using Bing map Control is an issue
that has been debated over and over on the WP 7/Silverlight.net community forums...
 
Technically speaking, sure it can be done...I really don't see nothing new under the sun in this tut...
I mean no big deal...
 
Legally speaking ... it's not... "You cannot access Google Maps imagery outside of an interface (read: APIs) provided by Google.
This is mentioned in an online FAQ which references an item in the terms of service"...
 
Sorry but combining both tiles in one Api is against the law Dead | X|
QuestionNavigation , Latitude ??membercuj3om3 Jun '12 - 0:47 
Hi, i'm a newbie developer. Can you give me a suggestion to use latitude, longtitude or navigation in your code here ? I'm starting to developt a app using googlemaps, not bing map so i need your help ! contact me at email if you can help, Thank a lot !
Sorry for my english !
Questionisn't it outdated?memberSperneder Patrick1 Jun '12 - 6:55 
using Bing-Maps in WP?
they even do have their own tut.
 
regards
P.
GeneralMy vote of 5memberShibu K V7 May '12 - 9:28 
Excellent piece of work. Appreciate sharing the knowledge with the community. Keep it up.
Generaldirections and routes.memberewfweefewewfewf28 Mar '12 - 0:02 
"In future, I plan to provide a more detailed tutorial containing all the features that Google Map provides including directions and routes."
 
and i'm waiting for that, when will you update this artical Smile | :)
GeneralMy vote of 4membermmabdullah14 Mar '12 - 22:56 
Nice, but i think you can reduce some lines of your code too. You do no need all 4 lines to repeat 4 times.
 
hybrid.Visibility = Visibility.Visible;
satellite.Visibility = Visibility.Collapsed;
street.Visibility = Visibility.Collapsed;
physical.Visibility = Visibility.Collapsed;
wateroverlay.Visibility = Visibility.Collapsed;
QuestionExcuse me! [modified]memberTM50010 Feb '12 - 13:34 
Excuse me!
I am working on a map application.I am working on   window phone 7 too.
i saw you use   this URL
UriFormat = @"http://mt{0}.google.com/vt/lyrs={1}&amp;amp;z={2}&amp;amp;x={3}&amp;amp;y={4}";
Then i wonder: "is this a URL of web service of the google map?".
Would you please help me to know   about this??
Where i could find information for   this URL??
I want to ddevelop more. Please show me.
 
Thx.

-- modified 13 Feb '12 - 2:12.
GeneralMy vote of 5memberPark,munchan3 Feb '12 - 3:24 
thank you post~
QuestionMap ProblemmemberHimanshu Nigam29 Aug '11 - 0:06 
Hi,
 
Thank you so much for providing this helpful code.
 
But I have one query that can I directly go to a particular location if I know Latitude and Longitude of a place.
 
I want to show the exact street in a city directly without navigating and for this I need to know x & y co-ordinate, so how to get this x & y co-ordinate.
 
And also can I mark this position in the Map with a circle or anything.
Questionadd pushpinmemberfithri16 Aug '11 - 21:11 
nice share..
I've tried the program and I did't get in trouble, but after I add pushpin in my map, my map lost the color to black, whether you can give the example for adding pushpin ..
 
thank you very much before.
AnswerRe: add pushpinmemberfithri16 Aug '11 - 21:37 
I already know why I lost my map, it is because the position at the return of the map is the screen coordinates not the map coordinates. but Did you can give an example to add a pushpin?
GeneralThis is against the Terms of UsememberRicky Brundritt15 Apr '11 - 11:00 
This is 100% against the terms of use for both Bing Maps and Google Maps. Don't use this for a production application. If you want Google Maps in WP7 take a look at porting over the DeepEarth project to WP7.
GeneralMy vote of 1memberRicky Brundritt15 Apr '11 - 10:58 
This is against licensing for both Bing Maps and Google Maps.
GeneralMy vote of 1memberMorten on GIS8 Feb '11 - 17:50 
This article is telling users how to break the Google License agreement, and should not be promoted in any way. Note this is not a rate of the article's quality or the content presented, but the mere fact that it can get a lot of users into a lot of legal problems. See google maps licence agreement #10.1 and 10.12, and
http://code.google.com/apis/maps/faq.html#tos_tiles[^]
http://rexdotnet.blogspot.com/2010/09/use-google-maps-with-arcgis-silverlight.html[^]
GeneralLegal issuesmembermuhammadumairomee9 Feb '11 - 17:03 
According to google term of services (http://www.google.com/accounts/TOS[^])
one can not access their services through other interfaces.. it is illegal..
 
But each of their tile.. when accessed comes with their logo.. so you are maintaining the integrity of their trademarks. In the article of mine Real Time Position Display Software Model for Google Maps using C#.NET and Microsoft XNA Framework[^]
 
I can also got the data from some other service providers with license if it is difficult to get license from Goolge because can also provide you the separate license for accessing their tiles maps.
Muhammad Umair
(Software Engineer)
(Interested in C#, .Net, Interfacing, Device Drivers, GIS, WCF, Asp.net, Ajax, Architecture and Design Patterns)

GeneralRe: Legal issuesmemberMorten on GIS10 Feb '11 - 15:21 
So... Are you agreeing with meow are you not?
I know for a fact that getting this type of license from google is pretty much impossible, because google is bound by licensing of their data themselves (ie they don't have the rights to redistribute the data through other means than their API).
The logo is only in the tiles to detect that you are using their stuff from other services, not to make it ok to steal their tiles.
GeneralMy vote of 5memberBenWintringham7 Feb '11 - 21:50 
Very useful information presented clearly.
QuestionWhat's the legal positionmemberBenWintringham7 Feb '11 - 1:45 
Hi
Nice article, and very interesting.
But what is the legal position with using google map information in this way?
Plus will it pass the marketplace testing do you know?
AnswerRe: What's the legal positionmemberNaveed tejani7 Feb '11 - 11:06 
there is no legal issue in using this information since Google is providing its Map API to developers and above application uses the same API. So no issues for passing the marketplace test Smile | :)
GeneralRe: What's the legal positionmemberBenWintringham7 Feb '11 - 21:49 
Hi Naveed
That's fantastic news, as it opens up a whole host of possiblities. I had dismmised this approach thinking that Google did not allow this way of using their data.
As I need google data rather than Bing data. As the area I am trying to use does not have good coverage from Bing yet..
By the way have you tried this approach with the map control that comes with standard Silverlight?
 
Regards
Ben
AnswerRe: What's the legal positionmemberMorten on GIS8 Feb '11 - 17:41 
In what way is this using the Google Maps API?
I'm 99.99999% sure that this thing is NOT allowed by Google.
 
I cannot for the life of me see how this is NOT a violation of the Google Maps API. I've been dealing with this issue in quite a lot of other cases, and know their agreement fairly well by now (basically using any of their data outside their JavaScript API is not allowed - especially scraping the tiles off their server like this).

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web01 | 2.6.130523.1 | Last Updated 3 Feb 2011
Article Copyright 2011 by Naveed tejani
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid