WhirlyGlobe-Maply是一个基于OpenGL ES、专注移动应用的开源地图工具包,支持ios和android平台,本文以android平台为例。
环境:windows7 64位,Androidstudio 1.2.2,WhirlyGlobe-Maply(下载地址)
打开Androidstudio,新建一个名为“HelloEarth”的项目:
选择SDK
选择空的activity
保持默认名称MainActivity,也可以修改名称
点击Finish完成。
复制WhirlyGlobeMaply.aar(下载压缩包app/libs/WhirlyGlobeMaply.aar)到app/libs文件夹下:
打开build.gradle(Project:HelloEarth)文件,在allprojects里面添加如下内容:
打开build.gradle(Module:app)文件,在dependencies里面添加以下内容:
右键点击MainActivity,在项目窗口中选择一个新的空白的Fragment,命名为HelloGlobeFragment,去掉三个对勾
打开activity_main.xml,注释掉原有的HelloWorld文本框,改为以下内容
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
<!--<TextView android:text="@string/hello_world" android:layout_width="wrap_content"-->
<!--android:layout_height="wrap_content" />-->
<fragment
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:name="com.example.administrator.helloearth.HelloGlobeFragment"
android:id="@+id/fragment"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"></fragment>
</RelativeLayout>
打开HelloGlobeFragment.java,修改原有的代码为以下代码
package com.example.administrator.helloearth;
import android.os.Bundle;
import android.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.mousebird.maply.GlobeMapFragment;
import com.mousebird.maply.QuadImageTileLayer;
import com.mousebird.maply.RemoteTileInfo;
import com.mousebird.maply.RemoteTileSource;
import com.mousebird.maply.SphericalMercatorCoordSystem;
import java.io.File;
/**
* A simple {@link Fragment} subclass.
*/
public class HelloGlobeFragment extends GlobeMapFragment {
// public HelloGlobeFragment() {
// // Required empty public constructor
// }
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle inState) {
// TextView textView = new TextView(getActivity());
// textView.setText(R.string.hello_blank_fragment);
// return textView;
super.onCreateView(inflater,container,inState);
return baseControl.getContentView();
}
@Override
protected MapDisplayType chooseDisplayType(){
return MapDisplayType.Globe;
}
@Override
protected void controlHasStarted(){
// 设置瓦片地图
String cacheDirName="stamen_watercolor";
File cacheDir=new File(getActivity().getCacheDir(),cacheDirName);
cacheDir.mkdir();
RemoteTileSource remoteTileSource=new RemoteTileSource(new RemoteTileInfo("http://tile.stamen.com/watercolor/","png",0,18));
remoteTileSource.setCacheDir(cacheDir);
SphericalMercatorCoordSystem coordSystem=new SphericalMercatorCoordSystem();
// 当为全球显示时使用globeControl
// 当为地图显示时使用mapControl
QuadImageTileLayer baseLayer=new QuadImageTileLayer(globeControl,coordSystem,remoteTileSource);
baseLayer.setImageDepth(1);
baseLayer.setSingleLevelLoading(false);
baseLayer.setUseTargetZoomLevel(false);
baseLayer.setCoverPoles(true);
baseLayer.setHandleEdges(true);
// 添加图层并确定位置
globeControl.addLayer(baseLayer);
globeControl.animatePositionGeo(-3.6704803, 40.5023056, 5, 1.0);
}
}
成果如图,可以通过双击放大:
以上是创建球状地图,接下来我们创建二维地图:
如上述过程选择一个新的空白的Fragment,命名为HelloMapFragment,修改activity_main.xml内容
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
<!--<TextView android:text="@string/hello_world" android:layout_width="wrap_content"-->
<!--android:layout_height="wrap_content" />-->
<!--<fragment-->
<!--android:layout_width="fill_parent"-->
<!--android:layout_height="fill_parent"-->
<!--android:name="com.example.administrator.helloearth.HelloGlobeFragment"-->
<!--android:id="@+id/fragment"-->
<!--android:layout_centerVertical="true"-->
<!--android:layout_centerHorizontal="true"></fragment>-->
<fragment
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:name="com.example.administrator.helloearth.HelloMapFragment"
android:id="@+id/fragment"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"></fragment>
</RelativeLayout>
修改HelloMapFragment.java代码
package com.example.administrator.helloearth;
import android.os.Bundle;
import android.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.mousebird.maply.GlobeMapFragment;
import com.mousebird.maply.QuadImageTileLayer;
import com.mousebird.maply.RemoteTileInfo;
import com.mousebird.maply.RemoteTileSource;
import com.mousebird.maply.SphericalMercatorCoordSystem;
import java.io.File;
/**
* A simple {@link Fragment} subclass.
*/
public class HelloMapFragment extends GlobeMapFragment {
// public HelloGlobeFragment() {
// // Required empty public constructor
// }
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle inState) {
// TextView textView = new TextView(getActivity());
// textView.setText(R.string.hello_blank_fragment);
// return textView;
super.onCreateView(inflater,container,inState);
return baseControl.getContentView();
}
@Override
protected MapDisplayType chooseDisplayType(){
return MapDisplayType.Map;
}
@Override
protected void controlHasStarted(){
// 设置瓦片地图
String cacheDirName="stamen_watercolor";
File cacheDir=new File(getActivity().getCacheDir(),cacheDirName);
cacheDir.mkdir();
RemoteTileSource remoteTileSource=new RemoteTileSource(new RemoteTileInfo("http://tile.stamen.com/watercolor/","png",0,18));
remoteTileSource.setCacheDir(cacheDir);
SphericalMercatorCoordSystem coordSystem=new SphericalMercatorCoordSystem();
// 当为全球显示时使用globeControl
// 当为地图显示时使用mapControl
QuadImageTileLayer baseLayer=new QuadImageTileLayer(mapControl,coordSystem,remoteTileSource);
baseLayer.setImageDepth(1);
baseLayer.setSingleLevelLoading(false);
baseLayer.setUseTargetZoomLevel(false);
baseLayer.setCoverPoles(true);
baseLayer.setHandleEdges(true);
// 添加图层并确定位置
mapControl.addLayer(baseLayer);
mapControl.animatePositionGeo(103.994067, 30.770697, 5, 1.0);
mapControl.setAllowRotateGesture(true);
}
}
成果如图,同样双击放大:
完成!