Android下实现GPS定位服务
1.申请Google API Key,参考前面文章
2.实现GPS的功能需要使用模拟器进行经纬度的模拟设置,请参考前一篇文章进行设置
3.创建一个Build Target为Google APIs的项目
4.修改Androidmanifest文件:
- <uses-library android:name="com.google.android.maps" />
- <uses-permission android:name="android.permission.INTERNET"/>
- <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
- <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
5.修改main.xml文件
- <com.google.android.maps.MapView
- android:id="@+id/MapView01"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:apiKey="0f8FBFJliR7j_7aNwDxClBv6VW8O12V2Y21W_CQ"/>
注意:这里的apiKey值请相应修改为自己的key值
6.代码清单:
- package com.hoo.android.LocationMap;
- import java.io.IOException;
- import java.util.List;
- import java.util.Locale;
- import android.content.Context;
- import android.graphics.Bitmap;
- import android.graphics.BitmapFactory;
- import android.graphics.Canvas;
- import android.graphics.Paint;
- import android.graphics.Point;
- import android.location.Address;
- import android.location.Criteria;
- import android.location.Geocoder;
- import android.location.Location;
- import android.location.LocationListener;
- import android.location.LocationManager;
- import android.os.Bundle;
- import android.widget.TextView;
- import com.google.android.maps.GeoPoint;
- import com.google.android.maps.MapActivity;
- import com.google.android.maps.MapController;
- import com.google.android.maps.MapView;
- import com.google.android.maps.Overlay;
- public class ActivityLocationMap extends MapActivity
- {
- public MapController mapController;
- public MyLocationOverlay myPosition;
- public MapView myMapView;
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- //取得LocationManager实例
- LocationManager locationManager;
- String context=Context.LOCATION_SERVICE;
- locationManager=(LocationManager)getSystemService(context);
- myMapView=(MapView)findViewById(R.id.MapView01);
- //取得MapController实例,控制地图
- mapController=myMapView.getController();
- //设置显示模式为街景模式
- myMapView.setStreetView(true);
- //*************使用系统自带的控件放大缩小视图***************************
- //取得MapController对象(控制MapView)
- mapController = myMapView.getController();
- //设置地图支持设置模式
- myMapView.setEnabled(true);
- //设置地图支持点击
- myMapView.setClickable(true);
- //设置缩放控制,这里我们自己实现缩放菜单
- myMapView.displayZoomControls(true);
- myMapView.setBuiltInZoomControls(true);
- //*******************************************************************
- ////设置设置地图目前缩放大小倍数(从1到21)
- mapController.setZoom(17);
- //设置使用MyLocationOverlay来绘图
- myPosition=new MyLocationOverlay();
- List<Overlay> overlays=myMapView.getOverlays();
- overlays.add(myPosition);
- //设置Criteria(标准服务商)的信息
- Criteria criteria =new Criteria();
- //*****设置服务商提供的精度要求,以供筛选提供商************************
- criteria.setAccuracy(Criteria.POWER_HIGH);//表明所要求的经纬度的精度
- criteria.setAltitudeRequired(false); //高度信息是否需要提供
- criteria.setBearingRequired(false); //压力(气压?)信息是否需要提供
- criteria.setCostAllowed(false); //是否会产生费用
- criteria.setPowerRequirement(Criteria.POWER_MEDIUM);//最大需求标准
- //*****************************************************
- //取得效果最好的criteria
- String provider=locationManager.getBestProvider(criteria, true);
- //得到坐标相关的信息
- Location location=locationManager.getLastKnownLocation(provider);
- //更新位置信息
- updateWithNewLocation(location);
- //注册一个周期性的更新,3000ms更新一次,0代表最短距离
- //locationListener用来监听定位信息的改变(OnLocationChanged)
- locationManager.requestLocationUpdates(provider, 3000, 0,locationListener);
- }
- //更新位置信息
- private void updateWithNewLocation(Location location)
- {
- String latLongString; //声明经纬度的字符串
- TextView myLocationText = (TextView)findViewById(R.id.TextView01);
- //初始化地址为没有找到,便于处理特殊情况
- String addressString="没有找到地址/n";
- if(location!=null)
- {
- //****************获取当前的经纬度,并定位到目标*************************
- //为绘制标志的类设置坐标
- myPosition.setLocation(location);
- //取得经度和纬度
- Double geoLat=location.getLatitude()*1E6;
- Double geoLng=location.getLongitude()*1E6;
- //将其转换为int型
- GeoPoint point=new GeoPoint(geoLat.intValue(),geoLng.intValue());
- //定位到指定坐标
- mapController.animateTo(point);
- //*********************************************************************
- double lat=location.getLatitude(); //获得经纬度
- double lng=location.getLongitude();
- latLongString="经度:"+lat+"/n纬度:"+lng; //设置经纬度字符串
- // double latitude=location.getLatitude();
- //double longitude=location.getLongitude();
- //根据地理位置来确定编码
- Geocoder gc=new Geocoder(this,Locale.getDefault());
- try
- {
- //取得地址相关的一些信息:经度、纬度
- List<Address> addresses=gc.getFromLocation(lat, lng,1);
- StringBuilder sb=new StringBuilder();
- if(addresses.size()>0)
- {
- Address address=addresses.get(0);
- for(int i=0;i<address.getMaxAddressLineIndex()-1;i++)
- sb.append(address.getAddressLine(i)).append(",");
- //获得地址sb.append(address.getLocality()).append("/n");
- //获得邮编sb.append(address.getPostalCode()).append("/n");
- sb.append(address.getCountryName());
- addressString=sb.toString();
- }
- }catch(IOException e){}
- }
- else
- {
- latLongString="没有找到坐标./n";
- }
- //显示
- myLocationText.setText("您当前的位置如下:/n"+latLongString+"/n"+addressString);
- }
- //监听位置信息的改变
- private final LocationListener locationListener=new LocationListener()
- {
- //当坐标改变时触发此函数
- public void onLocationChanged(Location location)
- {
- updateWithNewLocation(location);
- }
- //Provider被disable时触发此函数,比如GPS被关闭
- public void onProviderDisabled(String provider)
- {
- updateWithNewLocation(null);
- }
- //Provider被enable时触发此函数,比如GPS被打开
- public void onProviderEnabled(String provider){}
- //Provider的转态在可用、暂时不可用和无服务三个状态直接切换时触发此函数
- public void onStatusChanged(String provider,int status,Bundle extras){}
- };
- //方法默认是true,服务器所知的状态列信息是否需要显示
- protected boolean isRouteDisplayed()
- {
- return false;
- }
- class MyLocationOverlay extends Overlay
- {
- Location mLocation;
- //在更新坐标,以便画图
- public void setLocation(Location location)
- {
- mLocation = location;
- }
- @Override
- public boolean draw(Canvas canvas, MapView mapView, boolean shadow, long when)
- {
- super.draw(canvas, mapView, shadow);
- Paint paint = new Paint();
- Point myScreenCoords = new Point();
- // 将经纬度转换成实际屏幕坐标
- GeoPoint tmpGeoPoint = new GeoPoint((int)(mLocation.getLatitude()*1E6),(int)(mLocation.getLongitude()*1E6));
- mapView.getProjection().toPixels(tmpGeoPoint, myScreenCoords);
- //*********paint相关属性设置*********
- paint.setStrokeWidth(0);//文
- paint.setARGB(255, 255, 0, 0);
- paint.setStyle(Paint.Style.STROKE);
- //***********************************
- Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.green_dot);
- canvas.drawBitmap(bmp, myScreenCoords.x, myScreenCoords.y, paint);
- canvas.drawText("您目前的位置", myScreenCoords.x, myScreenCoords.y, paint);
- return true;
- }
- }
- }
代码参考网络,加以修改优化,谢谢
7.程序运行截图,前提是在命令行下输入geo fix 121.5 31.24(定位到上海东方明珠),在命令行下可以输入其他坐标,系统会根据坐标显示其他位置,如接着输入geo fix 113.325 23.113(定位到广州海心沙),不知为什么输入坐标的时候经常会不识别,有时能够成功而有时不行,郁闷,求解……
Android下实现GPS定位服务的更多相关文章
- 在iOS8下使用CLLocationManager定位服务需要系统授权
最近在ios8.0使用CLLocationManager定位服务,发现老不能定位,查看设置菜单中的项也是处于未知状态.想起之前都有一个弹出框提示用户是否允许定位,这次一直没有出现了.原来ios8.0下 ...
- android模拟器使用gps定位
在模拟器上获取GPS信息时,使用Location loc = LocationManager.getLastKnownLocation("gps");来获取location信息,但 ...
- Android入门之GPS定位详解
一.LocationManager LocationMangager,位置管理器.要想操作定位相关设备,必须先定义个LocationManager. LocationManger locationMa ...
- [置顶]
xamarin android使用gps定位获取经纬度
看了文章你会得出以下几个结论 1.android定位主要有四种方式GPS,Network(wifi定位.基站定位),AGPS定位 2.绝大部分android国产手机使用network进行定位是没有作用 ...
- [android学习]android_gps定位服务简单实现
前言 gps定位服务的学习是这段时间gps课程的学习内容,之前老师一直在将概念,今天终于是实践课(其实就是给了一个案例,让自己照着敲).不过在照着案列敲了两遍之后,发现老师的案例是在是太老了,并且直接 ...
- 【Android】18.1 利用安卓内置的定位服务实现位置跟踪
分类:C#.Android.VS2015: 创建日期:2016-03-04 一.安卓内置的定位服务简介 通常将各种不同的定位技术称为位置服务或定位服务.这种服务是通过电信运营商的无线电通信网络(如GS ...
- android 获取GPS定位
AndroidManifest.xml <?xml version="1.0" encoding="utf-8"?> <manifest xm ...
- 解决在iOS8环境下,当用户关闭定位服务总开关时,无法将APP定位子选项加入定位权限列表的问题
关键点:- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizati ...
- Android中通过GPS或NetWork获取当前位置的经纬度
今天在Android项目中要实现一个通过GPS或NetWork来获取当前移动终端设备的经纬度功能.要实现该功能要用到Android Framework 中的 LocationManager 类.下面我 ...
随机推荐
- 设计模式 - chain of Responsibility
Chain of Responsibility也就是职责链模式,通过使用链式结构,使对象都有机会处理请求,从而避免请求的发送者与接受者间的耦合关系.将这些对象连成链,并沿着该链传递请求,直到有对象处理 ...
- bzoj3294
感觉自己就是不怎么擅长计数的问题 设f[k,i,j]表示前k种颜色占据了i行j列的方案 g[k,i,j]表示第k种颜色占据了i行j列的方案,注意要减去并没占据满i行j列的情况 然后转移就很好写了 像这 ...
- usaco /the second wave
bzoj4582:简单递推题. #include<cstdio> #include<cstring> #include<iostream> #include< ...
- ASP.NET Cache
ASP.NET为了方便我们访问Cache,在HttpRuntime类中加了一个静态属性Cache,这样,我们就可以在任意地方使用Cache的功能. 而且,ASP.NET还给它增加了二个“快捷方式”:P ...
- 【持续更新】D3 的学习资料
经常有朋友问哪里有关于 D3 的比较好的学习资料,现整理成此文.以后找到更多更好的,会不断更新本文. 我是在2013年开始接触 D3 的,当时就觉得这个工具很好玩.至今,学习资料整理了不少.如果有朋友 ...
- SqlSugar轻量ORM
蓝灯软件数据股份有限公司项目,代码开源. SqlSugar是一款轻量级的MSSQL ORM ,除了具有媲美ADO的性能外还具有和EF相似简单易用的语法. 学习列表 0.功能更新 1.SqlSuga ...
- POJ 2677 Tour
题意:双调欧几里得旅行商问题.算法导论15-1题,从最左边的点严格从左走到右再从右走到左回到起点,所有点都要走且只走一次,求最短路径. 解法:定义dp[i][j]表示从i走到j的双调路径,分为两种情况 ...
- 使用selector修改TextView中字体的颜色
selector想必大家都用过了,但是在修改字体的颜色的时候还是要细心. 我们在TextView中设置字体颜色一般使用 android:textColor="@color/red" ...
- HDU 3642 Get The Treasury 线段树+分层扫描线
http://www.acmerblog.com/hdu-3642-get-the-treasury-6603.html 学习:三维就是把竖坐标离散化分层,每一层进行线段树二维面积并就好了
- 关于Excel中的需求或者是用例导入到QC中遇到的问题
Excel 中导入用例到QC时,会提示如图所示的错误信息: 解决方案: 我的电脑-->属性->高级-->性能设置-->添加QC程序