转载请注明出处:/zkjthinking/article/details/77278838
由于公司需求需要在移动平台上加载自己发布的wms 服务:
高德地图加载wms谷歌地图加载wms天地图加载wms或者wmts
目前就做了这几个地图上面加载wms 服务的需求,所有只在这里写这三种;
1.高德地图加载wms:
代码:
//加载自定义wms在你的Activity 中加载 amap为mapview.getAmap();HeritageScopeTileProvider tileProvider = newHeritageScopeTileProvider();aMap.addTileOverlay(new TileOverlayOptions().tileProvider(tileProvider));
然后我们看这个自定义类中怎么写吧
public class HeritageScopeTileProvider extends UrlTileProvider {private String mRootUrl;//默认瓦片大小private static int titleSize = 256;//a=6378137±2(m)//基本参数private final double initialResolution= 156543.03392804062;//2*Math.PI*6378137/titleSize;private final double originShift= 7508.342789244;//2*Math.PI*6378137/2.0; 周长的一半private final double HALF_PI = Math.PI / 2.0;private final double RAD_PER_DEGREE = Math.PI / 180.0;private final double HALF_RAD_PER_DEGREE = Math.PI / 360.0;private final double METER_PER_DEGREE = originShift / 180.0;//一度多少米private final double DEGREE_PER_METER = 180.0 / originShift;//一米多少度public HeritageScopeTileProvider() {super(titleSize, titleSize);//地址写你自己的wms地址mRootUrl = "http://xxxxxx自己的/wms?LAYERS=cwh:protect_region_38_0830&FORMAT=image%2Fpng&TRANSPARENT=TRUE&SERVICE=WMS&VERSION=1.1.1&REQUEST=GetMap&STYLES=&SRS=EPSG%3A900913&BBOX=";}public HeritageScopeTileProvider(int i, int i1) {super(i, i1);}@Overridepublic URL getTileUrl(int x, int y, int level) {try {String url = mRootUrl + TitleBounds(x, y, level);return new URL(url);} catch (MalformedURLException e) {e.printStackTrace();}return null;}/*** 根据像素、等级算出坐标** @param p* @param zoom* @return*/private double Pixels2Meters(int p, int zoom) {return p * Resolution(zoom) - originShift;}/*** 根据瓦片的x/y等级返回瓦片范围** @param tx* @param ty* @param zoom* @return*/private String TitleBounds(int tx, int ty, int zoom) {double minX = Pixels2Meters(tx * titleSize, zoom);double maxY = -Pixels2Meters(ty * titleSize, zoom);double maxX = Pixels2Meters((tx + 1) * titleSize, zoom);double minY = -Pixels2Meters((ty + 1) * titleSize, zoom);//转换成经纬度minX=Meters2Lon(minX);minY=Meters2Lat(minY);maxX=Meters2Lon(maxX);maxY=Meters2Lat(maxY);PositionModel position1 = PositionUtil.gcj_To_Gps84(minY,minX);minX = position1.getWgLon();minY = position1.getWgLat();PositionModel position2 = PositionUtil.gcj_To_Gps84(maxY,maxX);maxX = position2.getWgLon();maxY = position2.getWgLat();minX=Lon2Meter(minX);minY=Lat2Meter(minY);maxX=Lon2Meter(maxX);maxY=Lat2Meter(maxY);return minX + "," + Double.toString(minY) + "," + Double.toString(maxX) + "," + Double.toString(maxY) + "&WIDTH=256&HEIGHT=256";}/*** 计算分辨率** @param zoom* @return*/private double Resolution(int zoom) {return initialResolution / (Math.pow(2, zoom));}/*** X米转经纬度*/private double Meters2Lon(double mx) {double lon = mx * DEGREE_PER_METER;return lon;}/*** Y米转经纬度*/private double Meters2Lat(double my) {double lat = my * DEGREE_PER_METER;lat = 180.0 / Math.PI * (2 * Math.atan(Math.exp(lat * RAD_PER_DEGREE)) - HALF_PI);return lat;}/*** X经纬度转米*/private double Lon2Meter(double lon) {double mx = lon * METER_PER_DEGREE;return mx;}/*** Y经纬度转米*/private double Lat2Meter(double lat) {double my = Math.log(Math.tan((90 + lat) * HALF_RAD_PER_DEGREE)) / (RAD_PER_DEGREE);my = my * METER_PER_DEGREE;return my;}}
看下效果
2.谷歌地图:
谷歌地图要想在Android 中使用,必须要下载谷歌的三驾马车,google play商店, google service, google服务框架,如果需要还要有个google 账号。下载个go 谷歌安装器自动帮你安装;
有了这些东西在手机然后下面的代码跑起来才会起作用:
代码:
//google tilelayer 添加的方式;GoogleHeritageScopeTileProvider tileProvidergoogle = new GoogleHeritageScopeTileProvider();mMap.addTileOverlay(new com.google.android.gms.maps.model.TileOverlayOptions().tileProvider(tileProvidergoogle));
看下自定义的代码
public class GoogleHeritageScopeTileProvider extends UrlTileProvider {private String mRootUrl;//默认瓦片大小private static int titleSize = 256;//a=6378137±2(m)//基本参数private final double initialResolution= 156543.03392804062;//2*Math.PI*6378137/titleSize;private final double originShift= 7508.342789244;//2*Math.PI*6378137/2.0; 周长的一半private final double HALF_PI = Math.PI / 2.0;private final double RAD_PER_DEGREE = Math.PI / 180.0;private final double HALF_RAD_PER_DEGREE = Math.PI / 360.0;private final double METER_PER_DEGREE = originShift / 180.0;//一度多少米private final double DEGREE_PER_METER = 180.0 / originShift;//一米多少度public GoogleHeritageScopeTileProvider() {super(titleSize, titleSize);mRootUrl = "http://xxxxx你自己要加载/wms?LAYERS=cwh:protect_region_38_0830&FORMAT=image%2Fpng&TRANSPARENT=TRUE&SERVICE=WMS&VERSION=1.1.1&REQUEST=GetMap&STYLES=&SRS=EPSG%3A900913&BBOX=";}public GoogleHeritageScopeTileProvider(int i, int i1) {super(i, i1);}@Overridepublic URL getTileUrl(int x, int y, int level) {try {String url = mRootUrl + TitleBounds(x, y, level);return new URL(url);} catch (MalformedURLException e) {e.printStackTrace();}return null;}/*** 根据像素、等级算出坐标** @param p* @param zoom* @return*/private double Pixels2Meters(int p, int zoom) {return p * Resolution(zoom) - originShift;}/*** 根据瓦片的x/y等级返回瓦片范围** @param tx* @param ty* @param zoom* @return*/private String TitleBounds(int tx, int ty, int zoom) {double minX = Pixels2Meters(tx * titleSize, zoom);double maxY = -Pixels2Meters(ty * titleSize, zoom);double maxX = Pixels2Meters((tx + 1) * titleSize, zoom);double minY = -Pixels2Meters((ty + 1) * titleSize, zoom);//转换成经纬度minX=Meters2Lon(minX);minY=Meters2Lat(minY);maxX=Meters2Lon(maxX);maxY=Meters2Lat(maxY);PositionModel position1 = PositionUtil.gcj_To_Gps84(minY,minX);minX = position1.getWgLon();minY = position1.getWgLat();PositionModel position2 = PositionUtil.gcj_To_Gps84(maxY,maxX);maxX = position2.getWgLon();maxY = position2.getWgLat();minX=Lon2Meter(minX);minY=Lat2Meter(minY);maxX=Lon2Meter(maxX);maxY=Lat2Meter(maxY);return minX + "," + Double.toString(minY) + "," + Double.toString(maxX) + "," + Double.toString(maxY) + "&WIDTH=256&HEIGHT=256";}/*** 计算分辨率** @param zoom* @return*/private double Resolution(int zoom) {return initialResolution / (Math.pow(2, zoom));}/*** X米转经纬度*/private double Meters2Lon(double mx) {double lon = mx * DEGREE_PER_METER;return lon;}/*** Y米转经纬度*/private double Meters2Lat(double my) {double lat = my * DEGREE_PER_METER;lat = 180.0 / Math.PI * (2 * Math.atan(Math.exp(lat * RAD_PER_DEGREE)) - HALF_PI);return lat;}/*** X经纬度转米*/private double Lon2Meter(double lon) {double mx = lon * METER_PER_DEGREE;return mx;}/*** Y经纬度转米*/private double Lat2Meter(double lat) {double my = Math.log(Math.tan((90 + lat) * HALF_RAD_PER_DEGREE)) / (RAD_PER_DEGREE);my = my * METER_PER_DEGREE;return my;}}
效果
3.天地图加载wms或者wmts;
//必须符合人家这个发布的规定,不符合加不上去的String url = "/DataServerT=vec_c&x=13394&y=2704&l=14";mTianDiTuMap.setCustomTileService(url);
看下源码吧
好了,可以看到加载的话,只能符合人家的规范。若不是,好像并没有好的解决方案。
总结: 从高德地图和谷歌地图添加wms,这里是不是有一些惊奇的发现了,这个添加的方式基本相同(甚至类名和方法名都相同),知道一种另一个也就会添加了。还有些更多的可以自己看看人家源码;当然高德和Google在国内都是使用GCJ-02坐标系。就是我们常说的“火星坐标系”,至于天地图这个加载wms和wmts 的方式必须符合人家的发布规定。我并没有找到可靠的在高德和谷歌地图加载wmts 的方法,如果我如果那位大牛知道。麻烦告诉我感激不尽。
demo 地址/zkjmyy/MapForWms