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

📄 map.java.svn-base

📁 利用J2ME编写的手机应用程序。 功能包括显示图片
💻 SVN-BASE
📖 第 1 页 / 共 2 页
字号:
		int dist = Integer.MAX_VALUE;
		MapOverlay closest = null;
		for (int i = 0; i < overlays.size(); i++)
		{
			MapOverlay mp = (MapOverlay)overlays.elementAt(i);
			Point pos = mp.getPos();
			if (pos == null) continue;
			
			int newdx = mp.getPos().x - cursorWorld.x;
			int newdy = cursorWorld.y - mp.getPos().y;
			
			if (sameSign(newdx, dx) && sameSign(newdy, dy))
			{
				int d = cursorWorld.distance2(mp.getPos());
				if (mp != cursorSelected && d < dist)
				{
					closest = mp;
					dist = d;
				}
			}
		}

		if (cursorSelected != null && listener != null)
			listener.onOverlayUnselected(cursorSelected, cursorPos);
		
		if (closest != null)
		{
			Point closestPos = worldToPixel(closest.getPos());
			dist = closestPos.distance2(new Point(cursorPos.x + (dx / 2), cursorPos.y + (dy / 2)));
		}

		if (closest != null && dist < cursorStep * cursorStep)
		{
			cursorPos = worldToPixel(closest.getPos());
			cursorSelected = closest;

			if (listener != null)
				listener.onOverlaySelected(cursorSelected, closest.getPos());
		}
		else
		{
			cursorSelected = null;
			cursorPos.x = Math.max(Math.min(cursorPos.x + dx, cxViewport - cursor.getWidth()), 1);
			cursorPos.y = Math.max(Math.min(cursorPos.y + dy, cyViewport - cursor.getHeight()), 1);
		}
		
		int xbounds = cxViewport / 4;
		int ybounds = cyViewport / 4;
		
		int dxmap = 0;
		int dymap = 0;
		
		if (cursorPos.x < xbounds)
		{
			dxmap = cursorPos.x - xbounds;
			cursorPos.x = xbounds;
		}
		else if (cursorPos.x > cxViewport - xbounds)
		{
			dxmap = cursorPos.x - (cxViewport - xbounds);
			cursorPos.x = cxViewport - xbounds;
		}
		if (cursorPos.y < ybounds)
		{
			dymap = cursorPos.y - ybounds;
			cursorPos.y = ybounds;
		}
		else if (cursorPos.y > cyViewport - ybounds)
		{
			dymap = cursorPos.y - (cyViewport - ybounds);
			cursorPos.y = cyViewport - ybounds;
		}
		
		if (dxmap != 0 || dymap != 0)
		{
			Point oldc = worldToPixel(this.x, this.y);
			oldc.x += dxmap;
			oldc.y += dymap;
			Point newc = pixelToWorld(oldc);
			
			if (dxmap != 0 && dymap != 0)
				this.setPosition(newc.x, newc.y);
			else if (dxmap != 0)
				this.setPosition(newc.x, this.y);
			else
				this.setPosition(this.x, newc.y);
		}
		
		int x = Math.min(old.x, cursorPos.x) - 3;
		int y = Math.min(old.y, cursorPos.y) - 3;
		int cx = (Math.max(old.x, cursorPos.x) - x) + cursor.getWidth();
		int cy = (Math.max(old.y, cursorPos.y) - y) + cursor.getHeight();
		this.repaint(x, y, cx, cy);
	}
	
	public void setPosition(int x, int y)
	{
		this.x = x;
		this.y = y;
		updateViewport();
	}
	
	public void setUserPosition(int x, int y)
	{
		userx = x;
		usery = y;
	}
	
	public int getZoom()
	{
		return zoomlevel;
	}
	
	public void notifyNewTile(MapTile tile)
	{
		if (tile.getZoom() == zoomlevel)
		{
			int x = dxViewport + tilesize * (tile.getX() - tilex);
			int y = dyViewport + tilesize * (tile.getY() - tiley);

			if (x >= -tilesize && x <= cxViewport && y >= -tilesize && y <= cyViewport)
			{
				tile.setVisibility(true);
				repaint(x, y, tilesize, tilesize);
			}
		}
	}
	
	public void notifyOverlayClick(MapOverlay overlay, Point pos)
	{
		if (listener != null)
			listener.onOverlayClick(overlay, pos);
	}
	
	public Point pixelToWorld(Point pixel)
	{
		return pixelToWorld(pixel.x, pixel.y);		
	}
	
	public Point pixelToWorld(int x, int y)
	{
		Point pt = new Point(0, 0);
		float rx = (((float)x - (float)dxViewport) / (float)tilesize) + (float)tilex;
		float ry = (((float)y - (float)dyViewport) / (float)tilesize) + (float)tiley;				
		pt.x = maxWest + (int)(rx * (float)tilesize_real[zoomlevel]);
		pt.y = maxNorth - (int)(ry * (float)tilesize_real[zoomlevel]);
		return pt;
	}

	public Point worldToPixel(Point world)
	{
		return worldToPixel(world.x, world.y);
	}
	
	public Point worldToPixel(int x, int y)
	{
		Point pt = new Point(0, 0);		
		int wx = x - maxWest;
		int wy = (maxNorth - maxSouth) - (y - maxSouth);
		float dx = (float)wx / (float)tilesize_real[zoomlevel];
		float dy = (float)wy / (float)tilesize_real[zoomlevel];
		pt.x = dxViewport + (int)((float)tilesize * (dx - (float)tilex));
		pt.y = dyViewport + (int)((float)tilesize * (dy - (float)tiley));		
		return pt;		
	}
	
	public void pointerPressed(int x, int y)
	{
		if (listener == null)
			return;
		
		if (this.cursorSelected != null)
		{
			listener.onOverlayClick(this.cursorSelected, new Point(x, y));
			return;
		}
		
		Point world = pixelToWorld(x, y);
		// check overlays
		for (int i = 0; i < overlays.size(); i++)
		{
			MapOverlay mo = (MapOverlay)overlays.elementAt(i);
			if (mo.hitTest(world))
			{
				listener.onOverlayClick(mo, world);
				return;
			}
		}
	}
	
	private void broadcastKeyPressed(int key)
	{
		for (int i = 0; i < keyListeners.size(); i++)
			((MapInputListener)keyListeners.elementAt(i)).onKeyPressed(key);
	}
	
	public void keyRepeated(int key)
	{
		int action = getGameAction(key);
		switch (action)
		{
			case Canvas.UP:
				broadcastKeyPressed(action);
				moveCursor(0, -cursorStep);
				break;
			case Canvas.DOWN:
				broadcastKeyPressed(action);
				moveCursor(0, cursorStep);
				break;
			case Canvas.LEFT:
				broadcastKeyPressed(action);
				moveCursor(-cursorStep, 0);
				break;
			case Canvas.RIGHT:
				broadcastKeyPressed(action);
				moveCursor(cursorStep, 0);
				break;
		}
	}
	
	public void keyPressed(int key)
	{
		int action = getGameAction(key);
		switch (action)
		{
			case Canvas.UP:
				broadcastKeyPressed(action);
				moveCursor(0, -cursorStep);				
				break;
			case Canvas.DOWN:
				broadcastKeyPressed(action);
				moveCursor(0, cursorStep);
				break;
			case Canvas.LEFT:
				broadcastKeyPressed(action);
				moveCursor(-cursorStep, 0);
				break;
			case Canvas.RIGHT:
				broadcastKeyPressed(action);
				moveCursor(cursorStep, 0);
				break;
			case Canvas.FIRE:
				broadcastKeyPressed(action);
				pointerPressed(cursorPos.x, cursorPos.y);
				break;
		}
		
		switch (key)
		{
			case -6:	// LEFT BUTTON
				broadcastKeyPressed(Canvas.FIRE);
				pointerPressed(cursorPos.x, cursorPos.y);
				break;
			case -7:	// RIGHT BUTTON
				broadcastKeyPressed(Canvas.GAME_A);
				break;
			case 35:	// *
				setZoom(getZoom() - 1);
				break;
			case 42:	// #
				setZoom(getZoom() + 1);
				break;
		}
	}

	public void locationUpdated(LocationProvider lp, Location loc)
	{
		QualifiedCoordinates coord = loc.getQualifiedCoordinates();
		Coordinate utm = Map.latLonToUTMXY(coord.getLatitude(), coord.getLongitude(), 32.0);
		locationProviderMethod = loc.getLocationMethod();

		gpslog += "m: ";
		if ((locationProviderMethod & Location.MTE_SATELLITE) != 0)
			gpslog += "SAT|";
		if ((locationProviderMethod & Location.MTA_ASSISTED) != 0)
			gpslog += "ASS|";
		if ((locationProviderMethod & Location.MTE_ANGLEOFARRIVAL) != 0)
			gpslog += "ANG|";
		gpslog += "\n";
		
		this.setUserPosition((int)utm.x, (int)utm.y);
		this.setPosition(userx, usery);
		gpslog += "pos: " + String.valueOf(userx) + ", " + String.valueOf(usery) + "\n";
	}

	public void providerStateChanged(LocationProvider lp, int newState)
	{
		this.locationProviderStatus = newState;
		switch (locationProviderStatus)
		{
			case LocationProvider.AVAILABLE:
				gpslog += "state = available\n";
				break;
			case LocationProvider.TEMPORARILY_UNAVAILABLE:
				gpslog += "state = temp\n";
				break;
			case LocationProvider.OUT_OF_SERVICE:
				gpslog += "state = out-of-service\n";
				break;
		}

		repaint();
	}
	
    public void providerSelectedEvent()
    {
    	locationProvider = ConfigProvider.getInstance().getSelectedProvider();
    	locationProviderStatus = locationProvider.getState();
    	locationProvider.setLocationListener(this, 10, 5, 5);
    }
    
    public void enableCursor()
    {
    	cursorEnabled = true;
    }

    public void disableCursor()
    {
    	cursorEnabled = false;
    }
	
	/*
	 * LATLON to UTM conversion math made statically available
	 */
    static double sm_a = 6378137.0;
    static double sm_b = 6356752.314;
    static double sm_EccSquared = 6.69437999013e-03;
    static double UTMScaleFactor = 0.9996;

    static double degToRad(double deg)
    {
        return (deg / 180.0 * Math.PI);
    }
    static double radToDeg(double rad)
    {
        return (rad / Math.PI * 180.0);
    }

    static double arcLengthOfMeridian(double phi)
    {
        double alpha, beta, gamma, delta, epsilon, n;
        double result;

        /* Precalculate n */
        n = (sm_a - sm_b) / (sm_a + sm_b);

        /* Precalculate alpha */
        alpha = ((sm_a + sm_b) / 2.0)
           * (1.0 + (n * n / 4.0) + (n * n * n * n / 64.0));

        /* Precalculate beta */
        beta = (-3.0 * n / 2.0) + (9.0 * n * n * n / 16.0)
           + (-3.0 * n * n * n * n * n / 32.0);

        /* Precalculate gamma */
        gamma = (15.0 * n * n / 16.0)
            + (-15.0 * n * n * n * n / 32.0);
    
        /* Precalculate delta */
        delta = (-35.0 * n * n * n / 48.0)
            + (105.0 * n * n * n * n * n / 256.0);
    
        /* Precalculate epsilon */
        epsilon = (315.0 * n * n * n * n / 512.0);
    
        /* Now calculate the sum of the series and return */
        result = alpha
        	* (phi + (beta * Math.sin (2.0 * phi))
            + (gamma * Math.sin (4.0 * phi))
            + (delta * Math.sin (6.0 * phi))
            + (epsilon * Math.sin (8.0 * phi)));

        return result;
    }

    static double UTMCentralMeridian(double zone)
    {
        double cmeridian;
        cmeridian = degToRad(-183.0 + (zone * 6.0));    
        return cmeridian;
    }

    static Coordinate mapLatLonToXY(double phi, double lambda, double lambda0)
    {
        double N, nu2, ep2, t, t2, l;
        double l3coef, l4coef, l5coef, l6coef, l7coef, l8coef;
 
        /* Precalculate ep2 */
        ep2 = ((sm_a * sm_a) - (sm_b * sm_b)) / (sm_b * sm_b);
    
        /* Precalculate nu2 */
        nu2 = ep2 * Math.cos(phi) * Math.cos(phi);
    
        /* Precalculate N */
        N = (sm_a * sm_a) / (sm_b * Math.sqrt (1.0 + nu2));

        /* Precalculate t */
        t = Math.tan(phi);
        t2 = t * t;
  
        /* Precalculate l */
        l = lambda - lambda0;
    
        /* Precalculate coefficients for l**n in the equations below
           so a normal human being can read the expressions for easting
           and northing
           -- l**1 and l**2 have coefficients of 1.0 */
        l3coef = 1.0 - t2 + nu2;
        l4coef = 5.0 - t2 + 9.0 * nu2 + 4.0 * (nu2 * nu2);
        l5coef = 5.0 - 18.0 * t2 + (t2 * t2) + 14.0 * nu2 - 58.0 * t2 * nu2;
        l6coef = 61.0 - 58.0 * t2 + (t2 * t2) + 270.0 * nu2 - 330.0 * t2 * nu2;
        l7coef = 61.0 - 479.0 * t2 + 179.0 * (t2 * t2) - (t2 * t2 * t2);
        l8coef = 1385.0 - 3111.0 * t2 + 543.0 * (t2 * t2) - (t2 * t2 * t2);

        double cosphi = Math.cos(phi);
        
        /* Calculate easting (x) */
        double x = N * cosphi * l
            + (N / 6.0 * (cosphi * cosphi * cosphi) * l3coef * l * l * l)
            + (N / 120.0 * (cosphi * cosphi * cosphi * cosphi * cosphi) * l5coef * l * l * l * l * l)
            + (N / 5040.0 * (cosphi * cosphi * cosphi * cosphi * cosphi * cosphi * cosphi) * l7coef * l * l * l * l * l * l * l);
    
        /* Calculate northing (y) */
        double y = arcLengthOfMeridian(phi)
            + (t / 2.0 * N * (cosphi * cosphi) * l * l)
            + (t / 24.0 * N * (cosphi * cosphi * cosphi * cosphi) * l4coef * l * l * l * l)
            + (t / 720.0 * N * (cosphi * cosphi * cosphi * cosphi * cosphi * cosphi) * l6coef * l * l * l * l * l * l)
            + (t / 40320.0 * N * (cosphi * cosphi * cosphi * cosphi * cosphi * cosphi * cosphi * cosphi) * l8coef * l * l * l * l * l * l * l * l);
        
        return new Coordinate(x, y, 0.0);
    }
        
    public static Coordinate latLonToUTMXY(double lat, double lon, double zone)
    {
    	Coordinate c = mapLatLonToXY(degToRad(lat), degToRad(lon), UTMCentralMeridian(zone));
        c.x = c.x * UTMScaleFactor + 500000.0;
        c.y *= UTMScaleFactor;
        if (c.y < 0.0)
            c.y += 10000000.0;
        return c;
    }

	public void querySuccess(ResultSet rs) 
	{
		while (rs.next())
			addMapTile(rs.getInt("filehandle"), rs.getInt("x"), rs.getInt("y"), zoomlevel);
		rs.close();
		computeVisibility();
	}

	public void xmlFailure(String error, Object param) {
	}

	public void xmlImageSuccess(Image image, Object param) {
	}

	public void xmlSuccess(KXmlParser xml, Object param) throws Exception {
	}
}

⌨️ 快捷键说明

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