user controls - How to prevent a map from panning while dragging on it. Windows 10 universal application with C# -
i developing windows 10 universal application c# in witch have map pin. want pin can dragged on map , prevent map panning while drag pin. problem following code map trembling while dragging pin. code dragging following:
protected async override void onnavigatedto(navigationeventargs e) { gs = (gsforcorrection)e.parameter; try { string latstring = gs.latitude; string longstring = gs.longitude; if (gs.latitude.contains('.')) latstring = gs.latitude.replace('.', ','); if (gs.longitude.contains('.')) longstring = gs.longitude.replace('.', ','); double lat, lon; bool islatok = double.tryparse(latstring, out lat); bool islonok = double.tryparse(longstring, out lon); if (gs != null && (islatok == islonok) == true) { geopoint center = new geopoint(new basicgeoposition() { latitude = lat, longitude = lon }); dragablepin pin = new dragablepin(mymap); mapcontrol.setlocation(pin, center); mapcontrol.setnormalizedanchorpoint(pin, new point(0.5, 1)); pin.draggable = true; pin.dragend += pin_dragend; mymap.children.add(pin); await mymap.trysetviewasync(center, 17); } } catch (nullreferenceexception) { } } private void pin_dragend(geopoint obj) { dragged = true; newlat = obj.position.latitude.tostring("00.0000000"); newlong = obj.position.longitude.tostring("00.0000000"); } the pin user control following code behind.
public sealed partial class dragablepin : usercontrol { private mapcontrol _map; private bool isdragging = false; geopoint _center; public dragablepin(mapcontrol map) { this.initializecomponent(); _map = map; } public bool draggable { get; set; } public action<geopoint> drag; public action<geopoint> dragstart; public action<geopoint> dragend; protected override void onpointerpressed(pointerroutedeventargs e) { base.onpointerpressed(e); if (draggable) { if (_map != null) { _center = _map.center; //attach events map track touch , movement events _map.actualcamerachanged += _map_actualcamerachanged; _map.pointerreleased += _map_pointerreleased; _map.pointermoved += _map_pointermoved; } var pointerposition = e.getcurrentpoint(_map); geopoint location = null; _map.getlocationfromoffset(pointerposition.position, out location); if (location!=null) { mapcontrol.setlocation(this, location); } if(dragstart!=null) { dragstart(location); } //enable dragging this.isdragging = true; } } private void _map_pointermoved(object sender, pointerroutedeventargs e) { //check if user dragging pushpin if(this.isdragging) { // if so, move pushpin pointer is. var pointerposition = e.getcurrentpoint(_map); geopoint location = null; _map.getlocationfromoffset(pointerposition.position, out location); if (location != null) { mapcontrol.setlocation(this, location); } if(drag!=null) { drag(location); } } } private void _map_pointerreleased(object sender, pointerroutedeventargs e) { //pushpin released, remove dragging events if (_map != null) { _map.actualcamerachanged -= _map_actualcamerachanged; _map.pointerreleased -= _map_pointerreleased; _map.pointermoved -= _map_pointermoved; } var pointerposition = e.getcurrentpoint(_map); geopoint location = null; _map.getlocationfromoffset(pointerposition.position, out location); if (location != null) { mapcontrol.setlocation(this, location); } if(dragend!=null) { dragend(location); } this.isdragging = false; } private void _map_actualcamerachanged(mapcontrol sender, mapactualcamerachangedeventargs args) { //reset map center stored center value. //this prevents map panning when drag across it. if (isdragging) { _map.center = _center; } } } i trying prevent map panning on last part of pins code, in _map_actualcamerachanged() method resetting map's center.
how can avoid maps brembling?
Comments
Post a Comment