Select to view content in your preferred language

How can I show mutiple address markers on a map and set the proper map scale?

862
9
01-05-2012 02:38 PM
FranciscRomano
Emerging Contributor
Hello,

I have an Array of address objects and am currently looping through each array element and calling locateTask.addressToLocations() for each.

It works, but it's ugly, lots of moving around and scaling occuring for each address as it's located.

I was wondering what the proper way to do this is please and how to set map scale to have all markers in view.

The scaling is done as if for one marker:
private function mapAddressResult(candidates:Array,token:Object=null):void
{
 if(candidates.length > 0)
 {
  var addressCandidate:AddressCandidate=candidates[0];
  var mapGraphic:Graphic=new Graphic();
  mapGraphic.geometry=addressCandidate.location;
  mapGraphic.symbol=selectedSymbol;
  mapGraphic.toolTip=addressCandidate.address.toString();
  mapGraphic.id="graphic";
  graphicsLayer.add(mapGraphic);
  map.centerAt(addressCandidate.location);
  
  // Zoom to an appropriate level
  // Note: your tile levels and LOC_NAME might differ...
  if (addressCandidate.attributes.Loc_name.search("RoofTop") > 0)
  {
   map.scale=10000;
  }
  else if (addressCandidate.attributes.Loc_name.search("Address")>0)
  {
   map.scale=10000;
  }
  else if (addressCandidate.attributes.Loc_name.search("Street")>0)
  {
   map.scale=15000;
  }
  else if (addressCandidate.attributes.Loc_name.search("Zipcode")>0)
  {
   map.scale=40000;
  }
  else if (addressCandidate.attributes.Loc_name.search("City")>0)
  {
   map.scale=150000;
  }
  else
  {
   map.scale=500000;
  }
 }
}


That method is called for each of the addresses, hence the re-centering and re-scaling each time one is located.

How do I do this properly please?

Thank you.
Tags (2)
0 Kudos
9 Replies
FranciscRomano
Emerging Contributor
Help! *bump*
0 Kudos
andrewj_ca
Frequent Contributor
You can try this:

private var zoomToExtent:Extent;
private function doZoom(event:MouseEvent):void
{
if (graphicsLayer.numGraphics > 0)
{
var arr:Array = new Array();
for each (var g:Graphic in graphicsLayer.graphicProvider)
{
  arr.push(g);
}
var ge:Extent = GraphicUtil.getGraphicsExtent(arr);
if (ge)
{
  var dist:Number=1000;
  var graphicsExtent:Extent = new Extent(ge.xmin-dist,ge.ymin-dist,ge.xmax+dist,ge.ymax+dist,map.spatialReference);
  if (graphicsExtent)
  {
   zoomToExtent = graphicsExtent;
   map.extent = graphicsExtent;
  }
}else
{
  var bdist:Number=1000;
  var gr:Graphic = graphicsLayer.graphicProvider[0];
  var pt:MapPoint = gr.geometry as MapPoint;
  var ext:Extent = new Extent(pt.x -bdist,pt.y-bdist,pt.x+bdist,pt.y+bdist,map.spatialReference);
  zoomToExtent=ext;
  map.extent=ext;
}
checkEnvelope();
}
private function checkEnvelope():void
{
if (zoomToExtent.contains(map.extent))
{
  map.zoomOut();

}


Basically creates an extent for the graphics layer and then sets the map.extent to the new extent.  The check envelope is used for viewers that have cached base maps where the zooms are fixed.  Let me know, it works well for me.
0 Kudos
FranciscRomano
Emerging Contributor
Thank you for your answer, Andrew, much appreciated.

Scaling was part of my problem and your solution works nicely.
My other problem is I have an array of addresses that I loop through and for each successfully located address the mapAddressResult() I wrote is called.
Is there a method I can pass multiple addresses at the same time to do this?
Or should I just add markers as I do, but not set scale and then, once the last address marker is placed, scale using the function you wrote?

Again, thank you.
0 Kudos
FranciscRomano
Emerging Contributor
Help (again)! 🙂
0 Kudos
andrewj_ca
Frequent Contributor
I wouldn't set the scale at all.  Loop your results, add them to the map(graphicslayer) and after the loop call my function.  This would be far more efficient.  Cheers,

Andy
0 Kudos
FranciscRomano
Emerging Contributor
OK. So there's no locator that receives an array of addresses.
Looping after each one is still the best way.
Thank you, Andy.

By the way, is there an event that gets fired when the map is all set, markers loaded and extent set?
0 Kudos
andrewj_ca
Frequent Contributor
Since the extent changed is your last task, you can use the map.extentChanged() event.
0 Kudos
FranciscRomano
Emerging Contributor
Thanks again, Andy.
Awesome.
0 Kudos
andrewj_ca
Frequent Contributor
Happy to help, cheers.
0 Kudos