Announcement

All important changes of my site and my blog will be announced here.

Computer security

This is my hobby. I spend my freetime for it. I am not professional in this area. But I like it very much.

Everything Else

Which doesn’t belong to other categories will land here. It maybe about my daily life, entertainment, music, game … or something like that

Programming

This is my job. That means everyday I must sit before the monitor. Tip something, be annoyed by bugs,… but I simply like it.

Tutorial

When I found something interesting, I would like to share it to everyone. That is the reason for this category comes.

Home » Programming

Windows Phone – Embedded fonts and Bing maps

Submitted by on Sunday, 11 April 20106 Comments | 3,069 views

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

Popularity: 2% [?]

My strongly recommended books to read. Choose one and enjoy yourself.

Related Posts:

  • No Related Posts

6 Comments »

  • Brian said:

    Good work on adding in the ip location, any chance of making our original article actually a clickable link?

  • admin (author) said:

    @Brian: Yes of course if you want.

  • RK said:

    Code is not availble..Please check it Once

  • RaviKumar said:

    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

  • admin (author) said:

    @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.

  • Roch said:

    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 your response!

Add your comment below, or trackback from your own site. You can also subscribe to these comments via RSS.

Be nice. Keep it clean. Stay on topic. No spam.

You can use these tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

This is a Gravatar-enabled weblog. To get your own globally-recognized-avatar, please register at Gravatar.