package com.googlemap.online;
import org.osmdroid.tileprovider.MapTile;
import org.osmdroid.tileprovider.tilesource.XYTileSource;
public class GoogleTileSource extends XYTileSource {
//影像的叠加层 lyrs=h
static final String[] baseUrl_GoogleLabel = new String[]{
"http://mt1.google.cn/vt/imgtp=png32&lyrs=h@210000000&hl=en-US&gl=US&src=app&s=G",
"http://mt2.google.cn/vt/imgtp=png32&lyrs=h@210000000&hl=en-US&gl=US&src=app&s=G",
"http://mt3.google.cn/vt/imgtp=png32&lyrs=h@210000000&hl=en-US&gl=US&src=app&s=G"
};
//矢量底图 lyrs=m lyrs=是指瓦片类型 有标注 在国内但有偏移,国外暂无测试
static final String[] baseUrl_GoogleRoad = new String[]{
"http://mt1.google.cn/vt/lyrs=m@209712068&hl=en-US&gl=US&src=app&s=G",
"http://mt2.google.cn/vt/lyrs=m@209712068&hl=en-US&gl=US&src=app&s=G",
"http://mt3.google.cn/vt/lyrs=m@209712068&hl=en-US&gl=US&src=app&s=G"
};
//影像底图 lyrs=y 有标注 在国内但有偏移,国外暂无测试
static final String[] baseUrl_Google_cn = new String[]{
"http://mt0.google.cn/vt/lyrs=y@126&hl=zh-CN&gl=cn&src=app&s=G",
"http://mt1.google.cn/vt/lyrs=y@126&hl=zh-CN&gl=cn&src=app&s=G",
"http://mt2.google.cn/vt/lyrs=y@126&hl=zh-CN&gl=cn&src=app&s=G",
"http://mt3.google.cn/vt/lyrs=y@126&hl=zh-CN&gl=cn&src=app&s=G"
};
//影像底图 lyrs=s 没有标注
static final String[] baseUrl_GoogleSatellite = new String[]{
"http://mt0.google.cn/vt/lyrs=s@126&hl=en-US&gl=US&src=app&s=G",
"http://mt1.google.cn/vt/lyrs=s@126&hl=en-US&gl=US&src=app&s=G",
"http://mt2.google.cn/vt/lyrs=s@126&hl=en-US&gl=US&src=app&s=G",
"http://mt3.google.cn/vt/lyrs=s@126&hl=en-US&gl=US&src=app&s=G"
};
String urlXYZ = "&x={$x}&y={$y}&z={$z}";
public GoogleTileSource() {
super("Google", 2, 22, 256, "png", baseUrl_GoogleRoad);
}
@Override
public String getTileURLString(MapTile aTile) {
String url = getBaseUrl() + urlXYZ
.replace("{$x}", aTile.getX() + "")
.replace("{$y}", aTile.getY() + "")
.replace("{$z}", aTile.getZoomLevel() + "");
return url;
}
}
--------------
package com.googlemap.online;
import android.app.Activity;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Toast;
import org.osmdroid.tileprovider.MapTileProviderBasic;
import org.osmdroid.tileprovider.tilesource.TileSourceFactory;
import org.osmdroid.util.GeoPoint;
import org.osmdroid.views.MapView;
import org.osmdroid.views.overlay.ScaleBarOverlay;
import org.osmdroid.views.overlay.TilesOverlay;
import org.osmdroid.views.overlay.compass.CompassOverlay;
import org.osmdroid.views.overlay.compass.InternalCompassOrientationProvider;
import org.osmdroid.views.overlay.gestures.RotationGestureOverlay;
import org.osmdroid.views.overlay.mylocation.GpsMyLocationProvider;
import org.osmdroid.views.overlay.mylocation.MyLocationNewOverlay;
public class MainActivity extends Activity implements View.OnClickListener {
private MapView mapView;
//et地图旋转
private RotationGestureOverlay mRotationGestureOverlay;
//比例尺
private ScaleBarOverlay mScaleBarOverlay;
//指南针方向
private CompassOverlay mCompassOverlay = null;
//设置导航图标的位置
private MyLocationNewOverlay mLocationOverlay;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView() {
findViewById(R.id.button1).setOnClickListener(this);
mapView = (MapView) findViewById(R.id.mymapview);
mapView.setDrawingCacheEnabled(true);
mapView.setMaxZoomLevel(22);
mapView.setMinZoomLevel(6);
mapView.getController().setZoom(12);
//加载谷歌地图,设置地图数据源的形式
mapView.setTileSource(new GoogleTileSource());
mapView.setUseDataConnection(true);
mapView.setMultiTouchControls(true);// 触控放大缩小
//是否显示地图数据源
mapView.getOverlayManager().getTilesOverlay().setEnabled(true);
//地图自由旋转
mRotationGestureOverlay = new RotationGestureOverlay(mapView);
mRotationGestureOverlay.setEnabled(true);
mapView.getOverlays().add(this.mRotationGestureOverlay);
//比例尺配置
final DisplayMetrics dm = getResources().getDisplayMetrics();
mScaleBarOverlay = new ScaleBarOverlay(mapView);
mScaleBarOverlay.setCentred(true);
mScaleBarOverlay.setAlignBottom(true); //底部显示
mScaleBarOverlay.setScaleBarOffset(dm.widthPixels / 5, 80);
mapView.getOverlays().add(this.mScaleBarOverlay);
//指南针方向
mCompassOverlay = new CompassOverlay(this, new InternalCompassOrientationProvider(this),
mapView);
mCompassOverlay.enableCompass();
mapView.getOverlays().add(this.mCompassOverlay);
//设置导航图标
this.mLocationOverlay = new MyLocationNewOverlay(new GpsMyLocationProvider(this),
mapView);
mapView.getOverlays().add(this.mLocationOverlay);
mLocationOverlay.enableMyLocation(); //设置可视
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button1:
//定位当前的位置,并设置缩放级别
mapView.getController().setZoom(18);
mapView.getController().setCenter(new GeoPoint(23.12648183, 113.365548756));
break;
default:
break;
}
}
//增加图层的形式
public void initExtendMap() {
MapTileProviderBasic mapTileProviderBasic =
new MapTileProviderBasic(this, new GoogleTileSource());
TilesOverlay tilesOverlay = new TilesOverlay(mapTileProviderBasic, this);
mapView.getOverlays().add(tilesOverlay);
}
@Override
public void onPause() {
super.onPause();
}
@Override
public void onResume() {
super.onResume();
}
}