(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. java 二维码生成

    直接上代码: 二维码生成核心类: package com.bbkj.wechat.tool; import java.awt.image.BufferedImage; import java.io.F ...

  2. Elasticsearch Head 集群健康值:未连接

    安装elasticsearch 6.0  x-pack后,登录9200端口需要用户和密码, 这样,在使用elasticsearch head时,就不能直接访问9100了. 按照官方文档的要求,http ...

  3. Vue——解决[Vue warn]: Invalid prop: custom validator check failed for prop "index". found in错误

    Invalid prop: custom validator check failed for prop "index". 错误重现: 使用element-ui的菜单,在SubMe ...

  4. poj 2229 Sumsets 完全背包求方案总数

    Sumsets Description Farmer John commanded his cows to search for different sets of numbers that sum ...

  5. 字体渲染技术(字体抗锯齿技术) -webkit-font-smoothing: antialiased;

    1.-webkit-font-smoothing控制的字体渲染只对MacOS的webkit有效.所以,你在MacOS测试环境下面设置-webkit-font-smoothing时,只要你不把它设置为n ...

  6. winform无边框窗体点击任务栏最小化

    protected override CreateParams CreateParams { get { const int WS_MINIMIZEBOX = 0x00020000; // Winus ...

  7. Go连接MySql数据库Error 1040: Too many connections错误解决

    原文:https://my.oschina.net/waknow/blog/205654 摘要: 使用Go链接数据库时,由于连接释放不当会在一段时间以后产生too many connections的错 ...

  8. ubuntu16.04 安装以及要做的事情

    1.安装ubuntu 选择安装时更新,以及MP3.图形等:然后选择分区(ext4)(安装时需先进入虚拟系统连上网,输入清华net账号),分区情况按照下图来,swap为临时用的内存分区,可以不要: 选择 ...

  9. 场景设计以及Manual Scenario和Goal-OrientedScenario的区别

    Manual Scenario 手工场景 主要是设计用户变化,通过手工场景可以帮助我们分析系统的性能瓶颈.手动方案:如果要生成手动方案,请选择此方法.通过创建组并指定脚本.负载生成器和每组中包括的 V ...

  10. 为什么MVC不是一种设计模式?

    引用一段话: GoF (Gang of Four,四人组, <Design Patterns: Elements of Reusable Object-Oriented Software> ...