1

I've a MapGuide map display with OpenLayers 3 like this:

var options = {
"minZoom": 0,
"maxZoom": 100,
"center": [mapInfos.center.x, mapInfos.center.y],
"extent": [mapInfos.extent.minX, mapInfos.extent.minY,   mapInfos.extent.maxX, mapInfos.extent.maxY]
};
var projectionUnit = mapInfos.projection.unit;
var projectionCode = mapInfos.projection.code;
var projection = new ol.proj.Projection({
  code: projectionCode,
  extent: [mapInfos.projection.extent.minX, mapInfos.projection.extent.minY, mapInfos.projection.extent.maxX, mapInfos.projection.extent.maxY],
  units: projectionUnit,
});
ol.proj.addProjection(projection);
options.projection = projectionCode
var imgMG = new ol.source.ImageMapGuide({
  url: me._agentUrl,
  useOverlay: true,
  useAsyncOverlay: true,
  metersPerUnit: me._metersPerUnit,
  params: {
    FORMAT: 'PNG',
    MAPNAME: me._mapName,
    behavior: 2,  
    SESSION: me._session
  },
  ratio: 2
});
var layer = new ol.layer.Image({
  source: imgMG,
});
me._map = new ol.Map({
  interactions: ol.interaction.defaults({
    doubleClickZoom: false,
    shiftDragZoom: false,
    altShiftDragRotate: false,
    keyboard: false,
    pinchRotate: false,
    pinchZoom: false,
  }),
  controls: [],
  layers: [layer],
  target: me._options.target,
  view: new ol.View(options)
});

And when I want to add a scaleline control to my map like this:

var scaleLineControl = new ol.control.ScaleLine({
  "className": this._control.className,
  "units": "metric" //To do dynamically with the map projection units /!\
});

I've an error: "transform should be defined"

Have you an idea to resolve my problem?

1

you need to add custom coordinate transform functions to your projection using the ol.proj.addCoordinateTransforms to work correctly with scale line. Check this example here. It deals exactly with your case.

Also cosider that if your projection is a known projection you may use the proj4js library and should reference all the necessary tasks for you.

Now as long as you know the projection code. Do the following

  1. Download the proj4js library form here

  2. Refernce the proj4-src.js file within your project

  3. Declare your projection , so proj4js is aware of it like so proj4.defs("EPSG:27562","+proj=lcc +lat_1=46.8 +lat_0=46.8 +lon_0=0 +k_0=0.99987742 +x_0=600000 +y_0=200000 +a=6378249.2 +b=6356515 +towgs84=-168,-60,320,0,0,0,0 +pm=paris +units=m +no_defs");

  4. Now you may use your new projection like so var myProjection = ol.proj.get('EPSG:27562');

  • Thanks for your answer but where can I find the transform calcul of a specific projection to EPSG:4326 ? Have I to use proj4 transform ? – VincentDEJ Nov 6 '15 at 9:21
  • Do you know the projection code??? Or is it just a custom projection of you?? If you know the EPSG code I may guide you how to use the proj4js lib to do your job. If not you need to find the formula that tranforms the lon,lat to x,y of your projection – pavlos Nov 6 '15 at 9:29
  • I know the projection code, I have it from mapguide api. And in my parameter to create my map, i define the projection code of my map and the proj4js definition, for example : "proj4": { "alias": "EPSG:27562", "definition": "+proj=lcc +lat_1=46.8 +lat_0=46.8 +lon_0=0 +k_0=0.99987742 +x_0=600000 +y_0=200000 +a=6378249.2 +b=6356515 +towgs84=-168,-60,320,0,0,0,0 +pm=paris +units=m +no_defs" } – VincentDEJ Nov 6 '15 at 9:35
  • I have edited my answer to guide you. – pavlos Nov 6 '15 at 9:44
  • Have I to replace my new ol.proj.Projection to proj4.defs and add the proj4 projection to ol.proj.addProjection(proj4Projection) ? – VincentDEJ Nov 6 '15 at 9:51
1

I've resolved my problem thanks to @palvos. So, this is the functionnal code :

var csProj4 = this._projections.getProj4Of(this._projections.getFromProj4(projectionCode));

proj4.defs(csProj4.alias, csProj4.definition);

options.projection = projectionCode
options.extent = [mapInfos.extent.minX, mapInfos.extent.minY, mapInfos.extent.maxX, mapInfos.extent.maxY];

var imgMG = new ol.source.ImageMapGuide({
  url: me._agentUrl,
  useOverlay: true,
  useAsyncOverlay: true,
  metersPerUnit: me._metersPerUnit,
  params: {
    FORMAT: 'PNG',
    MAPNAME: me._mapName,
    behavior: 2
    SESSION: me._session
  },
  ratio: 2
});

var layer = new ol.layer.Image({
  source: imgMG,
});

me._map = new ol.Map({
  interactions: ol.interaction.defaults({
    doubleClickZoom: false,
    shiftDragZoom: false,
    altShiftDragRotate: false,
    keyboard: false,
    pinchRotate: false,
    pinchZoom: false,
  }),
  controls: [],
  layers: [layer],
  target: me._options.target,
  view: new ol.View(options)
});

And for the scaleline :

var scaleLineControl = new ol.control.ScaleLine({});
map.addControl(scaleLineControl);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.