sliverlight arcgis api实现地图地位冒气泡的功能
效果图如下:
前台页面代码:
<UserControl
xmlns:esriToolkit="clr-namespace:ESRI.ArcGIS.Client.Toolkit;assembly=ESRI.ArcGIS.Client.Toolkit"
xmlns:esri="clr-namespace:ESRI.ArcGIS.Client;assembly=ESRI.ArcGIS.Client"
xmlns:esriSymbols="clr-namespace:ESRI.ArcGIS.Client.Symbols;assembly=ESRI.ArcGIS.Client"
x:Class="_01.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006">
<Grid x:Name="LayoutRoot" Background="White">
<Grid.Resources>
<esriSymbols:SimpleFillSymbol x:Name="DefaultFillSymbol" Fill="#33FF0000" BorderBrush="Red" BorderThickness="2" />
</Grid.Resources>
<Grid.RowDefinitions>
<RowDefinition Height="130" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<!--Extent="-33,-12.85,99.75,87.15"-->
<esri:Map x:Name="MyMap" Grid.RowSpan="2" ExtentChanged="MyMap_ExtentChanged" Progress="MyMap_Progress">
<esri:Map.Layers>
<!--<esri:ArcGISDynamicMapServiceLayer ID="StreetMapLayer" />-->
<!--<esri:GraphicsLayer ID="GLayer" />-->
</esri:Map.Layers>
</esri:Map>
<Grid Height="110" HorizontalAlignment="Right" VerticalAlignment="Top" Margin="0,10,10,0" >
<Rectangle Fill="#22000000" RadiusX="10" RadiusY="10" Margin="0,4,0,0" />
<Rectangle Fill="#775C90B2" Stroke="Gray" RadiusX="10" RadiusY="10" Margin="0,0,0,5" />
<Rectangle Fill="#66FFFFFF" Stroke="DarkGray" RadiusX="5" RadiusY="5" Margin="10,10,10,15" />
<StackPanel Orientation="Vertical">
<esriToolkit:Toolbar x:Name="MyToolbar" MaxItemHeight="80" MaxItemWidth="80"
Grid.Row="0" VerticalAlignment="Top" HorizontalAlignment="Center"
Loaded="MyToolbar_Loaded"
ToolbarItemClicked="MyToolbar_ToolbarItemClicked"
ToolbarIndexChanged="MyToolbar_ToolbarIndexChanged"
Width="450" Height="80">
<esriToolkit:Toolbar.Items>
<esriToolkit:ToolbarItemCollection>
<esriToolkit:ToolbarItem Text="Zoom In">
<esriToolkit:ToolbarItem.Content>
<Image Source="Assets/images/i_zoomin.png" Stretch="UniformToFill" Margin="5" />
</esriToolkit:ToolbarItem.Content>
</esriToolkit:ToolbarItem>
<esriToolkit:ToolbarItem Text="Zoom Out">
<esriToolkit:ToolbarItem.Content>
<Image Source="Assets/images/i_zoomout.png" Stretch="UniformToFill" Margin="5" />
</esriToolkit:ToolbarItem.Content>
</esriToolkit:ToolbarItem>
<esriToolkit:ToolbarItem Text="Pan">
<esriToolkit:ToolbarItem.Content>
<Image Source="Assets/images/i_pan.png" Stretch="UniformToFill" Margin="5" />
</esriToolkit:ToolbarItem.Content>
</esriToolkit:ToolbarItem>
<esriToolkit:ToolbarItem Text="Previous Extent" >
<esriToolkit:ToolbarItem.Content>
<Image Source="Assets/images/i_previous.png" IsHitTestVisible="False" Opacity="0.3" Stretch="UniformToFill" Margin="5"/>
</esriToolkit:ToolbarItem.Content>
</esriToolkit:ToolbarItem>
<esriToolkit:ToolbarItem Text="Next Extent">
<esriToolkit:ToolbarItem.Content>
<Image Source="Assets/images/i_next.png" IsHitTestVisible="False" Opacity="0.3" Stretch="UniformToFill" Margin="5"/>
</esriToolkit:ToolbarItem.Content>
</esriToolkit:ToolbarItem>
<esriToolkit:ToolbarItem Text="Full Extent">
<esriToolkit:ToolbarItem.Content>
<Image Source="Assets/images/i_globe.png" Stretch="UniformToFill" Margin="5" />
</esriToolkit:ToolbarItem.Content>
</esriToolkit:ToolbarItem>
<esriToolkit:ToolbarItem Text="Full Screen">
<esriToolkit:ToolbarItem.Content>
<Image Source="Assets/images/i_widget.png" Stretch="UniformToFill" Margin="5" />
</esriToolkit:ToolbarItem.Content>
</esriToolkit:ToolbarItem>
</esriToolkit:ToolbarItemCollection>
</esriToolkit:Toolbar.Items>
</esriToolkit:Toolbar>
<TextBlock x:Name="StatusTextBlock" Text="" FontWeight="Bold" HorizontalAlignment="Center"/>
</StackPanel>
</Grid>
<!--progressbar 放在LayoutRoot中-->
<Grid HorizontalAlignment="Center" x:Name="progressGrid" VerticalAlignment="Center" Width="200" Height="20" Margin="5,5,5,5">
<ProgressBar x:Name="MyProgressBar" Minimum="0" Maximum="100" />
<TextBlock x:Name="ProgressValueTextBlock" Text="100%" HorizontalAlignment="Center" VerticalAlignment="Center" />
</Grid>
<Button Click="Button_Click" Content="弹出子窗体" Margin="12,254,288,12" Width="100" Height="35" Grid.RowSpan="2"/>
</Grid>
</UserControl>
后台代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using ESRI.ArcGIS.Client;
using ESRI.ArcGIS.Client.Geometry;
using ESRI.ArcGIS.Client.Symbols;
using System.Windows.Media.Imaging;
namespace _01
{
public partial class MainPage : UserControl
{
string _toolMode = "";
List<Envelope> _extentHistory = new List<Envelope>();
int _currentExtentIndex = 0;
bool _newExtent = true;
Image _previousExtentImage;
Image _nextExtentImage;
private Draw MyDrawObject;
public MainPage()
{
InitializeComponent();
ArcGISDynamicMapServiceLayer onlineMap = new ArcGISDynamicMapServiceLayer();
onlineMap.ID = "onlineMap";
onlineMap.Url = "http://services.arcgisonline.com/ArcGIS/rest/services/ESRI_Imagery_World_2D/MapServer";
ArcGISDynamicMapServiceLayer Dm_chinalayer=new ArcGISDynamicMapServiceLayer();
Dm_chinalayer.ID = "StreetMapLayer";
Dm_chinalayer.Url = "http://localhost/ArcGIS/rest/services/china/MapServer";
ArcGISDynamicMapServiceLayer TM_whmap_layer = new ArcGISDynamicMapServiceLayer();
TM_whmap_layer.ID = "Wh_layer";
TM_whmap_layer.Url = "http://localhost/ArcGIS/rest/services/whmap/MapServer";
Graphic[] graphics = new Graphic[2];
graphics[0] = new Graphic()
{
Geometry = new MapPoint(114.278112, 30.60987),
Symbol = new SimpleMarkerSymbol()
{
Color = new SolidColorBrush(Colors.Blue),
Size = 12,
Style = SimpleMarkerSymbol.SimpleMarkerStyle.Square
}
};
Uri uri = new Uri("../Assets/images/i_pushpin.png", UriKind.Relative);
BitmapImage BitMapImage = new BitmapImage(uri);
graphics[1] = new Graphic()
{
Geometry = new MapPoint(114.281612, 30.60787),
Symbol = new PictureMarkerSymbol()
{
Source = BitMapImage
}
};
GraphicsLayer glayer = new GraphicsLayer();
for (int i = 0; i < graphics.Length; i++)
{
graphics[i].Attributes.Add("ID", "Lee"+i);
glayer.Graphics.Add(graphics[i]);
}
MyMap.Layers.Insert(0, onlineMap);
MyMap.Layers.Insert(1, Dm_chinalayer);
MyMap.Layers.Insert(2, TM_whmap_layer);
MyMap.Layers.Insert(3, glayer);
//MyMap.Layers["GLayer"] as GraphicsLayer;
foreach (Graphic g in graphics)
{
glayer.Graphics.Add(g);
g.MouseLeftButtonUp+=new MouseButtonEventHandler(graphic_MouseLeftButtonDown);
}
MyDrawObject = new Draw(MyMap)
{
FillSymbol = DefaultFillSymbol,
DrawMode = DrawMode.Rectangle
};
MyDrawObject.DrawComplete += myDrawObject_DrawComplete;
}
private void graphic_MouseLeftButtonDown(object o, MouseButtonEventArgs e)
{
Graphic g = o as Graphic;
MessageBox.Show(string.Format("keys:{0}\nvalues:{1}", g.Attributes["ID"].ToString(),g.Attributes["ID"].ToString()));
}
private void Button_Click(object sender, RoutedEventArgs e)
{
ChildWindow1 win = new ChildWindow1();
win.Title = "这是我的窗口Lee!";
win.Content = new TextBlock
{
Text = "大家好,这个是Silverlight子窗口中的内容.",
TextWrapping = TextWrapping.Wrap,
Width = 380,
Height = 23
};
win.Show();
}
private void MyToolbar_Loaded(object sender, RoutedEventArgs e)
{
_previousExtentImage = MyToolbar.Items[3].Content as Image;
_nextExtentImage = MyToolbar.Items[4].Content as Image;
}
private void MyToolbar_ToolbarItemClicked(object sender, ESRI.ArcGIS.Client.Toolkit.SelectedToolbarItemArgs e)
{
MyDrawObject.IsEnabled = false;
_toolMode = "";
switch (e.Index)
{
case 0: // ZoomIn Layers
MyDrawObject.IsEnabled = true;
_toolMode = "zoomin";
break;
case 1: // Zoom Out
MyDrawObject.IsEnabled = true;
_toolMode = "zoomout";
break;
case 2: // Pan
break;
case 3: // Previous Extent
if (_currentExtentIndex != 0)
{
_currentExtentIndex--;
if (_currentExtentIndex == 0)
{
_previousExtentImage.Opacity = 0.3;
_previousExtentImage.IsHitTestVisible = false;
}
_newExtent = false;
MyMap.IsHitTestVisible = false;
MyMap.ZoomTo(_extentHistory[_currentExtentIndex]);
if (_nextExtentImage.IsHitTestVisible == false)
{
_nextExtentImage.Opacity = 1;
_nextExtentImage.IsHitTestVisible = true;
}
}
break;
case 4: // Next Extent
if (_currentExtentIndex < _extentHistory.Count - 1)
{
_currentExtentIndex++;
if (_currentExtentIndex == (_extentHistory.Count - 1))
{
_nextExtentImage.Opacity = 0.3;
_nextExtentImage.IsHitTestVisible = false;
}
_newExtent = false;
MyMap.IsHitTestVisible = false;
MyMap.ZoomTo(_extentHistory[_currentExtentIndex]);
if (_previousExtentImage.IsHitTestVisible == false)
{
_previousExtentImage.Opacity = 1;
_previousExtentImage.IsHitTestVisible = true;
}
}
break;
case 5: // Full Extent
MyMap.ZoomTo(MyMap.Layers.GetFullExtent());
break;
case 6: // Full Screen
Application.Current.Host.Content.IsFullScreen = !Application.Current.Host.Content.IsFullScreen;
break;
}
}
private void MyToolbar_ToolbarIndexChanged(object sender, ESRI.ArcGIS.Client.Toolkit.SelectedToolbarItemArgs e)
{
StatusTextBlock.Text = e.Item.Text;
}
private void myDrawObject_DrawComplete(object sender, DrawEventArgs args)
{
if (_toolMode == "zoomin")
{
MyMap.ZoomTo(args.Geometry as ESRI.ArcGIS.Client.Geometry.Envelope);
}
else if (_toolMode == "zoomout")
{
Envelope currentExtent = MyMap.Extent;
Envelope zoomBoxExtent = args.Geometry as Envelope;
MapPoint zoomBoxCenter = zoomBoxExtent.GetCenter();
double whRatioCurrent = currentExtent.Width / currentExtent.Height;
double whRatioZoomBox = zoomBoxExtent.Width / zoomBoxExtent.Height;
Envelope newEnv = null;
if (whRatioZoomBox > whRatioCurrent)
// use width
{
double mapWidthPixels = MyMap.Width;
double multiplier = currentExtent.Width / zoomBoxExtent.Width;
double newWidthMapUnits = currentExtent.Width * multiplier;
newEnv = new Envelope(new MapPoint(zoomBoxCenter.X - (newWidthMapUnits / 2), zoomBoxCenter.Y),
new MapPoint(zoomBoxCenter.X + (newWidthMapUnits / 2), zoomBoxCenter.Y));
}
else
// use height
{
double mapHeightPixels = MyMap.Height;
double multiplier = currentExtent.Height / zoomBoxExtent.Height;
double newHeightMapUnits = currentExtent.Height * multiplier;
newEnv = new Envelope(new MapPoint(zoomBoxCenter.X, zoomBoxCenter.Y - (newHeightMapUnits / 2)),
new MapPoint(zoomBoxCenter.X, zoomBoxCenter.Y + (newHeightMapUnits / 2)));
}
if (newEnv != null)
MyMap.ZoomTo(newEnv);
}
}
private void MyMap_ExtentChanged(object sender, ExtentEventArgs e)
{
if (e.OldExtent == null)
{
_extentHistory.Add(e.NewExtent.Clone());
return;
}
if (_newExtent)
{
_currentExtentIndex++;
if (_extentHistory.Count - _currentExtentIndex > 0)
_extentHistory.RemoveRange(_currentExtentIndex, (_extentHistory.Count - _currentExtentIndex));
if (_nextExtentImage.IsHitTestVisible == true)
{
_nextExtentImage.Opacity = 0.3;
_nextExtentImage.IsHitTestVisible = false;
}
_extentHistory.Add(e.NewExtent.Clone());
if (_previousExtentImage.IsHitTestVisible == false)
{
_previousExtentImage.Opacity = 1;
_previousExtentImage.IsHitTestVisible = true;
}
}
else
{
MyMap.IsHitTestVisible = true;
_newExtent = true;
}
}
private void ArcGISDynamicMapServiceLayer_InitializationFailed(object sender, EventArgs e)
{
ESRI.ArcGIS.Client.Layer layer = sender as ESRI.ArcGIS.Client.Layer;
MessageBox.Show(layer.InitializationFailure.Message);
}
private void MyMap_Progress(object sender, ProgressEventArgs e)
{
if (e.Progress < 100)
{
progressGrid.Visibility = Visibility.Visible;
MyProgressBar.Value = e.Progress;
ProgressValueTextBlock.Text = String.Format("正在处理 {0}%", e.Progress);
}
else
{
progressGrid.Visibility = Visibility.Collapsed;
}
}
}
}