none
Adding waypoint after getting from database RRS feed

  • Question

  • This program aims to get data from the server. As soon as data are available, it will create a waypoint and shows the route a vehicle is taking. However, it seems that the code below is not working.

    directionsManager = new Microsoft.Maps.Directions.DirectionsManager(map);

    Full code below:

    asp.net:

    <script type='text/javascript' src='http://ecn.dev.virtualearth.net/mapcontrol/mapcontrol.ashx?v=7.0'></script>
        <script type='text/javascript'>
            var map = null;
            var pushPinEntity = new Microsoft.Maps.EntityCollection();
            var directionsManager = null;
            function GetMap() {
                map = new Microsoft.Maps.Map(document.getElementById("mapDiv"),
                    { credentials: "credentials",
                        center: new Microsoft.Maps.Location(1.328301, 103.824345),
                        mapTypeId: Microsoft.Maps.MapTypeId.road,
                        zoom: 12
                    });
                map.entities.push(pushPinEntity);
                Microsoft.Maps.loadModule('Microsoft.Maps.Directions');
                directionsManager = new Microsoft.Maps.Directions.DirectionsManager(map);
                directionsManager.resetDirections();
                directionsManager.setRenderOptions({ itineraryContainer: document.getElementById('itineraryDiv'),
                    waypointPushpinOptions: { visible: false },
                    viapointPushpinOptions: { visible: false }
                });
                directionsManager.setRequestOptions({
                    routeMode: Microsoft.Maps.Directions.RouteMode.driving,
                    routeDraggable: false
                });
            }
            
            // Update push pins on map
            function UpdatePushPin(x, y) {
                pushPinEntity.clear();
                var pushpin = new Microsoft.Maps.Pushpin(new Microsoft.Maps.Location(x, y));
                pushPinEntity.push(pushpin);
            }
    
            // Add Waypoints
            function addPoints(x, y) {
                var startWP = new Microsoft.Maps.Directions.Waypoint({ location: new Microsoft.Maps.Location(x, y) });
                alert("Parsin works");
                directionsManager.addWaypoint(startWP);
            }
    
            // Draw the route
            function calDir() {
                directionsManager.calculateDirections();
            }
    
        </script>

    C#:

    protected void GetLocation(object sender, EventArgs e)
        {
            DataTable tblLocation = new DataTable();
            string query = "SELECT Latitude, Longitude FROM Location ";
            if (textbox1.Text != "")
            {
                textbox1.Text = Convert.ToString(Convert.ToInt32(textbox1.Text) + 1);
            }
            else
            {
                textbox1.Text = "1";
            }
            query += " WHERE num = " + textbox1.Text;
            SqlDataAdapter adapter = new SqlDataAdapter();
            string connString = ConfigurationManager.ConnectionStrings["ConString"].ConnectionString;
            SqlConnection conn = new SqlConnection(connString);
            conn.Open();
            adapter.SelectCommand = new SqlCommand(query, conn);
            adapter.Fill(tblLocation);
            conn.Close();
    
            if (tblLocation.Rows.Count > 0)
            {
                longitude.Text = tblLocation.Rows[0]["Longitude"].ToString();
                latitude.Text = tblLocation.Rows[0]["Latitude"].ToString();
                string addwp = "addPoints("+ latitude.Text + "," + longitude.Text + ");";
                if (Convert.ToInt32(textbox1.Text) > 1)
                {
                    addwp += "calDir()";
                }
                string script = "UpdatePushPin(" + latitude.Text + "," + longitude.Text + ");" + addwp;
                ScriptManager.RegisterStartupScript(this, this.GetType(), "Key", script, true);
            }
            else
            {
                textbox1.Text = Convert.ToString(Convert.ToInt32(textbox1.Text) - 1);
            }
        }

    Wednesday, July 16, 2014 8:54 AM

Answers

  • Before diving into an answer, is this going to be used to show the real-time position of vehicles as they move? If so it should be noted that asset tracking applications can only be licensed on Bing Maps if they are developed on a pure Windows Platform such as WPF, WinForm, Win 8 app, WP app. This is noted in the terms of use and you can also find this by using the licensing tool here: http://www.microsoft.com/maps/Licensing/licensing.aspxWeb apps using AJAX, Silverlight, Flash cannot be licensed for commercial asset tracking. Note that if this app is showing historical routes this can be licensed, or showing a single route a user might be following (i.e. showing me the path I've taken on my phone) then this would fall under consumer tracking which can be licensed.

    All that licensing stuff out of the way, here is your issue. You are only adding one Waypoint to the directions manager. You need a minimum of two points to calculate a route.


    http://rbrundritt.wordpress.com

    Wednesday, July 16, 2014 3:13 PM
  • Ok, three things to test. First, verify that x & y are numbers and not a number stored as a string. If it is a string then use parseFloat(x), parseFloat(y) to convert the string to a number. Next check that the values of x are between -90 and 90, and y are between -180 and 180.

    Lastly, check to see if Microsoft.Maps.Directions is even available. Its possible that you are trying to use this namespace before the module has even had a chance to load. Note that you can add a callback to the loadModule method that will fire when the module has been loaded.


    http://rbrundritt.wordpress.com

    Thursday, July 17, 2014 10:32 AM

All replies

  • Before diving into an answer, is this going to be used to show the real-time position of vehicles as they move? If so it should be noted that asset tracking applications can only be licensed on Bing Maps if they are developed on a pure Windows Platform such as WPF, WinForm, Win 8 app, WP app. This is noted in the terms of use and you can also find this by using the licensing tool here: http://www.microsoft.com/maps/Licensing/licensing.aspxWeb apps using AJAX, Silverlight, Flash cannot be licensed for commercial asset tracking. Note that if this app is showing historical routes this can be licensed, or showing a single route a user might be following (i.e. showing me the path I've taken on my phone) then this would fall under consumer tracking which can be licensed.

    All that licensing stuff out of the way, here is your issue. You are only adding one Waypoint to the directions manager. You need a minimum of two points to calculate a route.


    http://rbrundritt.wordpress.com

    Wednesday, July 16, 2014 3:13 PM
  • Hi Ricky,

    we are still looking for options before settling for Bing Maps. Currently, we are testing all available Maps API before acquiring any license. For the above code, we are not tracking any assets yet. All values used are created by a random generator.

    Secondly,

    var startWP = new Microsoft.Maps.Directions.Waypoint({ location: new Microsoft.Maps.Location(x, y) });
    always return a null while debugging with firebug. I'm not to sure what is the problem. Even when I'm trying to input 2 different waypoints, it still returns a null and shows a type error exception.


    • Edited by andikirman Thursday, July 17, 2014 1:43 AM
    Thursday, July 17, 2014 1:39 AM
  • Ok, three things to test. First, verify that x & y are numbers and not a number stored as a string. If it is a string then use parseFloat(x), parseFloat(y) to convert the string to a number. Next check that the values of x are between -90 and 90, and y are between -180 and 180.

    Lastly, check to see if Microsoft.Maps.Directions is even available. Its possible that you are trying to use this namespace before the module has even had a chance to load. Note that you can add a callback to the loadModule method that will fire when the module has been loaded.


    http://rbrundritt.wordpress.com

    Thursday, July 17, 2014 10:32 AM