(android 地图实战开发)2 创建MapActivity,根据设备当前位置,显示地图

http://www.cnblogs.com/macroxu-1982/archive/2011/09/13/2174657.html

实现效果:

获取手机gps当前的位置,显示位置对于的google地图.

具体的步骤:

1 Step One  创建包含MapView控件的应用界面

<com.google.android.maps.MapView

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:id="@+id/map_view"

android:clickable="true"

android:apiKey="0zoMLU7jtw20HSV-bpo6p81D6rFEvGwMz2BJqiw"

/>

在这里主要有两个属性 clickable是否允许点击,true ,将捕获点击事件,并作出基于位置的响应.

apiKey :注册并允许基于地图开发的key,具体如何获得apikey 看上一篇

开发环境下配置google地图功能步骤

2 Step Two 初始化MapView

MapView map_view =(MapView)findViewById(R.id.map_view);

map_view.setStreetView(true);

map_view.setTraffic(true);

map_view.setBuiltInZoomControls(true);

map_view.setSatellite(false);

主要几个方法说明:

setStreetView (true) 是否显示街道信息

setTraffic(true) 是否显示交通信息

setSatellite(false)是否显示卫星图,如True显示卫星图,false 显示地图

3 Step Three 获取终端GPS位置

l  获取服务位置服务

String context=Context.LOCATION_SERVICE;

locationManager=(LocationManager)getSystemService(context);

String provider=LocationManager.GPS_PROVIDER;

l  获取当前地理位置

1         获取定位服务的最后一次定位信息

location =locationManager.getLastKnownLocation(provider);

2         更新当前的定位信息

locationManager.requestLocationUpdates(provider, 0, 0, locationListener );

4 Step Four 显示定位下的地图信息

MapController mapcontroller=map_view.getController();

GeoPoint point=getCurrentPoint();// 设置地图的当前位置信息

mapcontroller.setCenter(point);

mapcontroller.setZoom(20);

mapcontroller.animateTo(point);

下面是完整的实现代码:

界面代码:

 
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"> <com.google.android.maps.MapView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/map_view"
android:clickable="true"
android:apiKey="0zoMLU7jtw20HSV-bpo6p81D6rFEvGwMz2BJqiw"
/> </LinearLayout>
 

后台代码:

 
publicclass LocationMap extends MapActivity {
LocationManager locationManager;
@Override
publicvoid onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.locationmap); //设置地图control
MapView map_view =(MapView)findViewById(R.id.map_view);
map_view.setStreetView(true);
map_view.setTraffic(true);
map_view.setBuiltInZoomControls(true);
map_view.setSatellite(false);
MapController mapcontroller=map_view.getController(); GeoPoint point=getCurrentPoint();// 设置地图的当前位置信息
mapcontroller.setCenter(point);
mapcontroller.setZoom(20);
mapcontroller.animateTo(point); }
@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;
} private GeoPoint getCurrentPoint()
{
String context=Context.LOCATION_SERVICE;
locationManager=(LocationManager)getSystemService(context);
String provider=LocationManager.GPS_PROVIDER; Location location =locationManager.getLastKnownLocation(provider); if(location ==null){ //没有最后位置,更新gps,获取当前位置
locationManager.requestLocationUpdates(provider, 0, 0, locationListener );
location =locationManager.getLastKnownLocation(provider);
} GeoPoint point=null;
if(location==null)
{
Double lat=37.422006*1E6; //默认值
Double lng=-122.084095*1E6;
point=new GeoPoint(lat.intValue(),lng.intValue());
}
else//当前反馈的GPS位置
{
Double lat=location.getLatitude()*1E6;
Double lng=location.getLongitude()*1E6;
point=new GeoPoint(lat.intValue(),lng.intValue());
}
return point;
}
//创建位置监听器
private LocationListener locationListener =new LocationListener(){
//位置发生改变时调用
@Override
publicvoid onLocationChanged(Location location) {
Log.d("Location", "onLocationChanged");
} //provider失效时调用
@Override
publicvoid onProviderDisabled(String provider) {
Log.d("Location", "onProviderDisabled");
} //provider启用时调用
@Override
publicvoid onProviderEnabled(String provider) {
Log.d("Location", "onProviderEnabled");
} //状态改变时调用
@Override
publicvoid onStatusChanged(String provider, int status, Bundle extras) {
Log.d("Location", "onStatusChanged");
}
}; }
 
 

