publicclassActivity01extendsMapActivity
{
publicMapControllermapController;
publicMyLocationOverlaymyPosition;
publicMapViewmyMapView;
privatestaticfinalintZOOM_IN=Menu.FIRST;
privatestaticfinalintZOOM_OUT=Menu.FIRST+1;

publicvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//取得LocationManager实例
LocationManagerlocationManager;
Stringcontext=Context.LOCATION_SERVICE;
locationManager=(LocationManager)getSystemService(context);
myMapView=(MapView)findViewById(R.id.MapView01);
//取得MapController实例,控制地图
mapController=myMapView.getController();
//设置显示模式
myMapView.setSatellite(true);
myMapView.setStreetView(true);
//设置缩放控制,这里我们自己实现缩放菜单
myMapView.displayZoomControls(false);
//设置使用MyLocationOverlay来绘图
mapController.setZoom(17);
myPosition=newMyLocationOverlay();
Listoverlays=myMapView.getOverlays();
overlays.add(myPosition);
//设置Criteria(服务商)的信息
Criteriacriteria=newCriteria();
//经度要求
criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setAltitudeRequired(false);
criteria.setBearingRequired(false);
criteria.setCostAllowed(false);
criteria.setPowerRequirement(Criteria.POWER_LOW);
//取得效果最好的criteria
Stringprovider=locationManager.getBestProvider(criteria,true);
//得到坐标相关的信息
Locationlocation=locationManager.getLastKnownLocation(provider);
//更新坐标
updateWithNewLocation(location);
//注册一个周期性的更新,3000ms更新一次
//locationListener用来监听定位信息的改变
locationManager.requestLocationUpdates(provider,3000,0,locationListener);
}
privatevoidupdateWithNewLocation(Locationlocation)
{
StringlatLongString;
TextViewmyLocationText=(TextView)findViewById(R.id.TextView01);

StringaddressString="没有找到地址\n";

if(location!=null)
{
//为绘制标志的类设置坐标
myPosition.setLocation(location);
//取得经度和纬度
DoublegeoLat=location.getLatitude()*1E6;
DoublegeoLng=location.getLongitude()*1E6;
//将其转换为int型
GeoPointpoint=newGeoPoint(geoLat.intValue(),geoLng.intValue());
//定位到指定坐标
mapController.animateTo(point);
doublelat=location.getLatitude();
doublelng=location.getLongitude();
latLongString="经度:"+lat+"\n纬度:"+lng;

doublelatitude=location.getLatitude();
doublelongitude=location.getLongitude();
//更具地理环境来确定编码
Geocodergc=newGeocoder(this,Locale.getDefault());
try
{
//取得地址相关的一些信息\经度、纬度
Listaddresses=gc.getFromLocation(latitude,longitude,1);
StringBuildersb=newStringBuilder();
if(addresses.size()>0)
{
Addressaddress=addresses.get(0);
for(inti=0;isb.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(IOExceptione){}
}
else
{
latLongString="没有找到坐标.\n";
}
//显示
myLocationText.setText("你当前的坐标如下:\n"+latLongString+"\n"+addressString);
}
privatefinalLocationListenerlocationListener=newLocationListener()
{
//当坐标改变时触发此函数
publicvoidonLocationChanged(Locationlocation)
{
updateWithNewLocation(location);
}
//Provider被disable时触发此函数,比如GPS被关闭
publicvoidonProviderDisabled(Stringprovider)
{
updateWithNewLocation(null);
}
//Provider被enable时触发此函数,比如GPS被打开
publicvoidonProviderEnabled(Stringprovider){}
//Provider的转态在可用、暂时不可用和无服务三个状态直接切换时触发此函数
publicvoidonStatusChanged(Stringprovider,intstatus,Bundleextras){}
};
protectedbooleanisRouteDisplayed()
{
returnfalse;
}
//为应用程序添加菜单
publicbooleanonCreateOptionsMenu(Menumenu)
{
super.onCreateOptionsMenu(menu);
menu.add(0,ZOOM_IN,Menu.NONE,"放大");
menu.add(0,ZOOM_OUT,Menu.NONE,"缩小");
returntrue;
}
publicbooleanonOptionsItemSelected(MenuItemitem)
{
super.onOptionsItemSelected(item);
switch(item.getItemId())
{
case(ZOOM_IN):
//放大
mapController.zoomIn();
returntrue;
case(ZOOM_OUT):
//缩小
mapController.zoomOut();
returntrue;
}
returntrue;
}


classMyLocationOverlayextendsOverlay
{
LocationmLocation;
//在更新坐标时,设置该坐标,一边画图
publicvoidsetLocation(Locationlocation)
{
mLocation=location;
}
@Override
publicbooleandraw(Canvascanvas,MapViewmapView,booleanshadow,longwhen)
{
super.draw(canvas,mapView,shadow);
Paintpaint=newPaint();
PointmyScreenCoords=newPoint();
//将经纬度转换成实际屏幕坐标
GeoPointtmpGeoPoint=newGeoPoint((int)(mLocation.getLatitude()*1E6),(int)(mLocation.getLongitude()*1E6));
mapView.getProjection().toPixels(tmpGeoPoint,myScreenCoords);
paint.setStrokeWidth(1);
paint.setARGB(255,255,0,0);
paint.setStyle(Paint.Style.STROKE);
Bitmapbmp=BitmapFactory.decodeResource(getResources(),R.drawable.home);
canvas.drawBitmap(bmp,myScreenCoords.x,myScreenCoords.y,paint);
canvas.drawText("HereamI",myScreenCoords.x,myScreenCoords.y,paint);
returntrue;
}
}
}

XML中:



更多相关文章

  1. android View onMeasure
  2. 基于Android的邮件收发
  3. 自定义垂直文字view
  4. 《Android(安卓)基础(十)》FloatingActionButton
  5. Android(安卓)MenuItem 设置文字颜色-TextColor的设置
  6. Android中播放视频
  7. android通知栏提示
  8. Android(安卓)解压zip文件
  9. android进度条对话框小例子

随机推荐

  1. android工程下运行main方法的配置方法
  2. Android(安卓)AlertDialog有EditText无法
  3. TensorFlow编Android(安卓)so库总结
  4. android configChanges
  5. Dialog
  6. Android的API版本和名称对应关系
  7. android Gallery(画廊)以及BaseAdapter
  8. Android(安卓)EditText 设置圆角
  9. Android(安卓)EditText 设置圆角
  10. 修改dialog背景以及代码扩展AlertDialog