none
How to get the coordinates of the center RRS feed

  • Question

  • Hey there,

    I try to create a usual Windows Form in C# that contains the Bing Maps. So far it works, it's no problem to look around the world and so on. But now I want to get the coordinates of the current center and display it in a label for example. In the interactive SDK I found the JavaScript function:

    function getCenter()
          {
            var latlon = map.getCenter();
            alert('Lat/Long: ' + latlon.latitude +'/' + latlon.longitude);
          }

    I guess that's it what I need. The problem is, how to call the map.getCenter() function from C# Code? Or can I get the information with a server request.

    Looking forward your answers!

    Monday, November 26, 2012 3:32 PM

Answers

All replies

  • Which map control are you using? If you are targeting Windows Forms you should use the WPF control in a WPF Host Element as documented here: http://rbrundritt.wordpress.com/2012/01/05/using-bing-maps-in-winforms/ If you are using this approach then you can get the center information from the center property of the map: http://msdn.microsoft.com/en-us/library/microsoft.maps.mapcontrol.wpf.map_properties.aspx

    http://rbrundritt.wordpress.com

    Monday, November 26, 2012 4:59 PM
  • You can pass commands from WinForms to a hosted Javascript control via the Document.InvokeScript() method of the WebBrowser object - http://msdn.microsoft.com/en-us/library/be9zzz62.aspx

    So, assuming webBrowser is your instance of the WebBrowser control, you'd call: webBrowser.Document.InvokeScript("getCenter");


    twitter: @alastaira blog: http://alastaira.wordpress.com/ | Pro Spatial with SQL Server 2012

    • Marked as answer by Ricky_Brundritt Wednesday, November 28, 2012 10:07 AM
    • Unmarked as answer by AndyFTW Thursday, November 29, 2012 3:48 PM
    Monday, November 26, 2012 5:04 PM
    Moderator
  • Thank you very much. I will test it on Thursday and tell you if it worked!

    Tuesday, November 27, 2012 10:22 PM
  • On more Question. From where get I the Microsoft.Maps reference? I searched but didn't find anything. Can you help me?
    Tuesday, November 27, 2012 10:27 PM
  • Microsoft.Maps is the namespace for all the items in the AJAX v7 library http://ecn.dev.virtualearth.net/mapcontrol/mapcontrol.ashx?v=7.0

    twitter: @alastaira blog: http://alastaira.wordpress.com/ | Pro Spatial with SQL Server 2012

    • Marked as answer by Ricky_Brundritt Wednesday, November 28, 2012 10:07 AM
    • Unmarked as answer by AndyFTW Thursday, November 29, 2012 3:48 PM
    Tuesday, November 27, 2012 10:51 PM
    Moderator
  • I tried it but when I try to compile the program it says "The name 'InitializeComponent' doesn't exist in the current context'.

    I get this error in MapUserControl.xaml.cs

    In MapUserControl.xaml I already can see the bing map, that means so far it works, but I can't compile it. But why I get this error? Went through the "How To" often now but didn't find the source of the error.

    Edit: Fixed it! Was a problem with namespaces. But works now!

    Edit2: Great! I can get the center property now. But how can I set that the mode of the map is from the beginning Aerial? Can't find the parameter and don't know how to set. Thank you

    Edit3: Got it. It's:

    Map.Mode = new AerialMode(true);
    So, thank you very much, helped me a lot!

    • Edited by AndyFTW Thursday, November 29, 2012 9:24 AM
    Wednesday, November 28, 2012 4:41 PM