Select to view content in your preferred language

Zoom and Pan with Address locator

1875
2
04-09-2010 08:19 AM
DonFreeman
Emerging Contributor
I have an address locator based on the ESRI sample working OK. The code is below. When it first locates the address, it zooms and pans to place the found address within the map frame but it does not center the found address. If I click the Find button again to reexecute the code, it then pans again to center the found address. I would like it to center the first time and not require a second click. I have tried placing the PanTo code in the first half of the IF statement but this seems to have no effect. Can someone tell me what I have to do to get the behavior I want?

Thanks

 Private Sub LocatorTask_AddressToLocationsCompleted(ByVal sender As Object, ByVal args As ESRI.ArcGIS.Client.Tasks.AddressToLocationsEventArgs)
  Dim returnedCandidates As List(Of AddressCandidate) = args.Results

  Dim graphicsLayer As GraphicsLayer = TryCast(MyMap.Layers("MyGraphicsLayer"), GraphicsLayer)
  graphicsLayer.ClearGraphics()

  Dim _candidateList = New List(Of AddressCandidate)()
  Dim candidateListBox As New ListBox()

  For Each candidate As AddressCandidate In returnedCandidates
   If candidate.Score >= 95 Then
    _candidateList.Add(candidate)
    candidateListBox.Items.Add(candidate.Address)
    Dim graphic As New Graphic() With {.Symbol = DefaultMarkerSymbol, .Geometry = candidate.Location}
    graphic.Attributes.Add("Address", candidate.Address)
    Dim latlon As String = String.Format("{0}, {1}", candidate.Location.X, candidate.Location.Y)
    graphic.Attributes.Add("LatLon", latlon)
    graphicsLayer.Graphics.Add(graphic)
   End If
  Next candidate

  AddHandler candidateListBox.SelectionChanged, AddressOf _candidateListBox_SelectionChanged

  CandidateScrollViewer.Content = candidateListBox
  CandidatePanelGrid.Visibility = Visibility.Visible

  Dim pt As MapPoint = _candidateList(0).Location
  If _firstZoom Then
   MyMap.ZoomToResolution(MyMap.Resolution / 8, pt)
   _firstZoom = False
  Else
   MyMap.PanTo(pt)
  End If

  _lastIndex = 0
  candidateListBox.SelectedIndex = 0
 End Sub
0 Kudos
2 Replies
dotMorten_esri
Esri Notable Contributor
Is this because the ZoomTo code only executed the first time? Can you zoom in this far?
0 Kudos
DominiqueBroux
Esri Frequent Contributor
If you are only applying ZoomToResolution (the first time in your code), you don't get the expected result because the argument of this method is the center to zoom around (e.g. the fix point). It's not the expected center of the map.

If you place the PanTo code with ZoomToResolution in the first half of the IF statement, you run into an issue with these 2 asynchronous methods that overlap (the pan begins while the zoomtoresolution is not over).

A workaround could be to reexecute PanTo after a delay:

      Map.ZoomToResolution(MyMap.MaximumResolution / 10, pt);
      Map.PanTo(pt);
      DispatcherTimer timer = new DispatcherTimer();
      timer.Interval = TimeSpan.FromMilliseconds(500);
      timer.Tick += new EventHandler((s, eventArgs) => {
        timer.Stop();
        Map.PanTo(pt);
      });
      timer.Start();

But the result is unaesthetic.

Another solution would be to use the 'ZoomTo' method, but you will have to expand the point to get the geometry corresponding to the expected resolution (--> not that easy calculation)

There are likely others solutions, but I have no more clue.

Dominique
0 Kudos