代码如下:
package com.j2me.google;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
/**
* @author funny
*
* 用以下URL求城市的经纬度,把经纬度放入函数retrieveStaticImage运算
* http://maps.google.com/maps/geo?q=shanghai&output=csv&key=ABQIAAAAxXXppGwQikP5W03kMkXwTBRwZeWwBJuDeBDs7Xe14alLLfNA1RSipLkOi1Os9IQgWszFCqI6nbQUwA
*
* 用以下URL输出城市地图
* http://maps.google.com/staticmap?center=31.2243531,121.4759159&format=png32&zoom=8&size=320x240&key=ABQIAAAAxXXppGwQikP5W03kMkXwTBRwZeWwBJuDeBDs7Xe14alLLfNA1RSipLkOi1Os9IQgWszFCqI6nbQUwA
*/
public class MyGCanvas extends Canvas{
private GoogleMaps gMap=null;
private String apiKey="ABQIAAAAxXXppGwQikP5W03kMkXwTBRwZeWwBJuDeBDs7Xe14alLLfNA1RSipLkOi1Os9IQgWszFCqI6nbQUwA";
//常州的经纬度
private double lng=31.7558405;
private double lat=119.9392140;
//放大的倍数
private int zoom=12;
//调整位置的经纬度
double[] cs;
public MyGCanvas()
{
gMap=new GoogleMaps(apiKey);
cs=new double[]{lng,lat};
}
protected void paint(Graphics g) {
try {
Image image=gMap.retrieveStaticImage(this.getWidth(),this.getHeight(), lng,lat, zoom, "jpg");
g.drawImage(image,0,0,g.TOP|g.LEFT);
} catch (Exception e) {
//e.printStackTrace();
}
}
protected void keyPressed(int keycode) {
/*System.out.println(keycode);
flag=true;*/
//switch (this.getGameAction(keycode)) {
String actionName=this.getKeyName(keycode);
int action=this.getGameAction(keycode);
if(action==Canvas.UP){
lat=MapUtil.adjust(lat, lng, 0, -30, zoom)[0];
lng=MapUtil.adjust(lat, lng, 0, -30, zoom)[1];
}
else if(action==Canvas.DOWN){
lat=MapUtil.adjust(lat, lng, 0, 30, zoom)[0];
lng=MapUtil.adjust(lat, lng, 0, 30, zoom)[1];
}
else if(action==Canvas.LEFT){
lat=MapUtil.adjust(lat, lng, -30, 0, zoom)[0];
lng=MapUtil.adjust(lat, lng, -30, 0, zoom)[1];
}
else if(action==Canvas.RIGHT){
lat=MapUtil.adjust(lat, lng, 30, 0, zoom)[0];
lng=MapUtil.adjust(lat, lng, 30, 0, zoom)[1];
}
else if(actionName.equals("1")){
if(zoom<17)
zoom++;
}
else if(actionName.equals("3")){
if(zoom>1)
zoom--;
}
//lat=cs[0];
//lng=cs[1];
//cs=MapUtil.adjust(lat, lng, 30, 0, zoom--);
repaint();
}
}
package com.j2me.google;
import net.dclausen.microfloat.MicroDouble;
public class MapUtil {
private static int offset = 268435456;
private static double radius = offset / Math.PI;
/**
* Utility method for map scrolling
* If you need to scroll your map, you'll need to calculate a new center for your static image. The following adjust() method will return the new map center latitude and longitude, accepting the following arguments:
* the current center latitute and longitude coordinates the deltaX and deltaY, in pixels, of new map center
* the map zoom level
* @param lat
* @param lng
* @param deltaX
* @param deltaY
* @param z zoom level
* @return
*/
public static 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)))
};
}
private static double LToX(double x)
{
return round(offset + radius * x * Math.PI / 180);
}
private static 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);
}
private static double XToL(double x)
{
return ((round(x) - offset) / radius) * 180 / Math.PI;
}
private static double YToL(double y)
{
return (Math.PI / 2 - 2 * Double.longBitsToDouble(
MicroDouble.atan(
MicroDouble.exp(Double.doubleToLongBits((round(y)-offset)/radius))
)
)) * 180 / Math.PI;
}
private static double round(double num)
{
double floor = Math.floor(num);
if(num - floor >= 0.5)
return Math.ceil(num);
else
return floor;
}
}
本文来自优快云博客,转载请标明出处:http://blog.youkuaiyun.com/mxz55/archive/2009/01/10/3747704.aspx
package com.j2me.google;
import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
/**
* @author funny
*
* 用以下URL求城市的经纬度,把经纬度放入函数retrieveStaticImage运算
* http://maps.google.com/maps/geo?q=shanghai&output=csv&key=ABQIAAAAxXXppGwQikP5W03kMkXwTBRwZeWwBJuDeBDs7Xe14alLLfNA1RSipLkOi1Os9IQgWszFCqI6nbQUwA
*
* 用以下URL输出城市地图
* http://maps.google.com/staticmap?center=31.2243531,121.4759159&format=png32&zoom=8&size=320x240&key=ABQIAAAAxXXppGwQikP5W03kMkXwTBRwZeWwBJuDeBDs7Xe14alLLfNA1RSipLkOi1Os9IQgWszFCqI6nbQUwA
*/
public class MyGCanvas extends Canvas{
private GoogleMaps gMap=null;
private String apiKey="ABQIAAAAxXXppGwQikP5W03kMkXwTBRwZeWwBJuDeBDs7Xe14alLLfNA1RSipLkOi1Os9IQgWszFCqI6nbQUwA";
//常州的经纬度
private double lng=31.7558405;
private double lat=119.9392140;
//放大的倍数
private int zoom=12;
//调整位置的经纬度
double[] cs;
public MyGCanvas()
{
gMap=new GoogleMaps(apiKey);
cs=new double[]{lng,lat};
}
protected void paint(Graphics g) {
try {
Image image=gMap.retrieveStaticImage(this.getWidth(),this.getHeight(), lng,lat, zoom, "jpg");
g.drawImage(image,0,0,g.TOP|g.LEFT);
} catch (Exception e) {
//e.printStackTrace();
}
}
protected void keyPressed(int keycode) {
/*System.out.println(keycode);
flag=true;*/
//switch (this.getGameAction(keycode)) {
String actionName=this.getKeyName(keycode);
int action=this.getGameAction(keycode);
if(action==Canvas.UP){
lat=MapUtil.adjust(lat, lng, 0, -30, zoom)[0];
lng=MapUtil.adjust(lat, lng, 0, -30, zoom)[1];
}
else if(action==Canvas.DOWN){
lat=MapUtil.adjust(lat, lng, 0, 30, zoom)[0];
lng=MapUtil.adjust(lat, lng, 0, 30, zoom)[1];
}
else if(action==Canvas.LEFT){
lat=MapUtil.adjust(lat, lng, -30, 0, zoom)[0];
lng=MapUtil.adjust(lat, lng, -30, 0, zoom)[1];
}
else if(action==Canvas.RIGHT){
lat=MapUtil.adjust(lat, lng, 30, 0, zoom)[0];
lng=MapUtil.adjust(lat, lng, 30, 0, zoom)[1];
}
else if(actionName.equals("1")){
if(zoom<17)
zoom++;
}
else if(actionName.equals("3")){
if(zoom>1)
zoom--;
}
//lat=cs[0];
//lng=cs[1];
//cs=MapUtil.adjust(lat, lng, 30, 0, zoom--);
repaint();
}
}
package com.j2me.google;
import net.dclausen.microfloat.MicroDouble;
public class MapUtil {
private static int offset = 268435456;
private static double radius = offset / Math.PI;
/**
* Utility method for map scrolling
* If you need to scroll your map, you'll need to calculate a new center for your static image. The following adjust() method will return the new map center latitude and longitude, accepting the following arguments:
* the current center latitute and longitude coordinates the deltaX and deltaY, in pixels, of new map center
* the map zoom level
* @param lat
* @param lng
* @param deltaX
* @param deltaY
* @param z zoom level
* @return
*/
public static 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)))
};
}
private static double LToX(double x)
{
return round(offset + radius * x * Math.PI / 180);
}
private static 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);
}
private static double XToL(double x)
{
return ((round(x) - offset) / radius) * 180 / Math.PI;
}
private static double YToL(double y)
{
return (Math.PI / 2 - 2 * Double.longBitsToDouble(
MicroDouble.atan(
MicroDouble.exp(Double.doubleToLongBits((round(y)-offset)/radius))
)
)) * 180 / Math.PI;
}
private static double round(double num)
{
double floor = Math.floor(num);
if(num - floor >= 0.5)
return Math.ceil(num);
else
return floor;
}
}
本文来自优快云博客,转载请标明出处:http://blog.youkuaiyun.com/mxz55/archive/2009/01/10/3747704.aspx