googlemap.java

来自「内容为j2me 访问googlemap地图」· Java 代码 · 共 240 行

JAVA
240
字号
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
 
import javax.microedition.io.Connector;
import javax.microedition.io.HttpConnection;
import javax.microedition.lcdui.Image;
 
public class GoogleMaps
{
	String apiKey = null;
 
	//these 2 properties will be used with map scrolling methods. You can remove them if not needed
	int offset = 268435456;
	double radius = offset / Math.PI;
	
	public GoogleMaps(String apiKey)
	{
		this.apiKey = apiKey;
	}
	
	public double[] geocodeAddress(String address) throws Exception
	{
		byte[] res = loadHttpFile(getGeocodeUrl(address));
		
		String resString = new String(res, 0, res.length);
		
		String[] data = split(resString, ',');
		
		if(data[0].compareTo("200") != 0)
		{
			int errorCode = Integer.parseInt(data[0]);
			
			throw new Exception("Google Maps Exception: " + getGeocodeError(errorCode));
		}
		else
		{
			return new double[]{
				Double.parseDouble(data[2]),
				Double.parseDouble(data[3])
			};
		}
	}
	public Image retrieveStaticImage(int width, int height, double lat, double lng, int zoom, String format) throws Exception
	{
		byte[] imageData = loadHttpFile(getMapUrl(width, height, lng, lat, zoom, format));
		
		return Image.createImage(imageData, 0, imageData.length);
	}
	
	String getGeocodeError(int errorCode)
	{
		switch(errorCode)
		{
		case 400:
			return "Bad request";
		case 500:
			return "Server error";
		case 601:
			return "Missing query";
		case 602:
			return "Unknown address";
		case 603:
			return "Unavailable address";
		case 604:
			return "Unknown directions";
		case 610:
			return "Bad API key";
		case 620:
			return "Too many queries";
		default:
			return "Generic error";
		}
	}
	
	String getGeocodeUrl(String address)
	{
		return "http://maps.google.com/maps/geo?q=" + urlEncode(address) 
			+ "&output=csv&key=" + apiKey;
	}
	String getMapUrl(int width, int height, double lng, double lat, int zoom, String format)
	{
		return "http://maps.google.com/staticmap?center=" + 
			lat + "," + lng + "&format=" + format + "&zoom=" + zoom + "&size=" + 
			width + "x" + height + "&key=" + apiKey;
	}
	String urlEncode(String str)
	{
		StringBuffer buf = new StringBuffer();
		char c;
		for(int i = 0; i < str.length(); i++)
		{ 
			c = str.charAt(i);
			if ((c >= '0' && c <= '9')||
				(c >= 'A' && c <= 'Z')||
				(c >= 'a' && c <= 'z'))
			{
				buf.append(c);
			}
			else
			{
				buf.append("%").append(Integer.toHexString((int) str.charAt(i)));
			}
		} 
		return buf.toString();
	}
	byte[] loadHttpFile(String url) throws Exception
	{
		HttpConnection hc = null;
		
		InputStream is = null;                         
		
		byte[] byteBuffer = null;
		
		try
		{
			hc = (HttpConnection) Connector.open(url);
			
			hc.setRequestMethod(HttpConnection.GET);
	
			int ch;
	
			is = hc.openInputStream();
	
			int len = (int)hc.getLength();
			
			if(len > 0)
			{
				byteBuffer = new byte[len];
				
				is.read(byteBuffer);
			}
			else
			{
				ByteArrayOutputStream bos = new ByteArrayOutputStream();
				
				while ((ch = is.read()) != -1)
				{
					bos.write(ch);
				}
				byteBuffer = bos.toByteArray();
				
				bos.close();
			}
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		finally
		{
			try
			{
				if(is != null)
					is.close();
				
				if(hc != null)
					hc.close();
			}
			catch(Exception e2)
			{
				e2.printStackTrace();
			}
		}
		return byteBuffer;
	}
	static String[] split(String s, int chr)
	{
		Vector res = new Vector();
		
		int curr = 0;
		int prev = 0;
		
		while((curr = s.indexOf(chr, prev)) >= 0)
		{
			res.addElement(s.substring(prev, curr));
			
			prev = curr + 1;
		}
		res.addElement(s.substring(prev));
		
		String[] splitted = new String[res.size()];
		
		res.copyInto(splitted);
		
		return splitted;
	}
}




//____________________________
public double[] adjust(double lat, double lng, int deltaX, int deltaY, int z)
{
	return new double[]{
		XToL(LToX(lat) + (deltaX<<(21-z))),
		YToL(LToY(lng) + (deltaY<<(21-z)))
	};
}
double LToX(double x)
{
	return round(offset + radius * x * Math.PI / 180);
}
 
double LToY(double y)
{
	return round(
		offset - radius * 
		Double.longBitsToDouble(MicroDouble.log(
			Double.doubleToLongBits(
			(1 + Math.sin(y * Math.PI / 180))
			/
			(1 - Math.sin(y * Math.PI / 180))
			)
		)) / 2);
}
 
double XToL(double x)
{
	return ((round(x) - offset) / radius) * 180 / Math.PI;
}
 
double YToL(double y)
{
	return (Math.PI / 2 - 2 * Double.longBitsToDouble(
				MicroDouble.atan(
					MicroDouble.exp(Double.doubleToLongBits((round(y)-offset)/radius))
				)
			)) * 180 / Math.PI;
}
double round(double num)
{
	double floor = Math.floor(num);
	
	if(num - floor >= 0.5)
		return Math.ceil(num);
	else
		return floor;
}

⌨️ 快捷键说明

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