2

I'm attempting to use MQA.TileMap.zoomToRect to set the view-port of a given bounding box.

var cust;
var rect = new MQA.RectLL();
for (var i = 0, len = custs.length; i < len; i++) {
    cust = custs[i];
    poi = new MQA.Poi({lat:cust.lat, lng:cust.lng});
    map.addShape(poi); // This works
    rect.extend(poi.latLng); // Does nothing to `rect'.
}
map.zoomToRect(rect, false); // This fails

It appears that the rect values remain as 0,0 for both lr and ul properties. The call results in the following output in firebog

"NetworkError: 500 Internal Server Error - http://coverage.mqcdn.com/coverage?format=json&jsonp=MQA._covCallback&loc=NaN,NaN,NaN,NaN&zoom=2&projection=sm&cat=map%2Chyb%2Csat"

EDIT: I've added notes to the example that adding a shape works fine, so the poi object is fine, also inspecting poi.latLng is fine.

Brett Ryan
  • 26,937
  • 30
  • 128
  • 163

2 Answers2

1

If you put the POI's in a collection you can use the collections getBoundingRect method.

Example:

var collection = new MQA.ShapeCollection();
collection.add( new MQA.Poi( { lat: 0, lng: 0 } ) ); 
collection.add( new MQA.Poi( { lat: 100, lng: 100 } ) );

map.zoomToRect( collection.getBoundingRect() );

EDIT Wont work without

<script
src="http://www.mapquestapi.com/sdk/js/v7.2.s/mqa.toolkit.js?key=<YOURKEY>"></script>
Siddharth
  • 9,349
  • 16
  • 86
  • 148
Ahrengot
  • 1,579
  • 1
  • 17
  • 29
-1

based on a quick glance, are you sure you are retrieving the lat/long values in your "for" loop? The 500 error shown in firebug has "..&loc=NaN,NaN,NaN,NaN&..." so it looks like you aren't successfully retrieving the lat/long values from the array and instead, retrieving objects.

If you can share more code, I can try to put together an example. Or even show me what your "custs" array looks like in terms of how it's structured. You'll probably need to do something like this:

poi = new MQA.Poi({lat:custs.cust[i].lat, lng:custs.cust[i].lng});

But again, it depends on what your array looks like. You just need to make sure you're looping through all of the individual items in the array and retrieving the lat/long values from each item in the array.

Also, MapQuest has a Developer Network (which I'm sure you've seen if you're using the API!), and there is a forum where you can post code samples and ask for help. MapQuest employees do participate in the forum and could possibly help, too, if you run into trouble.

Hope this helps!

jharahush
  • 767
  • 4
  • 6
  • I had forgotten the local `cust` assignment in my example. I should note that the POI object is fine because I get the POI objects showing up on the map in the correct locations. – Brett Ryan Jan 11 '12 at 19:58
  • 1
    I've never done anything with extending the viewport in this way (usually just using the "bestFit" method fits my needs), but I'm not sure that the rect.extend method will accept a POI object as an input. It looks like it's looking for a lat/long (MQA.LatLon) object, at least according to the API reference guide. I might be wrong -- I'm still learning, too. – jharahush Jan 11 '12 at 22:04
  • Yes it does accept a `MQA.LatLng` which is why I pass it the `MQA.Poi.latLng` property. – Brett Ryan Jan 11 '12 at 22:20
  • Now that I'm reading more about it, it looks like you need to pass an upper left and a lower right boundary when using zoomToRect and not just the lat/lon property from one POI. I think you would need to cycle through your array of POIs and calculate an upper left boundary and a lower right, and then pass BOTH of those lat/lons as lat/lon objects. Hope this helps! – jharahush Jan 11 '12 at 23:04