⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mapcanvas.java

📁 j2me写的google地图
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
            g.setColor(255, 0, 0);
            g.drawLine(x, y, x + sizeScr, y + sizeScr);
            g.drawLine(x, y + sizeScr, x + sizeScr, y);
            g.drawLine(x, y, x + sizeScr, y);
            g.drawLine(x, y, x, y + sizeScr);
            if(imaBuff != null && imaBuff.state == 2)
                drawString(g, "Loading...", x + 50, y + sizeScr / 2);
        }
    }

    public static String getCoordAsString(boolean isLon, Float theVal, boolean decimalMode)
    {
        StringBuffer str = new StringBuffer();
        System.out.println("TODO...");
        return str.toString();
    }

    public int getDist()
    {
        return getDist(true);
    }

    public int getDist(boolean doConvertInMeters)
    {
        long size = -1L;
        if(wayPoint != null)
        {
            int curLocX = px;
            int curLocY = py;
            if(m_lastLocation != null)
            {
                curLocX = m_lastLocation.px;
                curLocY = m_lastLocation.py;
            }
            int dx = wayPoint.px - curLocX;
            int dy = wayPoint.py - curLocY;
            if((dx != 0 || dy != 0) && doConvertInMeters)
                size = getSize((int)size / (1 << zoom));
        } else
        {
            size = -1L;
        }
        return (int)size;
    }

    public Float getCourseToTarget()
    {
        Float angle = null;
        if(wayPoint != null)
        {
            int curLocX = px;
            int curLocY = py;
            if(m_lastLocation != null)
            {
                curLocX = m_lastLocation.px;
                curLocY = m_lastLocation.py;
            }
            Float dy = new Float(-(wayPoint.py - curLocY));
            Float dx = new Float(-(wayPoint.px - curLocX));
            Float d = Float.sqrt(dx.Mul(dx).Add(dy.Mul(dy)));
            if(!d.Equal(new Float(0L)))
            {
                angle = Float.asin(dx).Div(d).Neg();
                if(dy.Less(0L))
                    angle = Float.PI.Sub(angle);
                angle = Float.toDegrees(angle);
            }
        }
        return angle;
    }

    public String convertDisToString(int inDist)
    {
        String sizef = null;
        if(inDist > 1999)
        {
            if(inDist > 0x30d40)
                sizef = inDist / 1000 + " km";
            else
                sizef = inDist / 1000 + "." + 2 + " km";
        } else
        {
            sizef = inDist + " m";
        }
        return sizef;
    }

    public void drawDirectiontoWayPoint(Graphics g)
    {
        if(wayPoint != null)
        {
            int dx = wayPoint.px - px;
            int dy = wayPoint.py - py;
            if(m_lastLocation != null)
            {
                dx = wayPoint.px - m_lastLocation.px;
                dy = wayPoint.py - m_lastLocation.py;
            }
            dx /= 1 << zoom;
            dy /= 1 << zoom;
            if(dx == 0 && dy == 0)
            {
                g.setColor(255);
                g.drawArc((offx + width / 2) - 20, (offy + height / 2) - 20, 40, 40, 0, 360);
                g.drawArc((offy + width / 2) - 19, (offy + height / 2) - 19, 38, 38, 0, 360);
            } else
            {
                int f = (int)Float.sqrt(new Float(dx * dx + dy * dy)).toLong();
                int size = getSize(f);
                String sizef = null;
                if(size > 1999)
                {
                    if(size > 0x30d40)
                        sizef = "Dist:" + size / 1000 + " km";
                    else
                        sizef = "Dist:" + size / 1000 + "." + (size / 100) % 10 + " km";
                } else
                {
                    sizef = "Dist:" + size + " m";
                }
                g.setColor(0);
                g.drawString(sizef, offx + width / 2 + 1, offy + 40 + 1, 17);
                g.setColor(0xffffff);
                g.drawString(sizef, offx + width / 2, offy + 40, 17);
                dx = (dx * 20) / f;
                dy = (dy * 20) / f;
                g.setColor(255);
                int cx = offx + (width / 2 + dx);
                int cy = offy + (height / 2 + dy);
                int d = f;
                if(d == 0)
                    d = 1;
                if(d < 60)
                {
                    cx += (dx * d) / 20 - 3 * dx;
                    cy += (dy * d) / 20 - 3 * dy;
                }
                int dx1 = dx / 4;
                int dy1 = dy / 4;
                g.fillTriangle(cx - dy1, cy + dx1, (cx + dx) - dy1, cy + dy + dx1, cx + dx + dy1, (cy + dy) - dx1);
                g.fillTriangle(cx - dy1, cy + dx1, cx + dx + dy1, (cy + dy) - dx1, cx + dy1, cy - dx1);
                cx += dx;
                cy += dy;
                g.fillTriangle(cx + dx, cy + dy, cx - dy / 2, cy + dx / 2, cx + dy / 2, cy - dx / 2);
                g.setColor(0);
                cx -= dx;
                cy -= dy;
                g.drawLine(cx - dy1, cy + dx1, (cx + dx) - dy1, cy + dy + dx1);
                g.drawLine((cx + dx) - dy1, cy + dy + dx1, (cx + dx) - dy / 2, cy + dy + dx / 2);
                g.drawLine(cx + 2 * dx, cy + 2 * dy, (cx + dx) - dy / 2, cy + dy + dx / 2);
                g.drawLine(cx + 2 * dx, cy + 2 * dy, cx + dx + dy / 2, (cy + dy) - dx / 2);
                g.drawLine(cx + dx + dy1, (cy + dy) - dx1, cx + dx + dy / 2, (cy + dy) - dx / 2);
                g.drawLine(cx + dy1, cy - dx1, cx + dx + dy1, (cy + dy) - dx1);
                g.drawLine(cx + dy1, cy - dx1, cx - dy1, cy + dx1);
            }
            wayPoint.paint(g, px, py, offx, offy, width, height, zoom);
            Float anglel = getCourseToTarget();
            dx = (int)Float.sin(anglel).Mul(50L).toLong();
            dy = -(int)Float.cos(anglel).Mul(50L).toLong();
            g.setColor(0xffff00);
            int cx = width / 2;
            int cy = height / 2;
        }
    }

    protected void paintMenu(Graphics g, String menu[])
    {
        if(m_modeShortkey)
        {
            int nbMenu = menu.length;
            int maxSize = 0;
            Font f = g.getFont();
            int off = f.getHeight() + 5;
            for(int i = 0; i < nbMenu; i++)
            {
                int size = f.stringWidth(menu[i]);
                if(size > maxSize)
                    maxSize = size;
            }

            int w = maxSize + 4;
            int h = f.getHeight() * nbMenu + 4;
            g.setColor(0xffffff);
            g.fillRoundRect(0, height - h - off, w, h, 6, 6);
            g.setColor(0);
            g.drawRoundRect(0, height - h - off, w, h, 6, 6);
            for(int i = 0; i < nbMenu; i++)
                g.drawString(menu[i], 2, (height - h - off) + 2 + i * f.getHeight(), 20);

        }
    }

    public String[] getMenusItem()
    {
        return menus;
    }

    public boolean onMenuSelected(int idx)
    {
        boolean processed = false;
        switch(idx)
        {
        default:
            break;

        case 1: // '\001'
            m_drawLon = !m_drawLon;
            if(m_drawLon)
                setInfoOnScreen("Display Lat/Lon");
            else
                setInfoOnScreen("Hide Lat/Lon");
            if(UtilMidp.DEBUG)
                BufferImage.dumpState();
            processed = true;
            break;

        case 2: // '\002'
            m_displaySize = !m_displaySize;
            if(m_displaySize)
                setInfoOnScreen("Show scale bar");
            else
                setInfoOnScreen("Remove scale bar");
            processed = true;
            break;

        case 3: // '\003'
            m_autoTrack = !m_autoTrack;
            break;

        case 4: // '\004'
            m_record = !m_record;
            if(!m_record)
                saveRecords();
            else
                curSavedTrack = Track.getTrack(null, "New Track");
            break;

        case 5: // '\005'
            if(m_record)
                m_pause = !m_pause;
            break;

        case 6: // '\006'
            BufferImage.m_doNotUseNetwork = !BufferImage.m_doNotUseNetwork;
            if(BufferImage.m_doNotUseNetwork)
                setInfoOnScreen("Network disabeld");
            else
                setInfoOnScreen("Network enabeld");
            break;

        case 7: // '\007'
            BufferImage.dumpState();
            break;
        }
        return processed;
    }

    public void recenterMapAroundSel()
    {
        recenterMapAroundSel(100);
    }

    public void recenterMapAroundSel(int percentage)
    {
        int miny = 0x2000000;
        int minx = 0x2000000;
        int maxy = 0xfe000000;
        int maxx = 0xfe000000;
        if(listPlaces.size() == 0)
        {
            if(m_segments == null)
                return;
            for(int i = 0; i < m_segments.length; i += 2)
            {
                if(m_segments[i + 1] > maxx)
                    maxx = m_segments[i + 1];
                if(m_segments[i + 1] < minx)
                    minx = m_segments[i + 1];
                if(m_segments[i] > maxy)
                    maxy = m_segments[i];
                if(m_segments[i] < miny)
                    miny = m_segments[i];
            }

        }
        Enumeration e = listPlaces.elements();
        do
        {
            if(!e.hasMoreElements())
                break;
            OneLoc l = (OneLoc)e.nextElement();
            if(l.px > maxx)
                maxx = l.px;
            if(l.py > maxy)
                maxy = l.py;
            if(l.px < minx)
                minx = l.px;
            if(l.py < miny)
                miny = l.py;
        } while(true);
        px = (maxx + minx) / 2;
        py = (maxy + miny) / 2;
        int zoomx = (int)(((long)(maxx - minx) * 1000L) / (long)(width * percentage));
        int zoomy = (int)(((long)(maxy - miny) * 1000L) / (long)(height * percentage));
        int lz = zoomx;
        if(zoomy > zoomx)
            lz = zoomy;
        lz = (lz + 5) / 10;
        int rz;
        for(rz = 0; 1 << rz < lz; rz++);
        if(rz > 16)
            rz = 16;
        setZoom(rz);
        if(UtilMidp.DEBUG)
            System.out.println("zoomx:" + zoomx + " zoomy:" + zoomy + " rz:" + rz);
    }

    public void drawString(Graphics g, String toDisp, int x, int y)
    {
        g.setColor(0, 0, 0);
        g.drawString(toDisp, x + 1, y + 1, 20);
        g.setColor(255, 255, 255);
        g.drawString(toDisp, x, y, 20);
    }

    public void goTo(String inPlace)
    {
        int pos = inPlace.indexOf(' ');
        Float px = Float.parse(inPlace.substring(0, pos), 10);
        int pos2 = inPlace.indexOf(' ', pos + 1);
        Float py = Float.parse(inPlace.substring(pos + 1, pos2), 10);
        pos = inPlace.indexOf(' ', pos2 + 1);
        int zoom = Integer.parseInt(inPlace.substring(pos2 + 1, pos));
        boolean isSatellite = inPlace.substring(pos + 1).startsWith("true");
        gotoLonLat(px, py, zoom, isSatellite);
    }

    public void goTo(OneLoc place)
    {
        gotoLonLat(place.m_lon, place.m_lat, place.zoom, place.isModeSat);
    }

    public static int xFromLon(Float inLon)
    {
        Float x = inLon.Add(F180).Div(180L).Mul(256L).Mul(0x10000L);
        return (int)x.toLong();
    }

    public static int yFromLat(Float inLat)
    {
        Float e = Float.sin(Float.toRadians(inLat));
        if(inLat.Less(-89L))
            e = (new Float(-99L)).Div(100L);
        if(!inLat.Less(89L))
            e = (new Float(99L)).Div(100L);
        Float pluse = F1.Add(e);
        Float moinse = F1.Sub(e);
        Float ln = Float.log(pluse.Div(moinse)).Neg().Div(Float.PImul2);
        Float y = ln.Add(F1).Mul(0x1000000L);
        return (int)y.toLong();
    }

    public void gotoLonLat(Float lon, Float lat, int zoom, boolean isSatellite)
    {
        if(UtilMidp.DEBUG)
            System.out.println("Goto  lon:" + lon + " lat:" + lat);
        int oldpx = px;
        int oldpy = py;
        int div = 1 << zoom;
        px = xFromLon(lon);
        py = yFromLat(lat);
        setZoom(zoom);
        m_modeSat = isSatellite;
        if(Math.abs((oldpx - px) / div) > getWidth() || Math.abs((oldpy - py) / div) > height)
            BufferImage.cleanBuffer();
    }

    public Float getLon()
    {
        return convLon(px);
    }

    public static Float convLon(int fromX)
    {
        Float lon = (new Float(fromX)).Mul(180L).Div(0x1000000L);
        lon = lon.Sub(F180);
        return lon;
    }

    public Float getLat()
    {
        return convLat(py);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -