本系列教程将分为两部分,第一部分是指导用户使用Mapview控件进行编程,其中包括了如何得到Google Map API,如何使用该API进行简单的开发,如何得到用户当前所在的位置。第二部分则包括如何在地图上,用第三方的组件库,实现气球式显示若干指定位置的功能。html
步骤1 建立新的Android 工程java
首先打开eclipse新创建一个Android 工程,其中相关参数设置以下:android
Project name:MallFindergit
Build Target: Google APIs Platform – 2.1 API Level 7编程
Application Name: Mall Finderapi
Package Name: com.shawnbe.mallfinder安全
Create Activity: MallFinderActivityapp
MinimumSDK: 7eclipse
以下图所示:ide
步骤2 注册Google Map API key
因为在使用google map的时候,须要使用google map api的key,所以须要先注册一个开发者key,能够到以下地址进行注册:http://code.google.com/android/add-ons/google-apis/mapkey.html ,其中须要咱们先产生开发期间的md5 密纹才能完成注册,所以咱们先学习如何生成一个MD5密纹。
咱们须要使用keytool工具,不使用传统的命令行方式下那枯燥的证书签名生成办法,而是直接在eclipse下经过插件进行完成,详细见步骤3
步骤3 安装keytool插件
在eclipse的Help菜单中,以下图,选择安装新软件:
在安装地址中输入以下地址:http://www.keytool.sourceforge.net/update ,以下图
接下来会加载相关的安装程序,并显示用户协议,选择接受全部用户协议后进行安装,安装成功后从新启动eclipse便可生效。
步骤4 产生开发期间的MD5密钥
在从新启动eclipse后,会发现工具栏多了以下图的keytool菜单
如今,咱们打开debug.keystore,注意其位置回因操做系统不一样而有不一样
Windows Vista : C:\Users\\.android\debug.keystore
Windows XP : C:\Documents and Settings\\.android\debug.keystore
OS X 和 Linux : ~/.android/debug.keystore
点keytool菜单中,选择open keystore,根据提示,选择当前系统所在的debug.keystore位置并打开,以下图,
其中,输入密码默认为android便可,并点Load加载。以后会在eclipse中出现新的keytool的视图,以下图所示:
双击打开androiddebugkey,复制其md5密纹,而后访问页面
http://code.google.com/android/maps-api-signup.html ,接受其协议,将md5密纹复制进去,再点Generate API Key,即产生google map的api key,记得保存好这个KEY,在接下来的步骤中要使用。
步骤5 增长MapView控件
接下来,咱们能够往布局文件中增长MapView控件。咱们在main.xml中添加以下代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<com.google.android.maps.MapView
android:id="@+id/mapView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:clickable="true"
android:apiKey="你的GOOGLE MAP API KEY "/>
</FrameLayout>
</LinearLayout>
在这个控件中,请注意要在android:apiKey的位置填入刚申请的google map api key。
步骤6 设置相关的权限
因为咱们的应用须要调用Google Map的数据,以及经过手机的GPS得到相关的其余地理位置数据,所以咱们必须在Android的Manifest文件中进行权限的设置。
咱们打开AndroidManifest.xml文件,而后增长以下代码所示的权限设置,注意添加在标签后,但要在标签前。
<uses-feature android:name="android.hardware.location.gps"/> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/> <uses-permission android:name="android.permission.INTERNET"/>
在这里,咱们分别调用了gps位置服务权限,使用互联网的权限以及使用最佳位置查找的权限(FINE_LOCATION)。在本文中,其实没用到FINE_LOCATION,但只是告诉开发者能够调整使用不一样的地理位置提供者(providers)以提供准确的位置服务,若是要有比较精确的地理位置服务,那么能够设置android.permisson.ACCESS_FINE_LOCATION服务。要注意的是在开发中,不要过多引用一些不须要使用的权限设置,不然会给用户带来担心安全等问题。要是只想调用通常的位置服务,可使用普通的android.permission.ACCESS_COARSE_LOCATION权限。
为了在应用中使用Google Map,须要在Manifest文件中包含相关的类库文件,因此加入以下代码:
<uses-library android:required="true" android:name="com.google.android.maps" />
为了视觉的美观,咱们把标题栏也去掉,以便留给地图更大的空间,因此设置为
android:theme="@android:style/Theme.NoTitleBar"
下面是完整的Manifest文件代码:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.shawnbe.mallfinder"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="7" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@android:style/Theme.NoTitleBar">
<activity
android:label="@string/app_name"
android:name=".MallFinderActivity" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<uses-library android:required="true" android:name="com.google.android.maps" />
</application>
<uses-feature android:name="android.hardware.location.gps"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.INTERNET"/>
</manifest>
步骤7 设置MapView
下面对主activity程序(MallFinderActivity.java)进行修改,首先让其继承MapActivity类,以下:
public class MallFinderActivity extends MapActivity {
因为继承了MapActivity类,所以必须实现isRouteDisplayed方法,这个方法是用来作路线导航用的,这里咱们不须要,所以只须要简单返回false便可:
@Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
接下来咱们就能够声明MapController控件,并对其进行相关属性的设置了,首先是声明控件
private MapController mapController;
private MapView mapView;
并在oncreate方法中进行属性设置以下:
mapView = (MapView)findViewById(R.id.mapView);
mapView.setBuiltInZoomControls(true);
mapView.setSatellite(false);
mapView.setStreetView(true);
mapController = mapView.getController();
mapController.setZoom(13);
在上面的代码中,设置了地图显示的方式为街道模式(经过设置mapview的setStreetView属性值为true),而且经过mapView.setBuiltInZoomControls(true); 使用了系统内置的放大缩小功能,并设置了地图的放大缩小倍数为13。
这个时候咱们就能够选择运行应用,看下是否符合咱们的初步预期效果,运行后效果以下图:
步骤8 得到当前所在位置
在LBS应用中,十分重要的工做是要得到当前设备所在的位置,这个可使用locationManager类实现,在得到当前位置的时候是须要花费一些时间的。咱们继续声明两个参数变量以下:
private LocationManager locationManager;
private GeoPoint currentLocation;
分别声明了LocationManager类的实例和GeoPoint类的实例(用于下文中的经纬度的计算)。
再增长以下几个方法:
public void getLastLocation(){
String provider = getBestProvider();
currentLocation = locationManager.getLastKnownLocation(provider);
if(currentLocation != null){
setCurrentLocation(currentLocation);
}
else
{
Toast.makeText(this, "Location not yet acquired", Toast.LENGTH_LONG).show();
}
}
public void animateToCurrentLocation(){
if(currentPoint!=null){
mapController.animateTo(currentPoint);
}
}
public String getBestProvider(){
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
criteria.setPowerRequirement(Criteria.NO_REQUIREMENT);
criteria.setAccuracy(Criteria.NO_REQUIREMENT);
String bestProvider = locationManager.getBestProvider(criteria, true);
return bestProvider;
}
public void setCurrentLocation(Location location){
int currLatitude = (int) (location.getLatitude()*1E6);
int currLongitude = (int) (location.getLongitude()*1E6);
currentLocation = new GeoPoint(currLatitude,currLongitude);
currentLocation = new Location("");
currentLocation.setLatitude(currentPoint.getLatitudeE6() / 1e6);
currentLocation.setLongitude(currentPoint.getLongitudeE6() / 1e6);
}
而且在oncreate方法中,添加对以上方法的调用代码以下:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mapView = (MapView)findViewById(R.id.mapView);
mapView.setBuiltInZoomControls(true);
mapView.setSatellite(false);
mapView.setStreetView(true);
mapController = mapView.getController();
mapController.setZoom(13);
getLastLocation();
animateToCurrentLocation();
}
首先,在getLastLocation这个方法中,建立了locationManager类的实例而且根据设置的条件返回一个合适的地理位置提供方法。在这里,咱们并无指定对地理位置提供者的查询条件,在实际应用中,开发者能够经过设置criteria.setAccuracy()和criteria.setPowerRequirement()方法进行查找。若是要使用最精确的provider的话,能够设置使用ACCURACY_FINE方法进行搜索,以下代码所示:
criteria.setAccuracy(Criteria.ACCURACY_FINE);
在本文中之因此不选择使用最准确的位置provider主要是由于是没在室外进行测试,都是在室内调试程序,建议开发完后换成GPS模式provider到室外进行调试那样将看到很好的效果。
接下来代码中使用 currentLocation = locationManager.getLastKnownLocation(provider); 一句,得到最新的位置信息,而且在setCurrentLocation方法中,对经过prodiver得到的地理位置信息Location对象进行经纬度的转换为GeoPoint对象。而后调用animateToCurrentLocation方法,将地图的中心点定位到咱们当前的位置上去。
此外,因为咱们的位置是会发生变化的,因此须要使用location 监听器去检测咱们的位置变化,这时须要实现locationListener接口,以下所示:
public class MallFinderActivity extends MapActivity implements LocationListener{
同时咱们须要实现LocationListener类的如下几个方法:
@Override
public void onLocationChanged(Location arg0) {
// TODO Auto-generated method stub
}
@Override
public void onProviderDisabled(String arg0) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String arg0) {
// TODO Auto-generated method stub
}
@Override
public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
// TODO Auto-generated method stub
}
其中这里咱们关心的是须要实现onLocationChanged方法,这里咱们调用以前编写好的setlocation方法,得到当前的最新位置,以下:
@Override
public void onLocationChanged(Location newLocation) {
// TODO Auto-generated method stub
setCurrentLocation(newLocation);
}
为了完善程序,在程序处在onResume及onPause状态下都能及时更新地理位置信息以及取消更新,加上以下代码:
@Override
protected void onResume() {
super.onResume();
locationManager.requestLocationUpdates(getBestProvider(), 1000, 1, this);
}
@Override
protected void onPause() {
super.onPause();
locationManager.removeUpdates(this);
}
小结
在本系列的第一讲中,分步讲解了如何注册Google API KEY以及Mapview控件基本方法的使用,还有让读者了解到使用google map的初步步骤,在下一讲中,将指导读者如何对地图上的位置进行标注。
本系列教程将分为两部分,第一部分是指导用户使用Mapview控件进行编程,其中包括了如何得到Google Map API,如何使用该API进行简单的开发,如何得到用户当前所在的位置。第二部分则包括如何在地图上,用第三方的组件库,实现气球式显示若干指定位置的功能。
步骤1 建立新的Android 工程
首先打开eclipse新创建一个Android 工程,其中相关参数设置以下:
Project name:MallFinder
Build Target: Google APIs Platform – 2.1 API Level 7
Application Name: Mall Finder
Package Name: com.shawnbe.mallfinder
Create Activity: MallFinderActivity
MinimumSDK: 7
以下图所示:
步骤2 注册Google Map API key
因为在使用google map的时候,须要使用google map api的key,所以须要先注册一个开发者key,能够到以下地址进行注册:http://code.google.com/android/add-ons/google-apis/mapkey.html ,其中须要咱们先产生开发期间的md5 密纹才能完成注册,所以咱们先学习如何生成一个MD5密纹。
咱们须要使用keytool工具,不使用传统的命令行方式下那枯燥的证书签名生成办法,而是直接在eclipse下经过插件进行完成,详细见步骤3
步骤3 安装keytool插件
在eclipse的Help菜单中,以下图,选择安装新软件:
在安装地址中输入以下地址:http://www.keytool.sourceforge.net/update ,以下图
接下来会加载相关的安装程序,并显示用户协议,选择接受全部用户协议后进行安装,安装成功后从新启动eclipse便可生效。
步骤4 产生开发期间的MD5密钥
在从新启动eclipse后,会发现工具栏多了以下图的keytool菜单
如今,咱们打开debug.keystore,注意其位置回因操做系统不一样而有不一样
Windows Vista : C:\Users\\.android\debug.keystore
Windows XP : C:\Documents and Settings\\.android\debug.keystore
OS X 和 Linux : ~/.android/debug.keystore
点keytool菜单中,选择open keystore,根据提示,选择当前系统所在的debug.keystore位置并打开,以下图,
其中,输入密码默认为android便可,并点Load加载。以后会在eclipse中出现新的keytool的视图,以下图所示:
双击打开androiddebugkey,复制其md5密纹,而后访问页面
http://code.google.com/android/maps-api-signup.html ,接受其协议,将md5密纹复制进去,再点Generate API Key,即产生google map的api key,记得保存好这个KEY,在接下来的步骤中要使用。
步骤5 增长MapView控件
接下来,咱们能够往布局文件中增长MapView控件。咱们在main.xml中添加以下代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<com.google.android.maps.MapView
android:id="@+id/mapView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:clickable="true"
android:apiKey="你的GOOGLE MAP API KEY "/>
</FrameLayout>
</LinearLayout>
在这个控件中,请注意要在android:apiKey的位置填入刚申请的google map api key。
步骤6 设置相关的权限
因为咱们的应用须要调用Google Map的数据,以及经过手机的GPS得到相关的其余地理位置数据,所以咱们必须在Android的Manifest文件中进行权限的设置。
咱们打开AndroidManifest.xml文件,而后增长以下代码所示的权限设置,注意添加在标签后,但要在标签前。
<uses-feature android:name="android.hardware.location.gps"/> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/> <uses-permission android:name="android.permission.INTERNET"/>
在这里,咱们分别调用了gps位置服务权限,使用互联网的权限以及使用最佳位置查找的权限(FINE_LOCATION)。在本文中,其实没用到FINE_LOCATION,但只是告诉开发者能够调整使用不一样的地理位置提供者(providers)以提供准确的位置服务,若是要有比较精确的地理位置服务,那么能够设置android.permisson.ACCESS_FINE_LOCATION服务。要注意的是在开发中,不要过多引用一些不须要使用的权限设置,不然会给用户带来担心安全等问题。要是只想调用通常的位置服务,可使用普通的android.permission.ACCESS_COARSE_LOCATION权限。
为了在应用中使用Google Map,须要在Manifest文件中包含相关的类库文件,因此加入以下代码:
<uses-library android:required="true" android:name="com.google.android.maps" />
为了视觉的美观,咱们把标题栏也去掉,以便留给地图更大的空间,因此设置为
android:theme="@android:style/Theme.NoTitleBar"
下面是完整的Manifest文件代码:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.shawnbe.mallfinder"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="7" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@android:style/Theme.NoTitleBar">
<activity
android:label="@string/app_name"
android:name=".MallFinderActivity" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<uses-library android:required="true" android:name="com.google.android.maps" />
</application>
<uses-feature android:name="android.hardware.location.gps"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.INTERNET"/>
</manifest>
步骤7 设置MapView
下面对主activity程序(MallFinderActivity.java)进行修改,首先让其继承MapActivity类,以下:
public class MallFinderActivity extends MapActivity {
因为继承了MapActivity类,所以必须实现isRouteDisplayed方法,这个方法是用来作路线导航用的,这里咱们不须要,所以只须要简单返回false便可:
@Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
接下来咱们就能够声明MapController控件,并对其进行相关属性的设置了,首先是声明控件
private MapController mapController;
private MapView mapView;
并在oncreate方法中进行属性设置以下:
mapView = (MapView)findViewById(R.id.mapView);
mapView.setBuiltInZoomControls(true);
mapView.setSatellite(false);
mapView.setStreetView(true);
mapController = mapView.getController();
mapController.setZoom(13);
在上面的代码中,设置了地图显示的方式为街道模式(经过设置mapview的setStreetView属性值为true),而且经过mapView.setBuiltInZoomControls(true); 使用了系统内置的放大缩小功能,并设置了地图的放大缩小倍数为13。
这个时候咱们就能够选择运行应用,看下是否符合咱们的初步预期效果,运行后效果以下图:
步骤8 得到当前所在位置
在LBS应用中,十分重要的工做是要得到当前设备所在的位置,这个可使用locationManager类实现,在得到当前位置的时候是须要花费一些时间的。咱们继续声明两个参数变量以下:
private LocationManager locationManager;
private GeoPoint currentLocation;
分别声明了LocationManager类的实例和GeoPoint类的实例(用于下文中的经纬度的计算)。
再增长以下几个方法:
public void getLastLocation(){
String provider = getBestProvider();
currentLocation = locationManager.getLastKnownLocation(provider);
if(currentLocation != null){
setCurrentLocation(currentLocation);
}
else
{
Toast.makeText(this, "Location not yet acquired", Toast.LENGTH_LONG).show();
}
}
public void animateToCurrentLocation(){
if(currentPoint!=null){
mapController.animateTo(currentPoint);
}
}
public String getBestProvider(){
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
criteria.setPowerRequirement(Criteria.NO_REQUIREMENT);
criteria.setAccuracy(Criteria.NO_REQUIREMENT);
String bestProvider = locationManager.getBestProvider(criteria, true);
return bestProvider;
}
public void setCurrentLocation(Location location){
int currLatitude = (int) (location.getLatitude()*1E6);
int currLongitude = (int) (location.getLongitude()*1E6);
currentLocation = new GeoPoint(currLatitude,currLongitude);
currentLocation = new Location("");
currentLocation.setLatitude(currentPoint.getLatitudeE6() / 1e6);
currentLocation.setLongitude(currentPoint.getLongitudeE6() / 1e6);
}
而且在oncreate方法中,添加对以上方法的调用代码以下:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mapView = (MapView)findViewById(R.id.mapView);
mapView.setBuiltInZoomControls(true);
mapView.setSatellite(false);
mapView.setStreetView(true);
mapController = mapView.getController();
mapController.setZoom(13);
getLastLocation();
animateToCurrentLocation();
}
首先,在getLastLocation这个方法中,建立了locationManager类的实例而且根据设置的条件返回一个合适的地理位置提供方法。在这里,咱们并无指定对地理位置提供者的查询条件,在实际应用中,开发者能够经过设置criteria.setAccuracy()和criteria.setPowerRequirement()方法进行查找。若是要使用最精确的provider的话,能够设置使用ACCURACY_FINE方法进行搜索,以下代码所示:
criteria.setAccuracy(Criteria.ACCURACY_FINE);
在本文中之因此不选择使用最准确的位置provider主要是由于是没在室外进行测试,都是在室内调试程序,建议开发完后换成GPS模式provider到室外进行调试那样将看到很好的效果。
接下来代码中使用 currentLocation = locationManager.getLastKnownLocation(provider); 一句,得到最新的位置信息,而且在setCurrentLocation方法中,对经过prodiver得到的地理位置信息Location对象进行经纬度的转换为GeoPoint对象。而后调用animateToCurrentLocation方法,将地图的中心点定位到咱们当前的位置上去。
此外,因为咱们的位置是会发生变化的,因此须要使用location 监听器去检测咱们的位置变化,这时须要实现locationListener接口,以下所示:
public class MallFinderActivity extends MapActivity implements LocationListener{
同时咱们须要实现LocationListener类的如下几个方法:
@Override
public void onLocationChanged(Location arg0) {
// TODO Auto-generated method stub
}
@Override
public void onProviderDisabled(String arg0) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String arg0) {
// TODO Auto-generated method stub
}
@Override
public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
// TODO Auto-generated method stub
}
其中这里咱们关心的是须要实现onLocationChanged方法,这里咱们调用以前编写好的setlocation方法,得到当前的最新位置,以下:
@Override
public void onLocationChanged(Location newLocation) {
// TODO Auto-generated method stub
setCurrentLocation(newLocation);
}
为了完善程序,在程序处在onResume及onPause状态下都能及时更新地理位置信息以及取消更新,加上以下代码:
@Override
protected void onResume() {
super.onResume();
locationManager.requestLocationUpdates(getBestProvider(), 1000, 1, this);
}
@Override
protected void onPause() {
super.onPause();
locationManager.removeUpdates(this);
}
小结
在本系列的第一讲中,分步讲解了如何注册Google API KEY以及Mapview控件基本方法的使用,还有让读者了解到使用google map的初步步骤,在下一讲中,将指导读者如何对地图上的位置进行标注。