当前位置: 首页 > news >正文

(android 地图实战开发)3 在地图上显示当前位置和自定义银行位置

前言:

 

下面讲述在MapView中显示当前位置,并同时已知的银行ATM机的位置

 

 

 

 

 

效果图:

 

 

 

 

 

1         实现效果说明:

 

实现显示当前位置和银行ATM机的位置,主要分以下两个步骤,

 

1)       根据位置服务找到当前的GPS坐标

 

 

2)       将当前坐标和ATM坐标,用图层的方式显示到地图上

 

 

 

2         根据位置服务找到当前的GPS坐标

 

l  设置应用的权限

 

AndroidManifest.xml文件中设置ACCESS_FINE_LOCATION权限

 

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

 

 

 

  • 获取 定位服务

 

String context=Context.LOCATION_SERVICE;

 

locationManager=(LocationManager)getSystemService(context);

 

 

 

  •   根据定位服务获取位置

 

定位服务接口

 

[1]  LocationManager.getLastKnownLocation(provider)

 

:获取最后一次定位坐标信息

 

 

 

[2] void android.location.LocationManager.requestLocationUpdates(String provider, long minTime, float minDistance, LocationListener listener)

 

:对指定provider进行侦听,条件为 最小时间minTime ,最小距离minDistance

 

 

 

3         根据位置服务找到当前的GPS坐标

 

  • 添加MyLocationOverlay,显示当前位置.

 

List<Overlay> overlays=map_view.getOverlays(); // set location Overlay

 

       MyLocationOverlay mylocationOver=new MyLocationOverlay(this,map_view);

 

        overlays.add(mylocationOver);

 

        mylocationOver.enableCompass(); //显示指南针

 

    mylocationOver.enableMyLocation(); //允许当前位置

 

 

 

  • 自定义层,显示ATM位置

 

集成ItemizedOverlay<OverlayItem>

 

调用方式:

 

atm=new ATMDynamicItemizedOverlay(this.getResources().getDrawable(R.drawable.map_atm));

 

    overlays.add(atm);

 

    atm.addNewItem(new GeoPoint(lat.intValue()+1000,lng.intValue()), "marketText", "snippet");

 

 

 

4         源代码:

 

自定义 ATMDynamicItemizedOverlay

 

View Code
publicclass ATMDynamicItemizedOverlay extends ItemizedOverlay<OverlayItem>{

private ArrayList<OverlayItem> items ;

public ATMDynamicItemizedOverlay(Drawable defaultMarker)
{
super(boundCenterBottom(defaultMarker));
items
=new ArrayList<OverlayItem>();
populate();
}

publicvoid addNewItem(GeoPoint location,String marketText,String snippet)
{
items.add(
new OverlayItem(location,marketText,snippet));
populate();
}

publicvoid removeItem(int index)
{
items.remove(index);
populate();

}

@Override
protected OverlayItem createItem(int index) {
// TODO Auto-generated method stub

return items.get(index);
}

@Override
publicint size() {
// TODO Auto-generated method stub
return items.size();
}

}

 

 

 窗体文件

 

View Code
publicclass LocationMap extends MapActivity {
LocationManager locationManager;
MapView map_view;
ATMDynamicItemizedOverlay atm;
@Override
publicvoid onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.locationmap);

//add bank button onclick event
ImageView imgView_back=(ImageView)findViewById(R.id.map_imgView_back);
imgView_back.setOnClickListener(
new ImageView.OnClickListener()
{
publicvoid onClick(View v){
ViewUtility.NavigateActivate(LocationMap.
this,Main.class );

}
});

//set MapControl
map_view =(MapView)findViewById(R.id.map_view);
map_view.setStreetView(
true);
map_view.setTraffic(
true);
map_view.setBuiltInZoomControls(
false);
map_view.setSatellite(
false);

List
<Overlay> overlays=map_view.getOverlays(); // set location Overlay
MyLocationOverlay mylocationOver=new MyLocationOverlay(this,map_view);
overlays.add(mylocationOver);
mylocationOver.enableCompass();
mylocationOver.enableMyLocation();

//set atm Overlay
atm=new ATMDynamicItemizedOverlay(this.getResources().getDrawable(R.drawable.map_atm));
overlays.add(atm);
String context
=Context.LOCATION_SERVICE;
locationManager
=(LocationManager)getSystemService(context);
String provider
=LocationManager.GPS_PROVIDER;


//OverlayItem a=new OverlayItem(null, provider, provider;
Location location =locationManager.getLastKnownLocation(provider);
if(location!=null)
{
UpdateMapView(location);
}

locationManager.requestLocationUpdates(provider,
10000, 5, locationListener );

}

/* By new location ,update MapWiew's Label */
privatevoid UpdateMapView(Location location)
{
MapController mapcontroller
=map_view.getController();

Double lat
=location.getLatitude()*1E6;
Double lng
=location.getLongitude()*1E6;
GeoPoint point
=new GeoPoint(lat.intValue(),lng.intValue());

mapcontroller.setCenter(point);
mapcontroller.setZoom(
20);
mapcontroller.animateTo(point);

atm.addNewItem(
new GeoPoint(lat.intValue()+1000,lng.intValue()), "marketText", "snippet");
Toast.makeText(
this, "lat:"+ String.valueOf(lat.intValue())+"lng:"+String.valueOf(lng.intValue()), Toast.LENGTH_SHORT).show();
}

