name="android.permission.INTERNET"/>
步骤7设置MapView
下面对主activity程序(MallFinderActivity.java)进行修改,首先让其继承MapActivity类,如下:
publicclassMallFinderActivityextendsMapActivity{
由于继承了MapActivity类,因此必须实现isRouteDisplayed方法,这个方法是用来做路线导航用的,这里我们不需要,因此只需要简单返回false即可:
@Override
protectedbooleanisRouteDisplayed(){
//TODOAuto-generatedmethodstub
returnfalse;
}
接下来我们就可以声明MapController控件,并对其进行相关属性的设置了,首先是声明控件
privateMapControllermapController;
privateMapViewmapView;
并在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类实现,在获得当前位置的时候是需要花费一些时间的。
我们继续声明两个参数变量如下:
privateLocationManagerlocationManager;
privateGeoPointcurrentLocation;
分别声明了LocationManager类的实例和GeoPoint类的实例(用于下文中的经纬度的计算)。
再增加如下几个方法:
publicvoidgetLastLocation(){
Stringprovider=getBestProvider();
currentLocation=locationManager.getLastKnownLocation(provider);
if(currentLocation!
=null){
setCurrentLocation(currentLocation);
}
else
{
Toast.makeText(this,"Locationnotyetacquired",Toast.LENGTH_LONG).show();
}
}
publicvoidanimateToCurrentLocation(){
if(currentPoint!
=null){
mapController.animateTo(currentPoint);
}
}
publicStringgetBestProvider(){
locationManager=(LocationManager)getSystemService(Context.LOCATION_SERVICE);
Criteriacriteria=newCriteria();
criteria.setPowerRequirement(Criteria.NO_REQUIREMENT);
criteria.setAccuracy(Criteria.NO_REQUIREMENT);
StringbestProvider=locationManager.getBestProvider(criteria,true);
returnbestProvider;
}
publicvoidsetCurrentLocation(Locationlocation){
intcurrLatitude=(int)(location.getLatitude()*1E6);
intcurrLongitude=(int)(location.getLongitude()*1E6);
currentLocation=newGeoPoint(currLatitude,currLongitude);
currentLocation=newLocation("");
currentLocation.setLatitude(currentPoint.getLatitudeE6()/1e6);
currentLocation.setLongitude(currentPoint.getLongitudeE6()/1e6);
}
并且在oncreate方法中,添加对以上方法的调用代码如下:
publicvoidonCreate(BundlesavedInstanceState){
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接口,如下所示:
publicclassMallFinderActivityextendsMapActivityimplementsLocationListener{
同时我们需要实现LocationListener类的以下几个方法:
@Override
publicvoidonLocationChanged(Locationarg0){
//TODOAuto-generatedmethodstub
}
@Override
publicvoidonProviderDisabled(Stringarg0){
//TODOAuto-generatedmethodstub
}
@Override
publicvoidonProviderEnabled(Stringarg0){
//TODOAuto-generatedmethodstub
}
@Override
publicvoidonStatusChanged(Stringarg0,intarg1,Bundlearg2){
//TODOAuto-generatedmethodstub
}
其中这里我们关心的是需要实现onLocationChanged方法,这里我们调用之前编写好的setlocation方法,获得当前的最新位置,如下:
@Override
publicvoidonLocationChanged(LocationnewLocation){
//TODOAuto-generatedmethodstub
setCurrentLocation(newLocation);
}
为了完善程序,在程序处在onResume及onPause状态下都能及时更新地理位置信息以及取消更新,加上如下代码:
@Override
protectedvoidonResume(){
super.onResume();
locationManager.requestLocationUpdates(getBestProvider(),1000,1,this);
}
@Override
protectedvoidonPause(){
super.onPause();
locationManager.removeUpdates(this);
}
小结
在本系列的第一讲中,分步讲解了如何注册GoogleAPIKEY以及Mapview控件基本方法的使用,还有让读者了解到使用googlemap的初步步骤,在下一讲中,将指导读者如何对地图上的位置进行标注。