Click here to Skip to main content
15,886,823 members
Articles / Web Development / HTML

Using Bing Maps for Windows 8 Metro Apps - C# / JavaScript

Rate me:
Please Sign up or sign in to vote.
4.97/5 (42 votes)
8 May 2013CPOL8 min read 151.6K   5.3K   69  
Building Windows 8 Metro applications using Bing Maps control.
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Bing.Maps;
using Windows.Devices.Geolocation;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234238

namespace BingDemo
{
    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    public sealed partial class MainPage : Page
    {
        const double maxZoom = 19;
        const double minZoom = 5;

        private CustomPin pin;
        public MainPage()
        {
            this.InitializeComponent();
            pin = new CustomPin();
            map.Children.Add(pin);
        }

        /// <summary>
        /// Invoked when this page is about to be displayed in a Frame.
        /// </summary>
        /// <param name="e">Event data that describes how this page was reached.  The Parameter
        /// property is typically used to configure the page.</param>
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {

        }


        private void btnZoomOut_Click(object sender, RoutedEventArgs e)
        {
            var zoom = map.ZoomLevel - 2;
            map.SetZoomLevel(zoom < minZoom ? minZoom : zoom);
        }

        private void btnZoomIn_Click(object sender, RoutedEventArgs e)
        {
            var zoom = map.ZoomLevel + 2;
            map.SetZoomLevel(zoom > maxZoom ? maxZoom : zoom);
        }

        private void btnChangeMapType_Click(object sender, RoutedEventArgs e)
        {
            switch (map.MapType)
            {
                case Bing.Maps.MapType.Aerial:
                    map.MapType = Bing.Maps.MapType.Birdseye;
                    break;
                case Bing.Maps.MapType.Birdseye:
                    map.MapType = Bing.Maps.MapType.Road;
                    break;
                default:
                    map.MapType = Bing.Maps.MapType.Aerial;
                    break;
            }
        }

        private async void btnSetLocation_Click(object sender, RoutedEventArgs e)
        {
            Geolocator geolocator = new Geolocator();
            var pos = await geolocator.GetGeopositionAsync(TimeSpan.FromDays(10), TimeSpan.FromHours(1));
            Location location = new Location(pos.Coordinate.Latitude, pos.Coordinate.Longitude);

            //Center map view on current location.
            MapLayer.SetPosition(pushPin, location);
            map.SetView(location, 15.0f);
        }

        private void map_Tapped(object sender, TappedRoutedEventArgs e)
        {
            var pos = e.GetPosition(map);
            Location location;
            map.TryPixelToLocation(pos, out location);

            MapLayer.SetPosition(pin, location);
            map.SetView(location);
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

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


Written By
Architect Sela
Israel Israel
Shai Raiten is VS ALM MVP, currently working for Sela Group as a ALM senior consultant and trainer specializes in Microsoft technologies especially Team System and .NET technology. He is currently consulting in various enterprises in Israel, planning and analysis Load and performance problems using Team System, building Team System customizations and adjusts ALM processes for enterprises. Shai is known as one of the top Team System experts in Israel. He conducts lectures and workshops for developers\QA and enterprises who want to specialize in Team System.

My Blog: http://blogs.microsoft.co.il/blogs/shair/

Comments and Discussions