English

How To: Convert pixel coordinate into map coordinate

Summary

Many user-interactive MapObjects Java applications need to get the location of mouse-click in the map. The location of a mouse-click can be returned from the MouseEvent object in terms of the Map object's pixel coordinate value. To perform various spatial functions, we need to convert this pixel coordinate into a map coordinate. This article outlines the steps to convert a pixel coordinate into a map coordinate.

Procedure

  1. Get the x and y location from the MouseEvent e:

    Code:
    int x1=e.getX();
    int y1=e.getY();


  2. Convert the integer values into double type:

    Code:
    double doubleX1=new Integer(x1).doubleValue();
    double doubleY1=new Integer(y1).doubleValue();


  3. Create a point object in pixel units:

    Code:
    com.esri.mo.cs.geom.Point pixelPoint= new com.esri.mo.cs.geom.Point(doubleX1,doubleY1);


  4. Get the Transform object from the Map and call the transform function:

    Code:
    com.esri.mo.cs.geom.Transform trans = map1.getPixelToWorldTransform();
    com.esri.mo.cs.geom.Point mapPoint= trans.transform(pixelPoint, null);


  5. Get the x and y location in map coordinates from the transformed point:

    Code:
    double mapX=mapPoint.x;
    double mapY=mapPoint.Y


    Note:
    Call getWorldToPixelTransform function on map object to transform Map Coordinate to Pixel Coordinate.