@Override
publicboolean onKeyDown(int keyCode, KeyEvent event) {
if(keyCode == KeyEvent.KEYCODE_BACK){
ViewUtility.NavigateActivate(LocationMap.
this, Main.class);
}
returnfalse;
}
@Override
protectedboolean isRouteDisplayed() {
// TODO Auto-generated method stub
returnfalse;
}

//create location listener
private LocationListener locationListener =new LocationListener(){
//location is changed
@Override
publicvoid onLocationChanged(Location location) {
UpdateMapView(location);
Log.d(
"Location", "onLocationChanged");
}

//location is Disable
@Override
publicvoid onProviderDisabled(String provider) {
Log.d(
"Location", "onProviderDisabled");
}

//location is enabled
@Override
publicvoid onProviderEnabled(String provider) {
Log.d(
"Location", "onProviderEnabled");
}

//location's status changes
@Override
publicvoid onStatusChanged(String provider, int status, Bundle extras) {
Log.d(
"Location", "onStatusChanged");
}
};

}

 


 

 

相关文章:

  • Nginx+php(fastcgi)搭建LNMP
  • python random模块
  • 打印Excel文件,直向變成橫向,模向變成直向
  • ZOJ Problem Set - 1002 Fire Net (Python)
  • 计划任务不能启动
  • 传苹果产品总设计师欲离职 地位堪比乔布斯
  • 金山游戏研发改制新进展:计划本周发出正式公告
  • exec-timeout
  • 登陆系统的两个界面以及忘记root密码怎么办
  • linux发行版适合做服务器排名
  • 读取指定目录下文件-- ---LIST+递归。(跳出结构思想。利用数据结构思路+程序写出更简单的逻辑。)...
  • CentOS 6.0 安装配置rails 2.3.11 + redmine 1.2.1 笔记
  • 望江南
  • 输不起慢的代价,赢不了休息的时间
  • P42S25C查看开机次数、时间、坏点的方法
  • Debian下无root权限使用Python访问Oracle
  • HashMap剖析之内部结构
  • in typeof instanceof ===这些运算符有什么作用
  • leetcode386. Lexicographical Numbers
  • PaddlePaddle-GitHub的正确打开姿势
  • PHP 7 修改了什么呢 -- 2
  • SpringCloud(第 039 篇)链接Mysql数据库,通过JpaRepository编写数据库访问
  • UEditor初始化失败(实例已存在,但视图未渲染出来,单页化)
  • windows-nginx-https-本地配置
  • 官方解决所有 npm 全局安装权限问题
  • 前端面试总结(at, md)
  • 腾讯优测优分享 | 你是否体验过Android手机插入耳机后仍外放的尴尬?
  • 微信开放平台全网发布【失败】的几点排查方法
  • 基于django的视频点播网站开发-step3-注册登录功能 ...
  • 说说我为什么看好Spring Cloud Alibaba
  • #{}和${}的区别是什么 -- java面试
  • #1014 : Trie树
  • (13)Hive调优——动态分区导致的小文件问题
  • (C)一些题4
  • (八)Spring源码解析:Spring MVC
  • (八)五种元启发算法(DBO、LO、SWO、COA、LSO、KOA、GRO)求解无人机路径规划MATLAB
  • (切换多语言)vantUI+vue-i18n进行国际化配置及新增没有的语言包
  • (三十五)大数据实战——Superset可视化平台搭建
  • (一)基于IDEA的JAVA基础10
  • (原创)Stanford Machine Learning (by Andrew NG) --- (week 9) Anomaly DetectionRecommender Systems...
  • (转)原始图像数据和PDF中的图像数据
  • (转)总结使用Unity 3D优化游戏运行性能的经验
  • ***原理与防范
  • .\OBJ\test1.axf: Error: L6230W: Ignoring --entry command. Cannot find argumen 'Reset_Handler'
  • .locked1、locked勒索病毒解密方法|勒索病毒解决|勒索病毒恢复|数据库修复
  • .NET Core6.0 MVC+layui+SqlSugar 简单增删改查
  • .NET MVC之AOP
  • .NET 事件模型教程(二)
  • .NET/C# 异常处理:写一个空的 try 块代码,而把重要代码写到 finally 中(Constrained Execution Regions)
  • .NET开源项目介绍及资源推荐:数据持久层
  • .pyc文件还原.py文件_Python什么情况下会生成pyc文件?
  • @param注解什么意思_9000字,通俗易懂的讲解下Java注解
  • @Transactional类内部访问失效原因详解
  • [ vulhub漏洞复现篇 ] JBOSS AS 5.x/6.x反序列化远程代码执行漏洞CVE-2017-12149
  • []串口通信 零星笔记