Solved

Openlayers - zooming to show multiple co-ordinates

Posted on 2014-11-10
1
245 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
groupNoAdj 7 115
allswap challenge 6 134
What does != "" mean in programming 8 133
VB.NET String Settings and Temp Folder Question 3 70
If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.

749 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