Android 使用地图

对"Where Am I"示例使用地图

       在下面的例子中,"Where Am I"项目将再次被扩展。这次,通过把它转换为一个Map Activity,将可以对它添加地图功能。随着设备位置的改变,地图将会自动地把它的中心定位到新的位置。

       (1) 首先,向应用程序的清单中加入访问Internet的uses-permission标签。同时还要在application标签中引入Android地图库。

复制代码
<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.paad.whereami"> 
<application 
android:icon="@drawable/icon"> 
<activity 
android:name=".WhereAmI" 
android:label="@string/app_name"> 
<intent-? lter> 
<action 
android:name="android.intent.action.MAIN" /> 
<category 
android:name="android.intent.category.LAUNCHER" /> 
</intent-? lter> 
</activity> 
<uses-library 
android:name="com.google.android.maps"/> 
</application> 
<uses-permission 
android:name="android.permission.INTERNET"/> 
<uses-permission 
android:name="android.permission.ACCESS_FINE_LOCATION"/> 
</manifest> 
复制代码

(2) 改变WhereAmI的继承性,让它继承MapActivity,而不是Activity。还需要包含对isRouteDisplayed方法的重写。因为这个活动不会显示路径的方向,所以你可以返回false。

public class WhereAmI extends MapActivity { 
@Override 
protected boolean isRouteDisplayed() { 
return false; 
} 
[ ... existing Activity code ... ] 
} 

(3) 通过修改main.xml布局资源来包含一个使用完全限定的类名的MapView。一定要保证在com.android.MapView节点中包含一个android:apikey属性。如果有一个Android 地图API key,那么在这里使用它。

复制代码
<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" 
android:layout_width="? ll_parent" 
android:layout_height="? ll_parent"> 
<TextView 
android:id="@+id/myLocationText" 
android:layout_width="? ll_parent" 
android:layout_height="wrap_content" 
android:text="@string/hello" /> 
<com.google.android.maps.MapView 
android:id="@+id/myMapView" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:enabled="true" 
android:clickable="true" 
android:apiKey="myMapKey" /> 
</LinearLayout> 
复制代码

 (4) 现在运行这个应用程序应该显示原始的地理位置文本,它的下面会有一个MapView,如图

(5) 配置Map View,并把对它的MapController的一个引用作为实例变量进行存储。然后设置Map View的显示选项来显示卫星和StreetView,并缩进到比较进的视角。

复制代码
MapController mapController; 

@Override 
public void onCreate(Bundle icicle) { 
super.onCreate(icicle); 
setContentView(R.layout.main); 
// 获得对MapView的引用 
MapView myMapView = (MapView)findViewById(R.id.myMapView); 
// 获得MapView的控制器 
mapController = myMapView.getController(); 
// 配置地图显示选项 
myMapView.setSatellite(true); 
myMapView.setStreetView(true); 
myMapView.displayZoomControls(false);
 
// 放大 
mapController.setZoom(17); 
LocationManager locationManager; 
String context = Context.LOCATION_SERVICE; 
locationManager = (LocationManager)getSystemService(context); 
Criteria criteria = new Criteria(); 
criteria.setAccuracy(Criteria.ACCURACY_FINE); 
criteria.setAltitudeRequired(false); 
criteria.setBearingRequired(false); 
criteria.setCostAllowed(true); 
criteria.setPowerRequirement(Criteria.POWER_LOW); 
String provider = locationManager.getBestProvider(criteria, true); 
Location location = locationManager.getLastKnownLocation(provider); 
updateWithNewLocation(location); 
locationManager.requestLocationUpdates(provider, 2000, 10, locationListener); 

} 
复制代码

 (6) 最后一步是修改updateWithNewLocation方法从而使用Map Controller把地图的中心定位到当前的位置。

复制代码
private void updateWithNewLocation(Location location) { 
String latLongString; 
TextView myLocationText; 
myLocationText = (TextView)? ndViewById(R.id.myLocationText); 
String addressString = "No address found"; 
if (location != null) { 
// 更新地图位置 
Double geoLat = location.getLatitude()*1E6; 
Double geoLng = location.getLongitude()*1E6; 
GeoPoint point = new GeoPoint(geoLat.intValue(), geoLng.intValue()); 
mapController.animateTo(point); 
double lat = location.getLatitude(); 
double lng = location.getLongitude(); 
latLongString = "Lat:" + lat + "\nLong:" + lng; 
double latitude = location.getLatitude(); 
double longitude = location.getLongitude(); 
Geocoder gc = new Geocoder(this, Locale.getDefault()); 
try { 
List<Address> addresses = gc.getFromLocation(latitude, longitude, 1); 
StringBuilder sb = new StringBuilder(); 
if (addresses.size() > 0) { 
Address address = addresses.get(0); 
for (int i = 0; i < address.getMaxAddressLineIndex(); i++) sb.append(address.getAddressLine(i)).append("\n"); 
sb.append(address.getLocality()).append("\n"); 
sb.append(address.getPostalCode()).append("\n"); 
sb.append(address.getCountryName()); 
} 
addressString = sb.toString(); 
} catch (IOException e) {} 
} else { 
latLongString = "No location found"; 
} 
myLocationText.setText("Your Current Position is:\n" + latLongString + "\n" + addressString); 

} 
复制代码

 

 

posted on   vus520  阅读(191)  评论(0编辑  收藏  举报

编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架

导航

< 2011年8月 >
31 1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31 1 2 3
4 5 6 7 8 9 10
点击右上角即可分享
微信分享提示