Solved

Openlayers - zooming to show multiple co-ordinates

Posted on 2014-11-10
1
243 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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
python question 5 76
Java Loop 6 61
Please help| Getting the syntax error in below query. 2 57
backup program with robocopy 6 18
This is an explanation of a simple data model to help parse a JSON feed
Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

778 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