Solved

Openlayers - zooming to show multiple co-ordinates

Posted on 2014-11-10
1
235 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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

I know it’s not a new topic to discuss and it has lots of online contents already available over the net. But Then I thought it would be useful to this site’s visitors and can have online repository on vim most commonly used commands. This post h…
This is an explanation of a simple data model to help parse a JSON feed
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…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

772 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

12 Experts available now in Live!

Get 1:1 Help Now