Solved

Openlayers - zooming to show multiple co-ordinates

Posted on 2014-11-10
1
239 Views
Last Modified: 2014-11-16
I have a bunch of co-ordinates, and i was the map to show them all on screen
Currently, its just zooming on one asset

I've, found some code, but it just seems to zoom into the sea (near gulf of guinea), i'm not sure if this is due to a map type?

Here is my code that i've been using to try and get it working


var selectedAsset_Coordinates = {};
var markers = new OpenLayers.Layer.Markers("Markers");

selectedAsset_Coordinates.d = [];

for (var kd = 0; kd < currentlySelectedCheckboxes.length; kd++) {
            
            k = currentlySelectedCheckboxes[kd];

            selectedAsset_Coordinates.d[k] = {};
            selectedAsset_Coordinates.d[k].vLongitude = obj.d[k].vLongitude;
            selectedAsset_Coordinates.d[k].vLatitude = obj.d[k].vLatitude;

            var _CurrentRowId = $("#chk" + k)[0].id.substring(3, $("#chk" + k)[0].id.length);
            if ($.inArray(parseInt(_CurrentRowId), currentlySelectedCheckboxes) > -1) {
                var point = new OpenLayers.Geometry.Point(obj.d[k].vLongitude, obj.d[k].vLatitude);
                point = point.transform(WGS84, map.getProjectionObject());

                var pointFeature = new OpenLayers.Feature.Vector(point, null, null);

                var _str = ShortAssetName(obj.d[k].vDeviceName);

                pointFeature.attributes = {
                    label: _str,
                    favColor: 'blue', //favorite color
                    align: "cm",
                    labelAlign: "cm",
                    AssetID: obj.d[k].vpkDeviceID
                };


                flagLayer.addFeatures([pointFeature]);  // this code for showing tooltip


                //map.setCenter(new OpenLayers.LonLat(obj.d[k].vLongitude, obj.d[k].vLatitude).transform(WGS84, map.getProjectionObject()), map.getZoom());


            var ipoint = new OpenLayers.Geometry.Point(obj.d[k].vLongitude, obj.d[k].vLatitude);
            ipoint = ipoint.transform(WGS84, map.getProjectionObject());

           var ipointFeature = new OpenLayers.Feature.Vector(ipoint, null, null);

            ipointFeature.attributes = {
                icon: maparrow + obj.d[k].vImage,
                label: obj.d[k].vDeviceName,
                radius: 1,
                date: recevedtime,
                Ignition: obj.d[k].bIsIgnitionOn,
                Latitude: obj.d[k].vLatitude,
                Longitude: obj.d[k].vLongitude,
                AssetName: obj.d[k].vDeviceName,
                DriverId: obj.d[k].DriverId,
                AssetPhotoName: obj.d[k].AssetPhotoName,
                iTrackerType: obj.d[k].iTrackerType,
                FuelVoltage: obj.d[k].FuelVoltage,
                Drivername: obj.d[k].Drivername,
                DriverPhoto: obj.d[k].DriverPhoto,
                Speed: obj.d[k].vVehicleSpeed,
                AssetID: obj.d[k].vpkDeviceID,
                ipkAssetID: parseInt(obj.d[k].ipkDeviceID, "10")
            };

            markerLayer.addFeatures([ipointFeature]);


            markerLayer.addFeatures([ipointFeature]);
            MselectControl.select(this.markerLayer.features[kd]); }
            map.addLayer(markers);
           var size = new OpenLayers.Size(25, 25);
            var offset = new OpenLayers.Pixel(-(size.w / 2), -size.h);
            var icon = new OpenLayers.Icon('http://www.openlayers.org/api/img/zoom-world-mini.png', size, offset);
            markers.addMarker(new OpenLayers.Marker(new OpenLayers.LonLat(selectedAsset_Coordinates.d[k].vLongitude,            
selectedAsset_Coordinates.d[k].vLatitude), icon));

}

    //zoom to multiple co-ordinates
    map.zoomToExtent(markers.getDataExtent());
  

Open in new window



as i said above, it zooms to the sea and not the co-ordinates
is there a better way/ other way/ error above?

HELP!
0
Comment
Question by:websss
1 Comment
 
LVL 35

Accepted Solution

by:
Robert Schutt earned 500 total points
ID: 40445982
I can't really test this but it looks like, on line 72 you forgot to transform the marker positions like you did on the other points, should probably be:
markers.addMarker(new OpenLayers.Marker(new OpenLayers.LonLat(selectedAsset_Coordinates.d[k].vLongitude, selectedAsset_Coordinates.d[k].vLatitude).transform(WGS84, map.getProjectionObject()), icon));
//                                                                                                                                                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Open in new window

When I say I can't really test, I did test with some standard code, taking out a lot of your code and using mock-up data that hopefully comes close to what you're trying to do and got it to work with that extra transform, see: http://schutt.nl/ee/Q_28554813
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This is an explanation of a simple data model to help parse a JSON feed
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now