2014-03-01 51 views
2

我真的想在WPF中創建自己的地圖控件,因爲唯一適合我需求的地方就是Google Maps JavaScript API,因爲它可以完成我所需要的任何操作,僅適用於網絡和移動設備,我嘗試過Bing和ESRI地圖,但他們無法做到我想要的。從零開始創建一個簡單的WPF地圖控件

我已經開始了一個小實驗項目,看是否縮放時我可以加載瓷磚和它種作品,這裏是代碼:

<ScrollViewer Margin="10" PanningMode="Both" HorizontalScrollBarVisibility="Visible"> 
     <Canvas x:Name="lyrTiles" Height="3000" Width="3000"/> 
    </ScrollViewer> 

    <Grid x:Name="lyrControl" Margin="10"> 
     <Button x:Name="moveUp" Content="U" HorizontalAlignment="Left" Margin="35,10,0,0" VerticalAlignment="Top" Width="20" Height="20"/> 
     <Button x:Name="moveRight" Content="R" HorizontalAlignment="Left" Margin="55,30,0,0" VerticalAlignment="Top" Width="20" Height="20"/> 
     <Button x:Name="moveDown" Content="D" HorizontalAlignment="Left" Margin="35,50,0,0" VerticalAlignment="Top" Width="20" Height="20"/> 
     <Button x:Name="moveLeft" Content="L" HorizontalAlignment="Left" Margin="15,30,0,0" VerticalAlignment="Top" Width="20" Height="20"/> 
     <Button x:Name="zoomIn" Content="ZI" HorizontalAlignment="Left" Margin="35,81,0,0" VerticalAlignment="Top" Width="20" Height="20"/> 
     <Button x:Name="zoomOut" Content="ZO" HorizontalAlignment="Left" Margin="35,311,0,0" VerticalAlignment="Top" Width="20" Height="20"/> 
     <Slider x:Name="zoomSlider" HorizontalAlignment="Left" Margin="35,106,0,0" VerticalAlignment="Top" Orientation="Vertical" Height="200" Width="20" VerticalContentAlignment="Center" HorizontalContentAlignment="Center" Interval="1"/> 
     <Button x:Name="typeHybrid" Content="Hybrid" HorizontalAlignment="Right" Margin="0,10,65,0" VerticalAlignment="Top" Width="50" Height="15" Padding="0,-1,0,0" FontSize="10"/> 
     <Button x:Name="typeTerrain" Content="Terrain" HorizontalAlignment="Right" Margin="0,10,10,0" VerticalAlignment="Top" Width="50" Height="15" Padding="0,-1,0,0" FontSize="10"/> 
     <Button x:Name="typeSatellite" Content="Satellite" HorizontalAlignment="Right" Margin="0,10,120,0" VerticalAlignment="Top" Width="50" Height="15" Padding="0,-1,0,0" FontSize="10"/> 
     <Button x:Name="typeRoad" Content="Road" HorizontalAlignment="Right" Margin="0,10,175,0" VerticalAlignment="Top" Width="50" Height="15" Padding="0,-1,0,0" FontSize="10"/> 
     <Label x:Name="copyright" Content="Map data ©2014 Google" HorizontalAlignment="Right" VerticalAlignment="Bottom" Padding="0" Width="200" FontSize="10" FontFamily="Calibri" FontWeight="Bold"/> 
    </Grid> 
    <Canvas x:Name="lyrActive" Margin="10,10,27,28" MouseWheel="lyrActive_MouseWheel" Background="#00000000"/> 




    public int zoomLevel = 0; 
    public int zoomWidth = 2; 

    public MainWindow() 
    { 
     InitializeComponent(); 

     Image i = new Image(); i.Width = 250; i.Height = 250; i.Margin = new Thickness(0, 0, 0, 0); 
     i.Source = new BitmapImage(new Uri("https://a.tiles.mapbox.com/v3/examples.map-9ijuk24y/0/0/0.png")); 
     lyrTiles.Children.Add(i); 
    } 

    private void lyrActive_MouseWheel(object sender, MouseWheelEventArgs e) 
    { 
     if (e.Delta > 0) 
     { 
      lyrTiles.Children.Clear(); 
      zoomLevel += 1; zoomWidth = zoomWidth + zoomWidth/2; 
      for (int x = 0; x < zoomWidth; x++) 
      { 
       for (int y = 0; y < zoomWidth; y++) 
       { 
        lyrTiles.Children.Add(new Image() 
        { 
         Margin = new Thickness(250 * x, 250 * y, 0, 0), 
         Source = new BitmapImage(new Uri("https://a.tiles.mapbox.com/v3/examples.map-9ijuk24y/" + zoomLevel + "/" + x + "/" + y + ".png")) 
        }); 
       } 
      } 
     } 
    } 

    private void ScrollViewer_MouseWheel(object sender, MouseWheelEventArgs e) 
    { 
     if (e.Delta > 0) 
     { 
      lyrTiles.Children.Clear(); 
      zoomLevel += 1; zoomWidth = zoomWidth + zoomWidth/2; 
      for (int x = 0; x < zoomWidth; x++) 
      { 
       for (int y = 0; y < zoomWidth; y++) 
       { 
        lyrTiles.Children.Add(new Image() 
        { 
         Margin = new Thickness(250 * x, 250 * y, 0, 0), 
         Source = new BitmapImage(new Uri("https://a.tiles.mapbox.com/v3/examples.map-9ijuk24y/" + zoomLevel + "/" + x + "/" + y + ".png")) 
        }); 
       } 
      } 
     } 
    } 

這是我應該渲染瓷磚的正確方法?我知道我必須刪除那些看不見的東西,但這只是一個非常非常簡單的項目,以查看我實際上可以做些什麼來製作地圖。我如何開始使這項工作更好?

另外,我認爲最重要和最重要的事情是座標,因爲它們都是從地圖中心的所有位置使用的,因此它可以下載正確的圖塊以將標記放置在特定位置。我怎麼能這樣做,我需要某種巨大的經緯度軸?

回答