Search code examples
androidmapsnutiteq

Polyline on nutiteq map onLocattionChanged


I have been working on nutiteq maps but I am unable to draw a polyline as my location changes.

I tried so far:

@Override
public void onLocationChanged(Location location)
{
    MapPos lineLocation = mapView.getLayers().getBaseProjection().fromWgs84(location.getLongitude(), location.getLatitude());
    //arr_lat_long.add(new MapPos(lat, lng));
    arr_lat_long.add(lineLocation);
    Toast.makeText(getApplicationContext(), "Array list lat Lng" + arr_lat_long, 1000).show();
    if (arr_lat_long.size() > 2)
    {
        GeometryLayer geoLayer = new GeometryLayer(new EPSG4326());
        mapView.getLayers().addLayer(geoLayer);
        LineStyle lineStyle = LineStyle.builder().setLineJoinMode(LineStyle.ROUND_LINEJOIN).build();
        //Label label = new DefaultLabel("Line", "Here is a line");
        Line line = new Line(arr_lat_long, null, lineStyle, null);
        geoLayer.add(line);
    }
}

Solution

  • The problem is that layer projection is EPSG4326, but you add lineLocation coordinates which are converted to baseProjection (projection of base layer), which is usually EPSG3857. As your geoLayer is already EPSG4326, and GPS coordinates are also EPSG4326 (WGS84), then this is enough:

    MapPos lineLocation = new MapPos(location.getLongitude(), location.getLatitude());
    

    Also: here you are adding new layer, define style and add new line (each next 1 point longer) for every GPS location coordinate, which happens every second. So you will get out of memory sooner or later. So I would suggest to rewrite your code: make geoLayer, lineStyle and line to fields. And update same line object across your app/mapview lifetime with:

    line.setVertexList(arr_lat_long);