none
How to call Bing Map API's in a C# Windows form Application?

    Question

  • Please help me in calling Bing Map API's in the C# Windows form Application only and not using WPF or SOAP etc as I am new to it... I am not sure of how to call it using C# . I want API's like open,Search,Zoom in, Zoom out, Pan in any direction,aerial view,road view and labels.. what all urls am i supposed to add.. and how do i obtain a Bing API key for all this ?? please help me as soon as possible..
    Monday, July 30, 2012 3:42 PM

Answers

All replies

  • You can use the WPF control in Win Forms. Take a look at this blog post: http://rbrundritt.wordpress.com/2012/01/05/using-bing-maps-in-winforms/

    You can create a Bing Maps Account and create a Bing maps key using the following documentation:

    http://msdn.microsoft.com/en-us/library/gg650598.aspx

    http://msdn.microsoft.com/en-us/library/ff428642.aspx


    http://rbrundritt.wordpress.com


    Monday, July 30, 2012 5:20 PM
  • Thank you so much sir.. but i have got one more doubt what are the url's and app name i am supposed to give .. and how to integrate those urls in the Windows Form Application ??? 
    Tuesday, July 31, 2012 11:21 AM
  • The WPF control can be downloaded here: http://www.microsoft.com/en-us/download/details.aspx?displaylang=en&id=27165 The blog post I provided above shows you how to use this in a WinForm application. If you want to do geocoding and routing in WinForm then take a look at this blog post: http://rbrundritt.wordpress.com/2012/01/06/bing-maps-rest-service-net-libraries/

    http://rbrundritt.wordpress.com

    Tuesday, July 31, 2012 11:53 AM
  • Thanks a Ton sir :) If i have more doubts I will ask you..
    Tuesday, July 31, 2012 12:12 PM
  • Sir I am still not getting an account of how to create the keys... and where to apply those keys in the xaml code...??? I am just getting a Blank form after debugging 

    Tuesday, July 31, 2012 5:11 PM
  • To create a Bing Maps key log into the Bing Maps portal at http://bingmapsportal.com Once logged in click on the "Create or view keys" link on the left side panel. Fill in the form and press submit. Afterwards you will find your Bing Maps key listed at the bottom of that page. you can use this in the xaml in the map reference like so:

    <m:Map CredentialsProvider="YOUR_BING_MAPS_KEY"/>


    http://rbrundritt.wordpress.com

    Wednesday, August 01, 2012 10:15 AM
  • form1.designer.cs
    namespace BingMapsWinForm
    {
        partial class Form1
        {
            /// <summary>
            /// Required designer variable.
            /// </summary>
            private System.ComponentModel.IContainer components = null;

            /// <summary>
            /// Clean up any resources being used.
            /// </summary>
            /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
            protected override void Dispose(bool disposing)
            {
                if (disposing && (components != null))
                {
                    components.Dispose();
                }
                base.Dispose(disposing);
            }

            #region Windows Form Designer generated code

            /// <summary>
            /// Required method for Designer support - do not modify
            /// the contents of this method with the code editor.
            /// </summary>
            private void InitializeComponent()
            {
                this.elementHost1 = new System.Windows.Forms.Integration.ElementHost();
                this.MyMapUserControl = new BingMapsWinForm.MapUserControl();
                this.SuspendLayout();
                // 
                // elementHost1
                // 
                this.elementHost1.Location = new System.Drawing.Point(12, 12);
                this.elementHost1.Name = "elementHost1";
                this.elementHost1.Size = new System.Drawing.Size(675, 426);
                this.elementHost1.TabIndex = 2;
                this.elementHost1.Text = "elementHost1";
                this.elementHost1.Child = this.MyMapUserControl;
                // 
                // Form1
                // 
                this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
                this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
                this.ClientSize = new System.Drawing.Size(699, 450);
                this.Controls.Add(this.elementHost1);
                this.Name = "Form1";
                this.Text = "Bing Maps WinForm";
                this.ResumeLayout(false);

            }

            #endregion

            private System.Windows.Forms.Integration.ElementHost elementHost1;
            private MapUserControl MyMapUserControl;
        }
    }



    UserControl.xaml
    <UserControl x:Class="projet1.UserControl3"
                 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
                 xmlns:d="http://schemas.microsoft.com/expression/blend/2008" Height="300" Width="300">
        <Grid>
            
        </Grid>
    </UserControl>



    MapUserControl.xaml.cs
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    using System.Windows.Navigation;
    using System.Windows.Shapes;

    namespace BingMapsWinForm
    {
        /// <summary>
        /// Interaction logic for MapUserControl.xaml
        /// </summary>
        public partial class MapUserControl : UserControl
        {
            public MapUserControl()
            {
                InitializeComponent();
            }
        }
    }


    form4.cs
    sing System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;

    namespace projet1
    {
        public partial class Form4 : Form
        {
            public Form4()
            {
                InitializeComponent();
            }
        }
    }



    Error 1 'projet1.Form3' does not contain a definition for 'elementHost1_ChildChanged' and no extension method 'elementHost1_ChildChanged' accepting a first argument of type 'projet1.Form3' could be found (are you missing a using directive or an assembly reference?) C:\Users\SuryaPraba\Desktop\project1\project1\Form3.Designer.cs 43 132 project1
    Error 2 The name 'InitializeComponent' does not exist in the current context C:\Users\SuryaPraba\Desktop\project1\project1\UserControl1.xaml.cs 24 13 project1



     <m:Map x:Name="myMap" CredentialsProvider="INSERT_YOUR_BING_MAPS_KEY" Mode="Aerial"/>
    I understood that i have to use this in the grid but please tell me how because its giving me error saying


    Error 3 The type 'm:Map' was not found. Verify that you are not missing an assembly reference and that all referenced assemblies have been built. C:\Users\SuryaPraba\Desktop\project1\project1\UserControl3.xaml 7 10 project1


    Please help me in completing this code as i am supposed to submit my project tomo night .. I am very new to C# .. I have very less knowledge about it... Please Please Please.. 
    Wednesday, August 01, 2012 8:31 PM
  • You need to add a reference to the WPF Bing Maps control in your project and then add that reference in your xaml. All of this is documented in the blog post I provided in the first reply.

    http://rbrundritt.wordpress.com

    Thursday, August 02, 2012 8:05 AM
  • Sir Is there any way to call the different services like Zoom in , Zoom out, Pan in any Direction, Aerial,Road view, labels etc using buttons of the Windows form application?? This is more like a web browser program.. I want to do it using buttons ..
    Friday, August 03, 2012 3:29 PM
  • Yes. You simply need to get a reference the map control from your code behind and then call the SetView method or set the map mode property to the map style you want. Take a look at this documentation: http://msdn.microsoft.com/en-us/library/microsoft.maps.mapcontrol.wpf.map.aspx

    http://msdn.microsoft.com/en-us/library/hh709343.aspx


    http://rbrundritt.wordpress.com

    Saturday, August 04, 2012 1:01 PM
  • <UserControl x:Class="WindowsFormsApplication1.BingUserControl1"
                 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
                 xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
                 xmlns:m="clr-namespace:Microsoft.Maps.MapControl.WPF;assembly=Microsoft.Maps.MapControl.WPF" Name="MapUserControl" mc:Ignorable="d" d:DesignHeight="300" d:DesignWidth="300" DataContext="{Binding}">
                 
                
        <Grid>
            <m:Map Mode="Road"  CredentialsProvider="AvDLouIOx6ofBxn6Z5ZYFrdu7oVZEVVxJTxi0a6Fuqy1dM_7jVOydN2xtIVvdUCP"  Grid.Column="0" Grid.Row="0" />
            <m:Map Mode="Aerial"  CredentialsProvider="AvDLouIOx6ofBxn6Z5ZYFrdu7oVZEVVxJTxi0a6Fuqy1dM_7jVOydN2xtIVvdUCP" Grid.Column="1" Grid.Row="0" />
            <m:Map Mode="AerialWithLabels"  CredentialsProvider="AvDLouIOx6ofBxn6Z5ZYFrdu7oVZEVVxJTxi0a6Fuqy1dM_7jVOydN2xtIVvdUCP" Grid.Column="0" Grid.Row="1" />
            <m:Map CredentialsProvider="AvDLouIOx6ofBxn6Z5ZYFrdu7oVZEVVxJTxi0a6Fuqy1dM_7jVOydN2xtIVvdUCP" Mode="AerialWithLabels" Center="37.806029,-122.407007" ZoomLevel="16"/>
            
        </Grid>
    </UserControl>


    Please help me how to switch over the map types using buttons... I have created 8 buttons open,zoom In,Zoom out,search box,pan in any direction,aerial view ,road view and labels.. along with a map on a canvas.. i used the bing api key and got the map in the form.. but i want to do all these 8 functions using button only.. how to do it ?? please help me..
    Monday, August 06, 2012 4:14 PM
  • using System;
    using System.Configuration;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Windows.Forms.Integration;
    using WindowsFormsApplication1.SearchService;
    using Microsoft.Maps.MapControl.WPF;
    using System.Text.RegularExpressions;

    private string SearchKeywordLocation(string keywordLocation)
            {
                String results = "";
                String key = "AvDLouIOx6ofBxn6Z5ZYFrdu7oVZEVVxJTxi0a6Fuqy1dM_7jVOydN2xtIVvdUCP";
                SearchRequest searchRequest = new SearchRequest();

                // Set the credentials using a valid Bing Maps key
                searchRequest.Credentials = new SearchService.Credentials();
                searchRequest.Credentials.ApplicationId = key;

                //Create the search query
                StructuredSearchQuery ssQuery = new StructuredSearchQuery();
                string[] parts = keywordLocation.Split(';');
                ssQuery.Keyword = parts[0];
                ssQuery.Location = parts[1];
                searchRequest.StructuredQuery = ssQuery;

                //Define options on the search
                searchRequest.SearchOptions = new SearchOptions();
                searchRequest.SearchOptions.Filters =
                    new FilterExpression()
                    {
                        PropertyId = 3,
                        CompareOperator = CompareOperator.GreaterThanOrEquals,
                        FilterValue = 8.16
                    };

                //Make the search request 
                SearchServiceClient searchService = new SearchServiceClient("BasicHttpBinding_ISearchService");
                SearchResponse searchResponse = searchService.Search(searchRequest);

                //Parse and format results
                if (searchResponse.ResultSets[0].Results.Length > 0)
                {
                    StringBuilder resultList = new StringBuilder("");
                    for (int i = 0; i < searchResponse.ResultSets[0].Results.Length; i++)
                    {
                        resultList.Append(String.Format("{0}. {1}\n", i + 1,
                            searchResponse.ResultSets[0].Results[i].Name));
                    }

                    results = MyMapUserControl.ToString();
                }
                else
                    results = "No results found";

                return results;
            }

        }

    Error:

    Error 4 The type or namespace name 'Credentials' does not exist in the namespace 'WindowsFormsApplication1.SearchService' (are you missing an assembly reference?) C:\Downloads\WindowsFormsApplication1 (2)\WindowsFormsApplication1\WindowsFormsApplication1\Form1.cs 96 59 MiniBingMapsProject

        
    Monday, August 13, 2012 10:07 AM