Using a map to visualize data within an application is great,but you must first get the location of the data to be displayed. If you have the address you can geocode it using the Bing Maps Web Services,but "What if you can't geocode it?" Or,"What if the geoCoding can't find the address?" Well,if your user kNows where the location is,then you can have them point it out by clicking on the map. Creating pushpins in response to a users click is nice,but wouldn't it be even nicer if they Could "Click and Drag" the pushpin around to define/edit/change the location of the data entity?
I have even seen this discussed a bit in regards to the Bing Maps Silverlight Control,and it isn't something that is built into the map control directly. However it isn't too difficult to implement,if you kNow what to do. So I decIDed to create and post a simple "Draggablepushpin" object deriving from the "Microsoft.Maps.MapControl.pushpin" object that implements Dragging in a nice,self contained fashion. There's no need to wire up any events,you simple add a "Draggablepushpin" to you Map,and the user can drag it around.
Here's the code for the "Draggablepushpin":
public class Draggablepushpin : Microsoft.Maps.MapControl.pushpin
{
private bool isDragging = false;
EventHandler<MapMouseDragEventArgs> ParentMapMousePanHandler;
MousebuttonEventHandler ParentMapMouseleftbuttonUpHandler;
MouseEventHandler ParentMapMouseMoveHandler;
protected overrIDe voID OnMouseleftbuttonDown(MousebuttonEventArgs e)
{
// Check if the Map Event Handlers have been created/attached to the Map
// If not,then attach them. This is done in the "pushpin.OnMouseleftbuttonDown"
// event because we don't kNow when the pushpin is added to a Map or MapLayer,but
// we do konw that when this event is fired the pushpin will already have been added.
var parentLayer = this.Parent as MapLayer;
if (parentLayer != null)
{
var parentMap = parentLayer.ParentMap;
if (parentMap != null)
{
if (this.ParentMapMousePanHandler == null)
{
this.ParentMapMousePanHandler = new EventHandler<MapMouseDragEventArgs>(ParentMap_MousePan);
parentMap.MousePan += this.ParentMapMousePanHandler;
}
if (this.ParentMapMouseleftbuttonUpHandler == null)
{
this.ParentMapMouseleftbuttonUpHandler = new MousebuttonEventHandler(ParentMap_MouseleftbuttonUp);
parentMap.MouseleftbuttonUp += this.ParentMapMouseleftbuttonUpHandler;
}
if (this.ParentMapMouseMoveHandler == null)
{
this.ParentMapMouseMoveHandler = new MouseEventHandler(ParentMap_MouseMove);
parentMap.MouseMove += this.ParentMapMouseMoveHandler;
}
}
}
// Enable Dragging
this.isDragging = true;
base.OnMouseleftbuttonDown(e);
}
#region "Mouse Event Handler Methods"
voID ParentMap_MousePan(object sender,MapMouseDragEventArgs e)
{
// If the pushpin is being dragged,specify that the Map's MousePan
// event is handled. This is to suppress the Panning of the Map that
// is done when the mouse drags the map.
if (this.isDragging)
{
e.Handled = true;
}
}
voID ParentMap_MouseleftbuttonUp(object sender,MousebuttonEventArgs e)
{
// left Mouse button released,stop dragging the pushpin
this.isDragging = false;
}
voID ParentMap_MouseMove(object sender,MouseEventArgs e)
{
var map = sender as Microsoft.Maps.MapControl.Map;
// Check if the user is currently dragging the pushpin
if (this.isDragging)
{
// If so,the Move the pushpin to where the Mouse is.
var mouseMapposition = e.Getposition(map);
var mouseGeocode = map.VIEwportPointTolocation(mouseMapposition);
this.Location = mouseGeocode;
}
}
#endregion
}
转自:http://pietschsoft.com/post/2010/05/30/Draggable-Pushpins-using-Bing-Maps-Silverlight-Control.aspx
总结以上是内存溢出为你收集整理的Draggable Pushpins using Bing Maps Silverlight Control全部内容,希望文章能够帮你解决Draggable Pushpins using Bing Maps Silverlight Control所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)