(android 地图实战开发)2 创建MapActivity,根据设备当前位置,显示地图的更多相关文章

  1. android wear开发之:创建可穿戴设备应用 - Creating Wearable Apps

    注:本文内容来自:https://developer.android.com/training/wearables/apps/index.html 翻译水平有限,如有疏漏,欢迎批评指教. 译:山人 创 ...

  2. Javascript实战开发:教你使用raphael.js绘制中国地图

    最近的数据统计项目中要用到中国地图,也就是在地图上动态的显示某个时间段某个省份地区的统计数据,我们不需要flash,仅仅依靠raphael.js以及SVG图像就可以完成地图的交互操作.在本文中,我给大 ...

  3. 千锋很火的SpringBoot实战开发教程视频

    springboot是什么? Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程.该框架使用了特定的方式来进行配置,从而使开发人员 ...

  4. Swift - 使用MapKit显示地图,并在地图上做标记

    通过使用MapKit可以将地图嵌入到视图中,MapKit框架除了可以显示地图,还支持在地图上做标记. 1,通过mapType属性,可以设置地图的显示类型 MKMapType.Standard :标准地 ...

  5. Android 利用代码在屏幕中间位置显示ProgressDialog和ProgressBar

    package cc.testprogressdialog; import android.os.Bundle; import android.view.Gravity; import android ...

  6. React Native Android原生模块开发实战|教程|心得|怎样创建React Native Android原生模块

    尊重版权,未经授权不得转载 本文出自:贾鹏辉的技术博客(http://blog.csdn.net/fengyuzhengfan/article/details/54691503) 告诉大家一个好消息. ...

  7. 实战使用Axure设计App,使用WebStorm开发(2) – 创建 Ionic 项目

    系列文章 实战使用Axure设计App,使用WebStorm开发(1) – 用Axure描述需求  实战使用Axure设计App,使用WebStorm开发(2) – 创建 Ionic 项目   实战使 ...

  8. 王家林的81门一站式云计算分布式大数据&移动互联网解决方案课程第14门课程:Android软硬整合设计与框架揭秘: HAL&Framework &Native Service &App&HTML5架构设计与实战开发

    掌握Android从底层开发到框架整合技术到上层App开发及HTML5的全部技术: 一次彻底的Android架构.思想和实战技术的洗礼: 彻底掌握Andorid HAL.Android Runtime ...

  9. 【Android实战开发】3G技术和Android发展简介

    随着移动设备的不断普及和发展,相关软件的开发也越来越受到人们的关注,其中要提及的就是Android开发.本系列博客主要为大家介绍Android的开发,可能会有人问:现在互联网上已经有很多的Androi ...

随机推荐

  1. js 苹果手机 keyup 事件不生效的问题

    $(document).on('keyup','input[name="txtInp"]',function(){}) 改 $(document).on('input proper ...

  2. shell统计各省的百强县

    原始数据在最后 baiqiang.txt文件中 shell命令: cat baiqiang.txt | grep -P "^国|^☆" | awk -F" " ...

  3. 【Python】深入浅出学习Python的yield和generator

    背景 之前走马观花接触过Python协程的概念,这两天和一个同事聊到了协程,死活想不起来曾经看过的东西,就记得一个yield,概念不清: 所以想捋一捋相关的东西,此篇作为学习的记录. Generato ...

  4. cglib动态代理(需导入cglib-nodep-2.1_3.jar)

    public interface AnimalInterface { public void cry(); } public class AnimalImpl implements AnimalInt ...

  5. Web应用体系结构

    容器 Servlet没有main()方法,它们受控于另一个Java应用,这个Java应用称为容器(Container).我们最常见的tomcat就是这样一个容器. Web服务器应用(如Apache)得 ...

  6. WPF 回车转Tab实现跳转

    1.重写窗体的KeyDown事件 protected override void OnKeyDown(KeyEventArgs e) { if (e.Key == Key.Enter) { // Mo ...

  7. Java编码方式再学

    一直以来对编码方式对了解不是很深入.建议读下这几篇博文 学点编码知识又不会死:Unicode的流言终结者和编码大揭秘 编码研究笔记 这几篇博文上回答了内心存在的一些问题,这些问题可能也是大家经常遇到的 ...

  8. jqPaginator分页(ajax用法和form表单提交用法)

    一般使用方法 <!DOCTYPE html> <html> <head lang="en"> <meta charset="UT ...

  9. HDU 5651 组合+逆元

    题目链接http://acm.hdu.edu.cn/showproblem.php?pid=5651 题目意思我看了半天没读懂,一直以为是回文子串又没看见substring的单词最后看博客才知道是用给 ...

  10. OLT配置学习

    1.console连接跟一般交换机一样,不赘述 2.修改系统名称 Add Hostname/Device Name: huawei(config)#system sys-info descriptio ...