
实质: 整个地图都是以由 小的地图瓦片组成的,因此可以将这些小瓦片图片保存下来,在需要的时候读取即可。
过程: 客户端将地图的三维坐标发送回服务器,服务器经过计算后,返回对应的 图片。
API 下载地址: http://download.youkuaiyun.com/detail/iamwangch/4933837
Google的离线地图并未真正意义上的离线,由于代码过于凌乱,无法正确解读,所以采用了添加本地服务器的方法实现离线
过程:
- (一)创建项目并添加GoogleMapAPIV3 文件包
下载地址: http://www.cnblogs.com/liongis/archive/2011/04/28/2032316.html
- (二)修改文件包内的mapapi.js的myUrl变量为项目登录网址如:http: hostlocal:8080/xx
- (三)响应请求 项目获取有js发出的一个action的请求 如:
http://"+myUrl+"/mt0/vt.action?lyrs=m@152&src=apiv3&hl=zh-CN&x=216&y=104&z=8&s=
上面的请求有x,y,z的三个参数传进来,这三个参数就是地图瓦片的值
根据这三个参数传回瓦片图片即可。
- (四)经纬度 转换到 图片坐标
参考: http://code.google.com/intl/zh-CN/apis/maps/documentation/javascript/overlays.html#CustomMapTypes
服务器代码
下载
/**
* 保存文件
*/
private
void
SaveImage(
int
x,
int
y,
int
z,String basePath){
String outPath = basePath+
"/"
+x+
"-"
+y+
"-"
+z+
".png"
;
String urlStr =
"http://mt1.googleapis.com/vt?lyrs=m@203000000&src=apiv3&hl=zh-CN&"
+
"x="
+x+
"&y="
+y+
"&z="
+z+
"&s=Galileo&style=api%7Csmartmaps"
;
int
chByte = 0;
URL url =
null
;
HttpURLConnection httpConn =
null
;
InputStream in =
null
;
FileOutputStream out =
null
;
try
{
url =
new
URL(urlStr);
httpConn = (HttpURLConnection)
url.openConnection();
HttpURLConnection. setFollowRedirects(
true
);
httpConn.setRequestMethod(
"GET"
);
httpConn.setRequestProperty(
"User-Agent"
,
"Mozilla/4.0 (compatible; MSIE 6.0; Windows 2000)"
);
// logger.info(httpConn.getResponseMessage());
in = httpConn.getInputStream();
out =
new
FileOutputStream(
new
File(outPath));
chByte = in.read();
while
(chByte != -1) {
out.write(chByte);
chByte = in.read();
}
}
catch
(MalformedURLException e) {
e.printStackTrace();
}
catch
(IOException e) {
e.printStackTrace();
}
}
返回客户端
public
String doLoad()
throws
IOException {
HttpServletRequest request = ServletActionContext.getRequest();
HttpServletResponse response = ServletActionContext.getResponse();
String basePath =
""
;
//基本路径
basePath = request.getSession().getServletContext().getRealPath(
"/map-images"
);
//System.out.println("基本路径 ->"+basePath);
System.
out
.println(
"坐标: x:"
+
x
+
" y:"
+
y
+
" z:"
+
z
);
ByteArrayOutputStream byteArrayOutputStream =
new
ByteArrayOutputStream();
String path = basePath+
"/"
+
x
+
"-"
+
y
+
"-"
+
z
+
".png"
;
File file =
new
File(path);
// 读取指定路径的图片
InputStream in =
null
;
try
{
in =
new
FileInputStream(file);
}
catch
(FileNotFoundException e) {
this
.SaveImage(
x
,
y
,
z
, basePath);
in =
new
FileInputStream(file);
//e.printStackTrace();
System.
out
.println(
"文件不存在,需要下载."
);
}
int
tag = 0;
byte
[] b =
new
byte
[100];
while
((tag = in.read(b))!=-1) {
//System.out.println("读取到的大小:"+tag);
byteArrayOutputStream.write(b, 0, tag);
}
this
.
imageStream
=
new
ByteArrayInputStream(byteArrayOutputStream.toByteArray());
return
"success"
;
}