0

I have a PolyLine on my map and want to add a new co-ordinate to it when a user clicks between two existing points.

I can get the click event with:-

 MouseArea {
    id: mouseArea
    anchors.fill: parent
    acceptedButtons: Qt.LeftButton | Qt.RightButton
    onClicked: {
        console.log('LINE');
    }
}

But I cannot figure out how to work out the required index for insertCoordinate() as there does not appear to be a method to get the start/end vertices of the segment clicked. Is this possible?

CF-GISRAW
  • 73
  • 1
  • 5

2 Answers2

1

I had a similar problem. Currently it cannot be done without writing a new Map object type. So I've changed approach completely and done the following:-

  • stopped using QtLocation for the map as it is too restrictive at present
  • integrated a WebKit control with Leaflet as the map provider in the browser HTML
  • used WebChannel and the WebSocketServer to communicate with the map via the javascript API

This has given me all the flexibility I need on the map as Leaflet is easy to configure and extend whilst allowing me to write the rest of the desktop app in Qt

CitizenFish
  • 312
  • 2
  • 13
0

I've revisited this project and found a way to do it without using Webkit. It is quite involved:-

1) use the click to get a coordinate

var mapCoord = gpxLine.mapToItem(mapView,mouseX,mouseY);
var coord = mapView.toCoordinate(Qt.point(mapCoord.x,mapCoord.y));

2) use this coordinate to iterate through the path and calculate the path line segment that it is closest to

 float distance = 1000000;
    float dx = 0;
    int index = 0;
    float x0 = coordinate.longitude(),
          y0 = coordinate.latitude(),
          x1y1x,
          x1y1y,
          x2y2x,
          x2y2y;

    double A,B,C,D,dot,len_sq,param,xx,yy,d_x,d_y;

    for(int i = 0; i < trackpoints.count() - 1; i++){
        //Distance from line algorithm https://stackoverflow.com/questions/849211/shortest-distance-between-a-point-and-a-line-segment
        x1y1x = trackpoints[i].latlon.longitude();
        x1y1y = trackpoints[i].latlon.latitude();
        x2y2x = trackpoints[i+1].latlon.longitude();
        x2y2y = trackpoints[i+1].latlon.latitude();

        A = x0 - x1y1x;
        B = y0 - x1y1y;
        C = x2y2x - x1y1x;
        D = x2y2y - x1y1y;

        dot = A * C + B * D;
        len_sq = C * C + D * D;
        param = dot /len_sq;

        if(param < 0  || (x1y1x == x2y2x && x1y1y == x2y2y)){
            xx = x1y1x;
            yy = x1y1y;
        } else if ( param > 1 ){
            xx = x2y2x;
            yy = x2y2y;
        } else {
            xx = x1y1x +param * C;
            yy = x1y1y + param * D;
        }

        d_x = x0 - xx;
        d_y = y0 - yy;
        dx = sqrt(d_x * d_x + d_y * d_y);

        if(dx < distance){
            distance = dx;
            index = i;
        }

    }

3) this gives me the index so I can now insert the coordinate at this index

CitizenFish
  • 312
  • 2
  • 13