簡體   English   中英

WPF Bing Maps圖釘始終從鼠標位置着陸

[英]WPF Bing Maps pushpin consistently landing away from the mouse position

我正在嘗試在地圖上放一個文件,然后將其放置在圖釘上,並將路徑附加到圖釘上(這樣,我可以單擊圖釘,然后轉到Windows資源管理器中的路徑)。

但是,該銷釘似乎落在了Grid.Column(0)映射圖標下,盡管這里沒有進一步討論,但這里也提到了該圖標。

如果該應用程序在不同的顯示器上,則該圖釘似乎就落在右側視圖的外面。

我注意到的另一件事是,無論我將文件放在何處,除非在嘗試之間使窗口處於混亂狀態,否則坐標似乎都是相同的。

假設問題來自grid.column,我該怎么解決呢?

像這樣: pt.x += worldMap.width/2

這是我的窗口: 窗口布局

這是我的fileDidDrop函數:

        private void File_Drop(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.FileDrop))
            {
                string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);

                // File dir
                Console.WriteLine(files[0].ToString());

                // Get mouse coords
                Point pt = Mouse.GetPosition(worldMap);

                Location dropLoc = worldMap.ViewportPointToLocation(pt);

                new Location(pt.X,pt.Y);

                GeoPushpin newPin = new GeoPushpin();
                newPin.Location = dropLoc;
                worldMap.Children.Add(newPin);
            }
        }

這是我的worldMap的XAML

            <m:Map x:Name="worldMap" 
                   CredentialsProvider="MY_KEY" 
                   Mode="Road"  
                   Grid.Column="1" 
                   ZoomLevel="4" 
                   Center="-27.608,134.8099"
                   Drop="File_Drop" 
                   AllowDrop="True">
            </m:Map>

助教。

我猜就像他們在鏈接中提到的那樣,這可能是自定義圖釘的問題,您可以嘗試使用僅從地圖繼承的控件(如地圖多邊形),並且可以在其中繪制圖釘。

這是我想出的解決方案。 我注意到該引腳始終會向擴展顯示器的原點偏移。

這意味着我們可以通過鼠標相對於屏幕的位置來調整pt並解決問題。

private void File_Drop(object sender, DragEventArgs e)
    {
        if (e.Data.GetDataPresent(DataFormats.FileDrop))
        {
            string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);

            // File dir
            Console.WriteLine(files[0].ToString());

            // Get mouse coords
            Point mouse = PointToScreen(e.GetPosition(this));
            Point pt = Mouse.GetPosition(worldMap);

            Point adjustedPt = new Point();
            adjustedPt.X = pt.X + mouse.X;
            adjustedPt.Y = pt.Y + mouse.Y;

            Location dropLoc = worldMap.ViewportPointToLocation(adjustedPt);

            GeoPushpin newPin = new GeoPushpin();
            newPin.Location = dropLoc;
            worldMap.Children.Add(newPin);
        }
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM