[OpenLayers-Users] Is there an easy way to center and zoom the map automatically based on the placemarks that are on it?

Aaron E-J azintirea at gmail.com
Fri Jul 6 13:25:30 PDT 2012


What it essentially comes down to is I haven't figured out how to get an
array of coordinates that represent all the filtered placemarks on the map.
If I could get this, it would be fairly easy to set and center the BBox.  Does
anyone have any examples of this for KML on a vector layer implemented
using the http protocol?  Thanks a lot.

Aaron E-J
http://otherrealm.org
http://aaronej.blogspot.com
Vision without action is a daydream,
Action without vision is a nightmare,
One needs both to succeed.

On Thu, Jul 5, 2012 at 1:45 PM, Someone [via OSGeo.org] <
ml-node+s1560n4986344h30 at n6.nabble.com> wrote:

> Here is a link to the kml file that I am using: http://66.230.78.217/venuesComplete.kml
>


On 2012-07-05 11:17, Someone whote:

> I'm getting a "bounds is null" error when I try to implement this line of
> code.

Probably something simple I'm not doing but I can't figure out what this
> is.  And suggestions appreciated.
>

function showVenues(state, city, venue){
    var kmllayer = new OpenLayers.Layer.Vector(
	"KML", {
	    strategies: [filterStrategy, new OpenLayers.Strategy.Fixed()],
	    protocol: new OpenLayers.Protocol.HTTP({
		url: "venuesComplete.kml",
		format: new OpenLayers.Format.KML({
		    placemarksDesc:true,
		    extractStyles: true,
		    extractAttributes: true
		}),
		attribution:'coordinates'
	    })
	});
    select = new OpenLayers.Control.SelectFeature(kmllayer);
    kmllayer.events.on({
	"featureselected": onFeatureSelect,
	"featureunselected": onFeatureUnselect
    });
    map.addControl(select);
    select.activate();
    function onPopupClose(evt) {
	select.unselectAll();
    }
    function onFeatureSelect(event) {
	var feature = event.feature;
	var selectedFeature = feature;
	var popup = new OpenLayers.Popup.FramedCloud("chicken",
	    feature.geometry.getBounds().getCenterLonLat(),
	    new OpenLayers.Size(100,100),
	    "<h2>"+feature.attributes.name + "</h2>" +
feature.attributes.description
+'<br>'+feature.attributes.Point.coordinates,
	    null,
	    true,
	    onPopupClose
	    );
	document.getElementById('venueName').value=feature.attributes.name;
	feature.popup = popup;
	map.addPopup(popup);
    }
    function onFeatureUnselect(event) {
	var feature = event.feature;
	if(feature.popup) {
	    map.removePopup(feature.popup);
	    feature.popup.destroy();
	    delete feature.popup;
	}
    }
    clearFilter();
    map.zoomToExtent(kmllayer.getDataExtent());
	    /* get the following message in firebug:
	    bounds is null
	    zoomToExtent(bounds=null, closest=undefined)Map.js (line 2250)
	    showVenues(state="", city="", venue="Fox Theatre -
Tucson")index.php (line 1204)
	    init()index.php (line 1259)
	    (?)()index.php (line 1126)
	    f(b=Document
index.php#?q=displayGetEventNames&str=||||||||Fox%20Theatre||||||||||||||||date,
f=[function()])jquery.min.js (line 2)
	    f(b=Document
index.php#?q=displayGetEventNames&str=||||||||Fox%20Theatre||||||||||||||||date,
c=[function()])jquery.min.js (line 2)
	    f(a=undefined)jquery.min.js (line 2)
	    f()jquery.min.js (line 2)
	    error source line:
	    [Break On This Error]
	    var center = bounds.getCenterLonLat();
	     */
}
function clearFilter(){
    filterStrategy.setFilter(null);
}
function setFilter(){
    /****returned from a php file via a .post request****
    $venueFilters = <<<'EOD'
    <script>
    filter=new OpenLayers.Filter.Logical({
		type: OpenLayers.Filter.Logical.OR,
		filters: [
    EOD;
	    $firstVenue = true;
	    foreach ($venueName as $k => $v) {
		if ($firstVenue) {
		    $venueFilters.= <<<EOD
    new OpenLayers.Filter.Comparison({
			type: OpenLayers.Filter.Comparison.LIKE,
			property: 'name',
			value: "$v"
		    })
    EOD;
		} else {
		    $venueFilters.= <<<EOD
    ,
    new OpenLayers.Filter.Comparison({
			type: OpenLayers.Filter.Comparison.LIKE,
			property: 'name',
			value: "$v"
		    })
    EOD;
		}
		$firstVenue = false;
	    }
	    $venueFilters.= <<<'EOD'
		]
	    });
	    setFilter();
    </script>
    EOD;
    *************************************************/
    filterStrategy.setFilter(filter);
}
function init() {
    map = new OpenLayers.Map('map');
    var google_map_layer = new OpenLayers.Layer.Google(
	'Google Map Layer',
	{
	    type: google.maps.MapTypeId.HYBRID
	    }
	);
    map.addLayer(google_map_layer);
    state="";
    state+=document.getElementById('stateProvDesc').value;
    city="";
    city+=document.getElementById('cityZip').value;
    venue="";
    venue+=document.getElementById('venueName').value;
    showVenues(state,city,venue);
    map.addControl(new OpenLayers.Control.LayerSwitcher({}));
    map.zoomToMaxExtent();
}

