Showing posts with label Bing. Show all posts
Showing posts with label Bing. Show all posts

Saturday, August 04, 2012

Using Bing Maps in a Metro App

Step 1: Download the Bing Maps SDK.

This is easier now as its been made into a Visual Studio Extension.

image

Step 2: Create a blank Metro App

Step 3: Reference the SDK dlls:

You need to reference Bing Maps as well as the Visual C++ runtime (don’t worry, this is only a dependency and you will not have to write an C++ code)

image

Step 4: Get your access key

To get your access key you need to sign up for a developer account at the Bing Maps developer portal (https://www.bingmapsportal.com/). Once you have created your account, you need to create a key for your metro app. Remember to set up your app as a Metro app when creating your key.

Step 5: Drop your key into your app.

A good practice is to make your key an application resource. This will make it easy to use it on any page that uses the Maps control.

To do this, open your App.xaml page and add the following line right after the “</ResourceDictionary.MergedDictionaries>” line:

<x:String x:Key="BingMapsApiKey">Insert Your Api Key</x:String>

image

Step 6: Reference the Bing.Maps namespace in the page where you intend to use the map control. To do this add the following line to the page declaration:

xmlns:bing="using:Bing.Maps"

image

Step 7: Add the map control. To do this, just add the following line in your page (I added it to the default grid that gets created when you create a blank page).

<Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
        <bing:Map x:Name="MyFirstBingMap" Credentials="{StaticResource BingMapsApiKey}" />
</Grid>

Step 8: Run your app and admire your creation Smile

image

Doing more:

Step 9: Centering the map to your location:

First, you need to enable your app to use location data. To do that, double click the “Package.appmanifest” file and on the capabilities tab, select “location”.

image

In the designer for you page, add a MapLayer to your page:

<bing:Map x:Name="MyFirstBingMap" Credentials="{StaticResource BingMapsApiKey}">
            <bing:MapLayer x:Name="MyMapLayer">
            </bing:MapLayer>
</bing:Map>

Add the following code to the “OnNavigatedTo” event in the page containing your map:

async protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            Geolocator geo = new Geolocator();
            geo.DesiredAccuracy = PositionAccuracy.Default;
            var currentPosition = await geo.GetGeopositionAsync();

            Location loc = new Location()
            {
                Latitude = currentPosition.Coordinate.Latitude,
                Longitude = currentPosition.Coordinate.Longitude
            };

            MyFirstBingMap.SetView(loc, 13, 0);

            Canvas pushPin = new Canvas();
            pushPin.Children.Add(new Ellipse() { Fill = new SolidColorBrush(Windows.UI.Colors.Red), Width = 22, Height = 22, Stroke = new SolidColorBrush(Windows.UI.Colors.Black), StrokeThickness = 2 });

            MapLayer.SetPosition(pushPin, loc);
            MyMapLayer.Children.Add(pushPin);

        }

Running the application will result in the following map:

image

Things to notice:

1. The method is labeled as “async” this is because we are using the “await” keyword on the GetGeopositionAsync method of the GeoLocator.

2. Instead of using “Pushpin” control, I am using a canvas and drawing a circle. The reason is the Pushpin control does not seem to allow changing of colors or size. (Don’t know if this is a bug in the release preview or by design). Which is why I am using a custom control as my pushpin.

Saturday, December 24, 2011

Bing maps–plane taking off

Found this in Bing’s satellite imagery – an airplane taking off from Las Vegas airport.

http://binged.it/ru5Er8

image

Sunday, September 13, 2009

Testing out the SilverLight Map Control

What am I doing right now? Taking the Bing SilverLight Map Control for a quick spin.

The first thing you need to do (after installing the Silverlight SDK) is to download the Silverlight Map Control SDK. It is currently in CTP and can be downloaded from the following site:

https://connect.microsoft.com/silverlightmapcontrolctp

At this time, you need to “self-apply” for access to the CTP (which means you need to log in with a live account and click on the apply link).

Hello World Sample for SilverLight

This was easier than I could imagine.

I created a SilverLight application using the new project wizard:
image

on the next page I had to make sure that I selected that the app would be hosted in a web-site and I set it up as a web-application project.

image

Next, I added a reference to the SilverLight map control to the SilverLight project. (which I found in the following location - C:\Program Files\Microsoft Virtual Earth Silverlight Map Control\CTP\Libraries)

image

Now, I all I had to do was add the control to my SilverLight page. VS had already added a page called “MainPage.xaml”. I had to first add the following line to the navigation node of the document (to reference the namespace of the map-control in the document):

xmlns:m="clr-namespace:Microsoft.VirtualEarth.MapControl;assembly=Microsoft.VirtualEarth.MapControl"

image

Final step, add the control to the page.

<m:Map/>

Run the application and you should see the following:

image

That was EASY!

Wednesday, July 01, 2009

OptiRoute has been updated

OptiRoute has been updated with tweaks to the UI and the algorithm. I have also added a help document and screen casts on using OptiRoute.

Using OptiRoute with Bing Maps

Using OptiRoute with Google Maps