实例介绍
【实例截图】
【核心代码】
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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 | package com.yarin.android.MobileMap; import java.io.IOException; import java.util.List; import java.util.Locale; import android.app.AlertDialog; import android.app.AlertDialog.Builder; import android.content.Context; import android.content.DialogInterface; import android.content.DialogInterface.OnClickListener; import android.location.Address; import android.location.Criteria; import android.location.Geocoder; import android.location.Location; import android.location.LocationListener; import android.location.LocationManager; import android.os.Bundle; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.ArrayAdapter; import android.widget.EditText; import android.widget.TextView; import com.google.android.maps.GeoPoint; import com.google.android.maps.MapActivity; import com.google.android.maps.MapController; import com.google.android.maps.MapView; import com.google.android.maps.Overlay; public class MobileMap extends MapActivity { private MapView mMapView; private MapController mMapController; private Geocoder mGeocoder; private LocationOverlay mLocationOverlay; private LocationManager mlocationManager; private Location mLocation; private static final int Search = Menu.FIRST; private static final int SelectCity = Menu.FIRST 1 ; private static final int ViewMode = Menu.FIRST 2 ; private static final int Exit = Menu.FIRST 3 ; public void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.main); init(); } protected boolean isRouteDisplayed() { return false ; } public void init() { mMapView=(MapView)findViewById(R.id.MapView01); //取得MapController实例,控制地图 mMapController=mMapView.getController(); mlocationManager=(LocationManager)getSystemService(Context.LOCATION_SERVICE); mMapView.setEnabled( true ); mMapView.setClickable( true ); //设置显示模式 mMapView.setTraffic( false ); mMapView.setSatellite( false ); mMapView.setStreetView( true ); //设置缩放 mMapView.setBuiltInZoomControls( true ); //设置地图等级 mMapController.setZoom( 12 ); mLocationOverlay= new LocationOverlay( this ); List<Overlay> overlays=mMapView.getOverlays(); overlays.add(mLocationOverlay); Criteria criteria = new Criteria(); //经度要求 criteria.setAccuracy(Criteria.ACCURACY_FINE); criteria.setAltitudeRequired( false ); criteria.setBearingRequired( false ); criteria.setCostAllowed( false ); criteria.setPowerRequirement(Criteria.POWER_LOW); //取得效果最好的criteria String provider=mlocationManager.getBestProvider(criteria, true ); //得到坐标相关的信息 mLocation=mlocationManager.getLastKnownLocation(provider); mGeocoder = new Geocoder( this ,Locale.getDefault()); updateLocation(mLocation); mlocationManager.requestLocationUpdates(provider, 3000 , 0 ,mLocationListener); } //更新定位 public void updateLocation(Location location) { if ( location == null ) { return ; } mLocationOverlay.setLocation(location); Double geoLat=location.getLatitude()*1E6; Double geoLng=location.getLongitude()*1E6; //将其转换为int型 GeoPoint point= new GeoPoint(geoLat.intValue(),geoLng.intValue()); //定位到指定坐标 mMapController.animateTo(point); } public MapController getMapController() { return mMapController; } private final LocationListener mLocationListener= new LocationListener() { public void onLocationChanged(Location location) { updateLocation(location); } public void onProviderDisabled(String provider){} public void onProviderEnabled(String provider){} public void onStatusChanged(String provider, int status,Bundle extras){} }; public boolean onCreateOptionsMenu(Menu menu) { super .onCreateOptionsMenu(menu); menu.add( 0 , Search, Menu.NONE, "搜索地点" ).setIcon(R.drawable.search); menu.add( 0 , SelectCity, Menu.NONE, "选择城市" ).setIcon(R.drawable.selectcity); menu.add( 0 , ViewMode, Menu.NONE, "地图模式" ).setIcon(R.drawable.viewmode); menu.add( 0 , Exit, Menu.NONE, "退出" ).setIcon(R.drawable.exit); return true ; } public boolean onOptionsItemSelected(MenuItem item) { super .onOptionsItemSelected(item); switch (item.getItemId()) { case Search: searchCity(); return true ; case SelectCity: selectCity(); return true ; case ViewMode: selectViewMode(); return true ; case Exit: this .finish(); return true ; } return true ; } //选择城市 public void selectCity() { OnClickListener listener = new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which){ Double geoLat=ConstData.cityCode[which][ 0 ]*1E6; Double geoLng=ConstData.cityCode[which][ 1 ]*1E6; mLocation.setLatitude(ConstData.cityCode[which][ 0 ]); mLocation.setLongitude(ConstData.cityCode[which][ 1 ]); mLocationOverlay.setLocation(mLocation); GeoPoint point= new GeoPoint(geoLat.intValue(),geoLng.intValue()); //定位到指定坐标 mMapController.animateTo(point); } }; ArrayAdapter<String> adapter = new ArrayAdapter<String>( this , android.R.layout.simple_spinner_item, ConstData.city); adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); new AlertDialog.Builder(MobileMap. this ) .setTitle( "请选择城市" ) .setAdapter(adapter, listener) .show(); } //选择地图模式 public void selectViewMode() { OnClickListener listener = new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { switch ( which ) { case 0 : mMapView.setTraffic( false ); mMapView.setSatellite( false ); mMapView.setStreetView( true ); break ; case 1 : mMapView.setSatellite( false ); mMapView.setStreetView( false ); mMapView.setTraffic( true ); break ; case 2 : mMapView.setStreetView( false ); mMapView.setTraffic( false ); mMapView.setSatellite( true ); break ; } } }; String[] menu={ "街景模式" , "交通流量" , "卫星地图" }; new AlertDialog.Builder(MobileMap. this ) .setTitle( "请选择地图模式" ) .setItems(menu, listener) .show(); } //搜索城市 public void searchCity() { //自定义一个带输入的对话框由TextView和EditText构成 final LayoutInflater factory = LayoutInflater.from(MobileMap. this ); final View dialogview = factory.inflate(R.layout.dialog, null ); //设置TextView的提示信息 ((TextView) dialogview.findViewById(R.id.TextView01)).setText( "搜索地图" ); //设置EditText输入框初始值 ((EditText) dialogview.findViewById(R.id.EditText01)).setText( "请输入要查找的地址..." ); Builder builder = new Builder(MobileMap. this ); builder.setTitle( "请输入地名" ); builder.setView(dialogview); builder.setPositiveButton(android.R.string.ok, new AlertDialog.OnClickListener() { public void onClick(DialogInterface dialog, int which) { //点击确定之后 String value = ((EditText) dialogview.findViewById(R.id.EditText01)).getText().toString().trim(); if ( value != "" ) { searchName(value); } } }); builder.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { dialog.cancel(); } }); builder.setOnCancelListener( new DialogInterface.OnCancelListener() { public void onCancel(DialogInterface dialog) { dialog.cancel(); } }); builder.show(); } public void searchName(String nameStr) { try { List<Address> addresses = mGeocoder.getFromLocationName(nameStr, 1 ); if (addresses.size() != 0 ) { Address address = addresses.get( 0 ); GeoPoint geoPoint = new GeoPoint(( int ) (address.getLatitude() * 1E6), ( int ) (address.getLongitude() * 1E6)); mLocation.setLatitude(address.getLatitude()); mLocation.setLongitude(address.getLongitude()); mLocationOverlay.setLocation(mLocation); mMapController.animateTo(geoPoint); } } catch (IOException e){} } } |
标签: 地图
网友评论
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明
支持(0) 盖楼(回复)