Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
155
The new newbie's question for xamMap
posted

HI All,

This is my second day get touch silverlight and XamMap, Here is what I am looking for :

I am receiving my trucks' latitude and longitude every 15 mins. I need to show it on a road level maps.

can I do it with xamMap ?

How can I do it ?

Your help is very appreciated.

Ray

 

 

 

  • 30692
    Suggested Answer
    Offline posted

    Hi,
    The best way to go about this might be to use a geo imagery layer and put a symbol layer on top of it to represent the trucks.
    You can find information here: http://help.infragistics.com/NetAdvantage/DV/2010.2/CLR4.0/?page=xamWebMap_Geo-Imagery_Integration.html
    about how to configure the various geo imagery options of the xamMap.

    Below is a small example that moves a truck dot around randomly on top of the geo-imagery.
    You will notice the one unfriendly part of this sample is that the 2 map layers don't pan at the same rate when you are panning the map using a mouse drag.
    I believe keeping these pans synchronized is something we are planning on adjusting soon.

    This sample uses OpenStreetMaps, but the principle is that same for the other imagery sources:
    The xaml:

            <igMap:XamMap x:Name="theMap" >
                <igMap:XamMap.MapTileSource >
                    <igMap:OpenStreetMapTileSource/>
                </igMap:XamMap.MapTileSource>
                
                <igMap:XamMap.Layers>
                    <igMap:MapLayer x:Name="trucks"/>
                </igMap:XamMap.Layers>
            </igMap:XamMap>
    


    And the code behind:

    public partial class MainPage : UserControl
        {
            DispatcherTimer _timer = new DispatcherTimer();
            TruckEmulator _emul = new TruckEmulator();
    
            public MainPage()
            {
                InitializeComponent();
    
                _timer.Interval = TimeSpan.FromSeconds(.5);
                _timer.Tick += Timer_Tick;
                _timer.Start();
                this.Loaded += new RoutedEventHandler(MainPage_Loaded);
            }
    
            void MainPage_Loaded(object sender, RoutedEventArgs e)
            {
                InitMapCoordinates();
            }
    
            void Timer_Tick(object sender, EventArgs e)
            {
                double x;
                double y;
                _emul.GetCurrentCoordinates(out x, out y);
    
                MapLayer truckLayer = theMap.Layers[0];
                if (truckLayer.WorldRect.IsEmpty)
                {
                    truckLayer.WorldRect = theMap.WorldRect;
                }
                SymbolElement ele =
                    truckLayer.Elements.OfType<SymbolElement>()
                    .FirstOrDefault();
    
                if (ele == null)
                {
                    ele = new SymbolElement();
                    ele.SymbolSize = 5;
                    ele.SymbolType = MapSymbolType.Bubble;
                }
    
                ele.SymbolOrigin = 
                    theMap.MapProjection
                    .ProjectToMap(new Point(x, y));
    
                if (!truckLayer.Elements.Contains(ele))
                {
                    truckLayer.Elements.Add(ele);
                }
            }
    
            private void InitMapCoordinates()
            {
                // define world dimensions 
                Point worldTopLeft = new Point(-180, 90);
                Point worldBottomRight = new Point(180, -90);
    
                // Convert Geodetic to Cartesian coordinates
                Point winTopLeft = theMap.MapProjection.ProjectToMap(worldTopLeft);
                Point winBottomRight = theMap.MapProjection.ProjectToMap(worldBottomRight);
                // Create Rect structure the map control's WindowRect and WorldRect
                Rect winRect = new Rect()
                {
                    X = Math.Min(winTopLeft.X, winBottomRight.X),
                    Y = Math.Min(winTopLeft.Y, winBottomRight.Y),
                    Width = Math.Abs(winTopLeft.X - winBottomRight.X),
                    Height = Math.Abs(winTopLeft.Y - winBottomRight.Y)
                };
                theMap.IsAutoWorldRect = false;
                theMap.WindowZoomMaximum = 80;
    
                // Change the map control's WindowRect and WorldRect
                theMap.WindowRect = theMap.WorldRect = winRect;
            }
        }
    
    
    
        public class TruckEmulator
        {
            private double _currentLat = 40.29029630058813;
            private double _currentLong = -74.55051898956299;
            public void GetCurrentCoordinates(out double x, out double y)
            {
                RandomlyMove();
                x = _currentLong;
                y = _currentLat;
            }
    
            private Random _rand = new Random();
    
            private void RandomlyMove()
            {
                if (_rand.NextDouble() > .5)
                {
                    _currentLat += _rand.NextDouble() * .001;
                }
                else
                {
                    _currentLat -= _rand.NextDouble() * .001;
                }
    
                if (_rand.NextDouble() > .5)
                {
                    _currentLong += _rand.NextDouble() * .001;
                }
                else
                {
                    _currentLong -= _rand.NextDouble() * .001;
                }
            }
        }
    


    Hope this helps!

    -Graham