none
Get a users latitude and longitude from geoLocationProvider.getCurrentPosition() function RRS feed

  • Question

  • I am using the geoLocationProvider.getCurrentPosition() on my map control to default the Bing map to my users location. I would like to know how I can get the latitude, and longitude from this function to pass to a stored procedure in my database. I am very new to geocoding apps, but, I believe that I am almost there. Once I learn how to pull the Lat and Long from the getCurrentPosition() method, I can use the values to pass to my stored procedure to get locations from my Database that are within a 20 mile radius. Any help would be very much appreciated.


    mark a fisher

    Wednesday, March 13, 2013 8:19 PM

Answers

  • In the getCurrentPosition function you can pass in some properties. One of them can be a callback that gets called after the users location is found. You can then get the users location. Take a look at this documentation: http://msdn.microsoft.com/en-us/library/hh125839.aspx

    As for getting nearby locations in your database. If you are using SQL Server 2008 or above you can store your location data as a spatial Geography object. If you do this then you would be able to use the spatial search functionality to do your radial search. In your store procedure you would take the users location and create a buffer on it for the radius you want in meters. And then do an intersection test against your Geography column of data. Here are some useful docs on the topic:

    http://technet.microsoft.com/en-us/library/bb933811.aspx

    http://technet.microsoft.com/en-us/library/bb933965.aspx

    http://technet.microsoft.com/en-us/library/bb933962.aspx

    If using Entity Framework 5 you may find this useful:

    http://rbrundritt.wordpress.com/2012/06/08/entity-framework-5-bing-maps-wpf/


    http://rbrundritt.wordpress.com

    • Proposed as answer by Ricky_Brundritt Thursday, March 14, 2013 11:48 AM
    • Marked as answer by MarkF67 Thursday, March 14, 2013 3:09 PM
    Thursday, March 14, 2013 11:48 AM
  • I looked at the link, and I'm not sure how to assign the +args.center to a lable in my aspx page, from my .js file??

    I have ;

    geoLocationProvider.getCurrentPosition({successCallback:displayCenter});

    function displayCenter(args) {

    //Display the users location when the goe location request returns

     alert("The user's location is"+ args.center)}

    Can I do something like this:

    lblLatLong.Text = args.Center;

       


      


    mark a fisher


    • Edited by MarkF67 Thursday, March 14, 2013 3:22 PM
    • Marked as answer by MarkF67 Thursday, March 14, 2013 10:40 PM
    Thursday, March 14, 2013 3:22 PM
  • Actually, I would like to update this thread with the solution that I put together. I have tested it and it works fine.

    var map = null;
    var mapOtions;

    var geoLocationProvider;
    var startingZoom = 10;

    function GetMap() {

        // Set the default page map and view settings
        mapOtions = {
            credentials: credentials, 
            mapTypeId: Microsoft.Maps.MapTypeId.road,
            animate: false,
            zoom: startingZoom,
            center: new Microsoft.Maps.Location(geoLocationProvider) // geoLocationProvider is my custom view
        };

        // Initialize the map
        map = new Microsoft.Maps.Map(document.getElementById("myMap"), mapOtions);

        geoLocationProvider = new Microsoft.Maps.GeoLocationProvider(map); // Initialize the location provider 
        geoLocationProvider.getCurrentPosition({ successCallback: displayCenter })// Get the user's current location by calling the displayCenter function


    }
    function displayCenter(args) {
        //Display the users location when the goe location request returns
        alert("May we use your Location?")
        var locationPoint1 = args.center.latitude;//assign to a var
        var locationPoint2 = args.center.longitude;//assign to a var
        var lat = document.getElementById("hdnLatitude").value = locationPoint1;//initialize the asp:HiddenFields value with the latitude and longitude
        var lng = document.getElementById("hdnLongitude").value = locationPoint2;
        map.entities.clear();
        var pushpin = new Microsoft.Maps.Pushpin(map.getCenter(), null);
        map.entities.push(pushpin);
        pushpin.setLocation(new Microsoft.Maps.Location(locationPoint1, locationPoint2));//add a pushpin to the location on the map

    }

     I want to thank Richard for pointing me in the right direction with the links above, and I also want to thank him for his great Bing Maps Blog that I suggest to all who want to learn more about using mapping capabilities in their applications. 

    mark a fisher


    • Marked as answer by MarkF67 Wednesday, April 10, 2013 7:19 PM
    • Edited by MarkF67 Wednesday, April 10, 2013 7:19 PM
    Wednesday, April 10, 2013 7:19 PM

