//public String replace(String s, String one, String another) {
    //    // In a string replace one substring with another
    //    if (s.Equals(""))
    //        return "";
    //    String res = "";
    //    int i = s.IndexOf(one, 0);
    //    int lastpos = 0;
    //    while (i != -1) {
    //        res += s.Substring(lastpos, i) + another;
    //        lastpos = i + one.Length();
    //        i = s.IndexOf(one, lastpos);
    //    }
    //    res += s.Substring(lastpos); // the rest
    //    return res;
    //}

    /**
     * distance(p0, p1, p2) computes the distance between the point p0 and the
     * segment [p1,p2]. This could probably be replaced with something that is a
     * bit more numerically stable.
     *
     * @param p0
     * @param p1
     * @param p2
     * @return
     */
    public double distance(Trackpoint p0, Trackpoint p1, Trackpoint p2)
    {
        double u, result = 0.0;

        if (p1.getLatDouble() == p2.getLatDouble() &&
            p1.getLonDouble() == p2.getLonDouble())
        {
            result = Math.Sqrt(Math.Pow(p2.getLatDouble() - p0.getLatDouble(), 2)
                               + Math.Pow(p2.getLonDouble() - p0.getLonDouble(), 2));
        }
        else
        {
            u = ((p0.getLatDouble() - p1.getLatDouble())
                 * (p2.getLatDouble() - p1.getLatDouble()) + (p0
                                                              .getLonDouble() - p1.getLonDouble())
                 * (p2.getLonDouble() - p1.getLonDouble()))
                / (Math.Pow(p2.getLatDouble() - p1.getLatDouble(), 2) + Math
                   .Pow(p2.getLonDouble() - p1.getLonDouble(), 2));

            if (u <= 0)
            {
                result = Math.Sqrt(Math.Pow(p0.getLatDouble() - p1.getLatDouble(),
                                            2)
                                   + Math.Pow(p0.getLonDouble() - p1.getLonDouble(), 2));
            }
            if (u >= 1)
            {
                result = Math.Sqrt(Math.Pow(p0.getLatDouble() - p2.getLatDouble(),
                                            2)
                                   + Math.Pow(p0.getLonDouble() - p2.getLonDouble(), 2));
            }
            if (0 < u && u < 1)
            {
                result = Math.Sqrt(Math.Pow(p0.getLatDouble() - p1.getLatDouble()
                                            - u * (p2.getLatDouble() - p1.getLatDouble()), 2)
                                   + Math.Pow(p0.getLonDouble() - p1.getLonDouble() - u
                                              * (p2.getLonDouble() - p1.getLonDouble()), 2));
            }
        }
        return(result);
    }
    private String createEncodings(List <Trackpoint> points, double[] dists)
    {
        StringBuilder encodedPoints = new StringBuilder();

        double maxlat = 0, minlat = 0, maxlon = 0, minlon = 0;

        int plat = 0;
        int plng = 0;

        for (int i = 0; i < points.Count; i++)
        {
            // determin bounds (max/min lat/lon)
            if (i == 0)
            {
                maxlat = minlat = points[i].getLatDouble();
                maxlon = minlon = points[i].getLonDouble();
            }
            else
            {
                if (points[i].getLatDouble() > maxlat)
                {
                    maxlat = points[i].getLatDouble();
                }
                else if (points[i].getLatDouble() < minlat)
                {
                    minlat = points[i].getLatDouble();
                }
                else if (points[i].getLonDouble() > maxlon)
                {
                    maxlon = points[i].getLonDouble();
                }
                else if (points[i].getLonDouble() < minlon)
                {
                    minlon = points[i].getLonDouble();
                }
            }

            if (dists[i] != 0 || i == 0 || i == points.Count - 1)
            {
                Trackpoint point = points[i];

                int late5 = floor1e5(point.getLatDouble());
                int lnge5 = floor1e5(point.getLonDouble());

                int dlat = late5 - plat;
                int dlng = lnge5 - plng;

                plat = late5;
                plng = lnge5;

                encodedPoints.Append(encodeSignedNumber(dlat));
                encodedPoints.Append(encodeSignedNumber(dlng));
            }
        }

        Dictionary <String, Double> bounds = new Dictionary <String, Double>();

        bounds.Add("maxlat", maxlat);
        bounds.Add("minlat", minlat);
        bounds.Add("maxlon", maxlon);
        bounds.Add("minlon", minlon);

        this.setBounds(bounds);
        return(encodedPoints.ToString());
    }