Windows Phone – Embedded fonts and Bing maps

Today I would like to illustrate about how I can embed font and Bing maps into Windows Phone application. I know it’s a such simple example that every programmer on Windows Phone can write but I just want to make a demo to show that we can use embedded font and host a Bing map control. For Bing map example, I use this example http://www.earthware.co.uk/blog/index.php/2010/03/writing-a-bing-maps-location-aware-application-for-windows-phone-7-series/ with some modifications so that I can relatively find my location on Bing map.

Embedded fonts

Silverlight allows us to embed font in application without installing it on operating system. Using custom font makes GUI of application really beautiful and creative. To embed font, copy it to a folder of your project, set “Build Action” to “Content” and link to control as following

<TextBlock FontFamily="Fonts/AnnabelScript.ttf#Annabel Script" Text="Duration" x:Name="txtDuration" Margin="20,530,173,82"/>

Bing maps

To use Bing map control, you can read the example above. I just tell which I learned when I was trying to use Microsoft.Maps.MapControl library. It’s really very strange. After adding reference to Microsoft.Maps.MapControl, if I add this reference in XAML first

xmlns:bing="clr-namespace:Microsoft.Maps.MapControl;assembly=Microsoft.Maps.MapControl"

Then I can not use Map control in XAML.

<bing:Map Name="mapMain" NavigationVisibility="Collapsed" Mode="AerialWithLabels" CredentialsProvider="USE_YOUR_BING_MAP_KEY" Height="577">
	<bing:MapLayer>
		<Ellipse Fill="Red" Width="20" Height="20" bing:MapLayer.Position="0,0" Name="ppLocation" Visibility="Collapsed" />
	</bing:MapLayer>
</bing:Map>

Visual Studio says that it can not find Map control and asks if I forget to add reference to assembly. But if I use Map control in XAML first and add reference later then everything works.

private void btnBrowse_Click(object sender, RoutedEventArgs e)
{
	GetLocationOfCity();
}

private void GetLocationOfCity()
{
	string strGetIpUrl = "http://www.whatismyip.com/automation/n09230945.asp";
	WebClient wcIP = new WebClient();
	wcIP.DownloadStringCompleted += new DownloadStringCompletedEventHandler(wcIP_DownloadStringCompleted);
	wcIP.DownloadStringAsync(new Uri(strGetIpUrl, UriKind.Absolute));
}

void wcIP_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
	string strIP = e.Result;
	string strLocation = "http://api.hostip.info/?ip=" + strIP + "&position=true";
	WebClient wcLocation = new WebClient();
	wcLocation.DownloadStringCompleted += new DownloadStringCompletedEventHandler(wcLocation_DownloadStringCompleted);
	wcLocation.DownloadStringAsync(new Uri(strLocation, UriKind.Absolute));
}

void wcLocation_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
	string strResult = e.Result;
	string strOpenTag = "<gml:coordinates>";
	int nStartIndex = strResult.IndexOf(strOpenTag) + strOpenTag.Length;
	string strClosedTag = "</gml:coordinates>";
	int nEndIndex = strResult.IndexOf(strClosedTag);
	string strCoordinates = strResult.Substring(nStartIndex, nEndIndex - nStartIndex);
	double longtitude = Convert.ToDouble(strCoordinates.Split(',')[0]);
	double latitude = Convert.ToDouble(strCoordinates.Split(',')[1]);
	Location locCurrent = new Location(latitude, longtitude);
	mapMain.SetView(locCurrent, 15);

	MapLayer.SetPosition(ppLocation, locCurrent);
	ppLocation.Visibility = System.Windows.Visibility.Visible;
}

As you can see, because the location service is not available with emulator cause of lacking GPS device. I modify the example by getting my IP address through service of whatismyip and then pass my IP to service of hostip to get the longitude and latitude of that IP. With these information I can use Bing map control to navigate to my location but it’s not exact 100%. I hope with a real phone and GPS device, the Bing map can locate exactly where I am.
You can download the source code of Bing map example “Windows Phone Bing Map

7 thoughts on “Windows Phone – Embedded fonts and Bing maps”

  1. Hi,
    I am not getting the gml:coordinates in the wcLocation_DownloadStringCompleted event…in the e.Result, gml:coordinates under ipLocation node is not there.

    Thanks
    RK

  2. @RaviKumar: The web service which I used, is a free one. Maybe your city is not supported. Try to access it directly from your browser and see if your needed information is there.

  3. Is there a way to have an embed map of bing using the bird’s eye view but zoomed out? The default zoom is too close and can’t really show the whole neighborhood.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.