(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. 【Network Architecture】Feature Pyramid Networks for Object Detection(FPN)论文解析(转)

    目录 0. 前言 1. 博客一 2.. 博客二 0. 前言   这篇论文提出了一种新的特征融合方式来解决多尺度问题, 感觉挺有创新性的, 如果需要与其他网络进行拼接,还是需要再回到原文看一下细节.这里 ...

  2. [BZOJ3244][NOI2013]树的计数

    这题大家为什么都写O(NlogN)的算法呢?…… 让本蒟蒻来写一个O(N)的吧…… 首先还是对BFS序和DFS序重编号,记标好的DFS序为d[1..n].令pos[x]为x在d[]中出现的位置,即po ...

  3. 在Windows使用VC编译ICU

    1 编译过程在Cygwin下进行,所以必须得安装Cygwin,并且加上Automake, autoconf, make, ar等选项 2 打开命令行窗口,设置环境变量,主要是可以启动cygwin的ba ...

  4. Unity与Web结合

    偶然在论坛上看到了一篇文章,觉的挺有意思,转载一下,之前做游戏,现在做前端,这篇文章不错..转载 Unity WebPlayer 写在前面 最近在做unity与web之间通讯的项目,在网上搜索了一些资 ...

  5. 微信小程序:scroll-view的bug

    flex:1并不能使scroll-view的高度固定,需要添加高度height:1rpx(数值大于0)就行

  6. 百度编辑器(ueditor)@功能之获取坐标

    //获取百度编辑器的工具类 var domUtils = UE.dom.domUtils; //获取编辑器的坐标 var $ueditor_offset = $("#ueditor_0&qu ...

  7. DML,DML,DCL,DQL

    可以先看看这篇微博:http://blog.csdn.net/jiben2qingshan/article/details/7832344 http://blog.163.com/chenwenlin ...

  8. ajax实现用户注册

    需求分析 页面中给出注册表单: 在username input标签中绑定onblur事件处理函数. 当input标签失去焦点后获取 username表单字段的值,向服务端发送AJAX请求: djang ...

  9. git代码版本回退

    git log 用于查看我们修改提交的快照记录(commit记录),然后会有个commit修改记录的id编号,该命令显示从最近到最远的提交日志,我们可以看到3次提交记录,如下: git checkou ...

  10. Java中子类对象赋值给父类变量

    1. 在Java中将一个字类对象赋值给父类变量是合法的,这也是实现多态(即方法的动态分派)的前提条件之一. 2. 在Java中父类赋值同样也适用于数组.可以将一个子类数组赋值给一个父类数组变量.但可能 ...