All replies

  • In the getCurrentPosition function you can pass in some properties. One of them can be a callback that gets called after the users location is found. You can then get the users location. Take a look at this documentation: http://msdn.microsoft.com/en-us/library/hh125839.aspx

    As for getting nearby locations in your database. If you are using SQL Server 2008 or above you can store your location data as a spatial Geography object. If you do this then you would be able to use the spatial search functionality to do your radial search. In your store procedure you would take the users location and create a buffer on it for the radius you want in meters. And then do an intersection test against your Geography column of data. Here are some useful docs on the topic:

    http://technet.microsoft.com/en-us/library/bb933811.aspx

    http://technet.microsoft.com/en-us/library/bb933965.aspx

    http://technet.microsoft.com/en-us/library/bb933962.aspx

    If using Entity Framework 5 you may find this useful:

    http://rbrundritt.wordpress.com/2012/06/08/entity-framework-5-bing-maps-wpf/


    http://rbrundritt.wordpress.com

    • Proposed as answer by Ricky_Brundritt Thursday, March 14, 2013 11:48 AM
    • Marked as answer by MarkF67 Thursday, March 14, 2013 3:09 PM
    Thursday, March 14, 2013 11:48 AM
  • I looked at the link, and I'm not sure how to assign the +args.center to a lable in my aspx page, from my .js file??

    I have ;

    geoLocationProvider.getCurrentPosition({successCallback:displayCenter});

    function displayCenter(args) {

    //Display the users location when the goe location request returns

     alert("The user's location is"+ args.center)}

    Can I do something like this:

    lblLatLong.Text = args.Center;

       


      


    mark a fisher


    • Edited by MarkF67 Thursday, March 14, 2013 3:22 PM
    • Marked as answer by MarkF67 Thursday, March 14, 2013 10:40 PM
    Thursday, March 14, 2013 3:22 PM
  • Try something like this: lblLatLong.Text = args.Center.latitude + ", " + args.Center.longitude;

    http://rbrundritt.wordpress.com

    Thursday, March 14, 2013 8:03 PM
  • Great, that will worked for me.Thanks again for the links earlier, thay are very helpful and informative.

    mark a fisher

    Thursday, March 14, 2013 10:40 PM
  • Actually, I jumped the gun. I cannot access the values. My code behind is saying that the name 'args' does not exist. I am trying to pass the users current location

    (args.Center.latitude, args.Center.longitude, 10)into this method in my code behind in C#;

    GetHealthCenters(args.Center.latitude, args.Center.longitude, 10 );//the last value is an "int" for distance that my stored procedure uses to get the nearest locations based on the lat and long passsed on to it.

    When my page loads, the map defaults to the users current location. I use the function I described earlier;

    geoLocationProvider.getCurrentPosition({successCallback:displayCenter});

    function displayCenter(args) {

    //Display the users location when the goe location request returns

     alert("The user's location is"+ args.center)}

    But, I am lost as to how to pass these variables to my method. I am really new to javascript.

     

    mark a fisher


    • Edited by MarkF67 Friday, March 15, 2013 3:56 PM
    Friday, March 15, 2013 3:56 PM
  • Actually, I would like to update this thread with the solution that I put together. I have tested it and it works fine.

    var map = null;
    var mapOtions;

    var geoLocationProvider;
    var startingZoom = 10;

    function GetMap() {

        // Set the default page map and view settings
        mapOtions = {
            credentials: credentials, 
            mapTypeId: Microsoft.Maps.MapTypeId.road,
            animate: false,
            zoom: startingZoom,
            center: new Microsoft.Maps.Location(geoLocationProvider) // geoLocationProvider is my custom view
        };

        // Initialize the map
        map = new Microsoft.Maps.Map(document.getElementById("myMap"), mapOtions);

        geoLocationProvider = new Microsoft.Maps.GeoLocationProvider(map); // Initialize the location provider 
        geoLocationProvider.getCurrentPosition({ successCallback: displayCenter })// Get the user's current location by calling the displayCenter function


    }
    function displayCenter(args) {
        //Display the users location when the goe location request returns
        alert("May we use your Location?")
        var locationPoint1 = args.center.latitude;//assign to a var
        var locationPoint2 = args.center.longitude;//assign to a var
        var lat = document.getElementById("hdnLatitude").value = locationPoint1;//initialize the asp:HiddenFields value with the latitude and longitude
        var lng = document.getElementById("hdnLongitude").value = locationPoint2;
        map.entities.clear();
        var pushpin = new Microsoft.Maps.Pushpin(map.getCenter(), null);
        map.entities.push(pushpin);
        pushpin.setLocation(new Microsoft.Maps.Location(locationPoint1, locationPoint2));//add a pushpin to the location on the map

    }

     I want to thank Richard for pointing me in the right direction with the links above, and I also want to thank him for his great Bing Maps Blog that I suggest to all who want to learn more about using mapping capabilities in their applications. 

    mark a fisher


    • Marked as answer by MarkF67 Wednesday, April 10, 2013 7:19 PM
    • Edited by MarkF67 Wednesday, April 10, 2013 7:19 PM
    Wednesday, April 10, 2013 7:19 PM