Vision without action is a daydream,
Action without vision is a nightmare,
One needs both to succeed.

On 7/4/2012 3:17 AM, Tobias Reinicke wrote:

The same function is also available for vector layers;
http://dev.openlayers.org/docs/files/OpenLayers/Layer/Vector-js.html#OpenLayers.Layer.Vector.getDataExtent


 I presume you will have set your kml layer up somewhere along the lines
of;

 var myKMLLayer = new OpenLayers.Layer.Vector("KML", {

            strategies: [new OpenLayers.Strategy.Fixed()],
            protocol: new OpenLayers.Protocol.HTTP({
                url: "whatever",
                format: new OpenLayers.Format.KML({
                    extractStyles: true,
                    extractAttributes: true,
                    maxDepth: 2
                })
            })
        };
Then all you do is call
map.zoomToExtent(myKMLLayer.getDataExtent());

 That work for you?

 Toby




On 4 July 2012 01:21, Aaron E-J <[hidden
email]<http://osgeo-org.1560.n6.nabble.com/user/SendEmail.jtp?type=node&node=4985988&i=0>
> wrote:

>  Do you have, or know where I could find, an example of this being
> implemented on KML layer?
>
> Aaron E-Jhttp://www.otherrealm.orghttp://www.prospectdesigns.com
>
> Vision without action is a daydream,
> Action without vision is a nightmare,
> One needs both to succeed.
>
>  On 7/3/2012 5:52 PM, Greg Allensworth wrote:
>
> On 7/3/2012 2:11 PM, Aaron E-J wrote:
>
> Is there an easy way to center and zoom the map automatically based on
> the placemarks that are on it?I have it so that one can filter
> placemarks based on user input but I'm not sure how to dynamically
> update the map to fit the newly filtered content.Thanks for any insight
> that can be given regarding this matter.
>
>
> An OpenLayers.Layer.Markers instance, has a method called getDataExtent()
> This should do most of what you want.
>
> It's not automatic, though. You'd still need to call map.zoomToExtent()
> after the markers have been added to the layer.
>
> http://dev.openlayers.org/docs/files/OpenLayers/Layer/Markers-js.html
>
>
>
>
> _______________________________________________
> Users mailing list
> [hidden email]<http://osgeo-org.1560.n6.nabble.com/user/SendEmail.jtp?type=node&node=4985988&i=1>
> http://lists.osgeo.org/mailman/listinfo/openlayers-users
>
>



_______________________________________________
Users mailing list
[hidden email]<http://osgeo-org.1560.n6.nabble.com/user/SendEmail.jtp?type=node&node=4985988&i=2>
http://lists.osgeo.org/mailman/listinfo/openlayers-users
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/openlayers-users/attachments/20120706/c4a61be9/attachment.html>


More information about the Users mailing list