当前位置:首页 » 安卓系统 » android定位代码

android定位代码

发布时间: 2022-08-17 19:29:49

A. 如何在android studio中快速定位类的函数

Android Studio 常用快捷键分类整理:1. 查找:Ctrl+R 替换文本Ctrl+F 查找文本Ctrl+N 查找类Ctrl+Shift+N 查找文件Ctrl+Shift+Alt+N 查找类中的方法或变量Alt+F3 ,逐个往下查找相同文本,并高亮显示。2. 移动:Ctrl+Alt+ left/right 返回至上次浏览的位置Alt+ left/right 切换代码视图Alt+ Up/Down 在方法间快速移动定位Ctrl+Shift+Up/Down 代码向上/下移动。F2 或Shift+F2 高亮错误或警告快速定位3. 补全:Alt+Insert 生成代码(如get,set方法,构造函数等)Ctrl+/ 或 Ctrl+Shift+/ 注释(// 或者 )Ctrl+J 自动代码4. 提示:Ctrl+空格 代码提示Ctrl+Alt+Space 类名或接口名提示Ctrl+P 方法参数提示Ctrl+H 显示类结构图

B. 求android GPRS定位源代码

find location service

C. 实现Android的不同精度的定位(基于网络和GP

Android中的定位服务的相关类基本上都在android.location包中,下面会按编写的顺序依次讲解。
1.获取位置服务管理器LocationManager

//变量定义
private LocationManager locationManager;
//onCreate()方法中得到LocationManager
locationManager = (LocationManager) this
.getSystemService(Context.LOCATION_SERVICE)

2.开启位置服务的监听
有了LocationManager之后,我们就可以开始监听位置的变化了。我们使用LocationManager中的方法:
requestLocationUpdates(String
provider, long minTime, float minDistance, LocationListener
listener)
来设置监听器。
首先,我们要注意到第1个参数,这个参数的值为2选1,分别是:LocationManager.NETWORK_PROVIDER和LocationManager.GPS_PROVIDER,前者用于移动网络中获取位置,精度较低但速度很快,后者使用GPS进行定位,精度很高但一般需要10-60秒时间才能开始第1次定位,如果是在室内则基本上无法定位。
这2种Provider本质上是互补的,在本教程中,我们会同时开启2个监听,但基于移动网络的监听只会执行一次就会被停止,而基于GPS的监听则会一直持续下去,直至用户自己停止监听。
代码片段如下:
首先,我们会声明1个监听器的内部类,这个类会同时用于2种模式的监听。还要声明1个变量,用于记录当前的位置

private class MyLocationListener implements LocationListener {

Location currentLocation;

@Override
public void onLocationChanged(Location location) {
if (currentLocation != null) {
if (isBetterLocation(location, currentLocation)) {
currentLocation = location;
showLocation(location);
} else {
Toast.makeText(getApplicationContext(), "not very good", Toast.LENGTH_SHORT).show();
}
} else {
currentLocation = location;
showLocation(location);
}
//移除基于LocationManager.NETWORK_PROVIDER的监听器
if (LocationManager.NETWORK_PROVIDER.equals(location.getProvider())) {
if (ActivityCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
locationManager.removeUpdates(this);
}
}

@Override
public void onStatusChanged(String s, int i, Bundle bundle) {

}

@Override
public void onProviderEnabled(String s) {

}

@Override
public void onProviderDisabled(String s) {

}

private void showLocation(Location location) {
Toast.makeText(getApplication(), "经度:" + location.getLongitude() + ",纬度:" + location.getLatitude() + ",精确度:" + location.getAccuracy(), Toast.LENGTH_SHORT).show();
}
}

3.添加开始监听的代码
首先声明两个全局变量,并定义监听方法registerLocationListener()

private LocationListener gpsListener = null;
private MyLocationListener networkListener = null;

private void registerLocationListener() {

if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
networkListener = new MyLocationListener();
gpsListener = new MyLocationListener();
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 3000, 0, networkListener);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5000, 0, gpsListener);
}

以上的代码还是很易懂的吧,创建1个监听器对象,然后指定1个provider,然后requestLocationUpdates。在监听器中检查如果是NETWORK_PROVIDER,则取消监听,只留GPS的监听(在你的实际应用中可以根据情况来进行,因为GPS有可能会因为建筑的阻挡而暂时不工作)。
位置精度的判断在showLocation方法中,我们打印了location的Accuracy属性,这就是精确度,一般来说NETWORK得到的位置精度一般在500-1000米,GPS得到的精度一般在5-50米,基于这个属性我们可以对精度进行判断,以决定是否采用这个精度。

4.增加判断获取的位置是否更好的方法isBetterLocation()
你可能已经注意到上面的代码中有1个isBetterLocation方法,这是用来判断获取的位置是否更好,事实上这个方法来自于Dev
Guide。我们看下这个方法的内容:

protected boolean isBetterLocation(Location location,
Location currentBestLocation) {
if (currentBestLocation == null) {
// A new location is always better than no location
return true;
}

// Check whether the new location fix is newer or older
long timeDelta = location.getTime() - currentBestLocation.getTime();
boolean isSignificantlyNewer = timeDelta > CHECK_INTERVAL;
boolean isSignificantlyOlder = timeDelta < -CHECK_INTERVAL;
boolean isNewer = timeDelta > 0;

// If it's been more than two minutes since the current location,
// use the new location
// because the user has likely moved
if (isSignificantlyNewer) {
return true;
// If the new location is more than two minutes older, it must
// be worse
} else if (isSignificantlyOlder) {
return false;
}

// Check whether the new location fix is more or less accurate
int accuracyDelta = (int) (location.getAccuracy() - currentBestLocation
.getAccuracy());
boolean isLessAccurate = accuracyDelta > 0;
boolean isMoreAccurate = accuracyDelta < 0;
boolean isSignificantlyLessAccurate = accuracyDelta > 200;

// Check if the old and new location are from the same provider
boolean isFromSameProvider = isSameProvider(location.getProvider(),
currentBestLocation.getProvider());

// Determine location quality using a combination of timeliness and
// accuracy
if (isMoreAccurate) {
return true;
} else if (isNewer && !isLessAccurate) {
return true;
} else if (isNewer && !isSignificantlyLessAccurate
&& isFromSameProvider) {
return true;
}
return false;
}

/** Checks whether two providers are the same */
private boolean isSameProvider(String provider1, String provider2) {
if (provider1 == null) {
return provider2 == null;
}
return provider1.equals(provider2);
}

从代码中可以很清楚的看出判断位置是否“更好”的准则,不仅使用了精度(getAccuracy()),还使用了时间进行判断。事实上除了在导航应用,其它的时候均可以直接使用上面的这个方法来对位置更新信息进行过滤,以减少不断更新界面而带来的性能损失。

5.结束监听
只需要调用LocationManager对象的removeUpdates(LocationListener listener)方法就可以停止监听。事实上,在之前的代码中你已经看到我们移除了基于移动网络的监听器,下面的代码片段用于移除GPS监听器。

if(gpsListener!=null){
locationManager.removeUpdates(gpsListener);
gpsListener=null;
}

LocationManager的其它使用这里还要介绍LocationManager中的几个方法:
getLastKnownLocation(String
provider),用于得到上次定位时的最后位置,通常在应用刚启动时立刻得到1个位置,这样应用看上去会比较快。
getBestProvider(Criteria
criteria, boolean
enabledOnly),根据条件(精度的高低,是否能够得到海拔等)以及当前是否开启,得到1个最好的位置Provider。看上去很美,但现在的Android系统中只有2个Provider,而大多数用户的GPS都是开启的,在仅仅是2选1的情况我想象不出这个方法的用途。而即便用户关闭了GPS,我们也有能力帮他开启,用完了之后再关掉它。开启的方法见《进阶:如何编程实现开启或关闭GPS?》。
总结2个Provider提供了不同精度的定位服务,我们可以根据情况来使用。
一般来说,先使用NETWORK来得到1个精度较差的位置,再使用GPS来得到更准确的位置。
概括起来就是2句话:“快速反应,渐进式精确”。在实际的使用中也要根据自己的情况画1个时间线,好决定何时开始监听,何时结束监听。

D. android GPS定位实现

这是我自己写的GPS定位,可以参考一下,有没看明白的地方可以问

E. 如何利用Android编程实现GPS定位

您好,很高兴为您解答。


一、准备工作
需要如下三种软件:
1. Eclipse
2. Android SDK
3. 开发Android程序的Eclipse 插件

为了开始我们的工作,首先要安装Eclipse,然后从Google的网站获得Android SDK,并且安装Eclipse插件。

二、Activity类
每一种移动开发环境都有自己的基类。如J2ME应用程序的基类是midlets,BREW的基类是applets,而Android程序的基类是 Activity。这个activity为我们提供了对移动操作系统的基本功能和事件的访问。这个类包含了基本的构造方法,键盘处理,挂起来恢复功能,以 及其他底层的手持设备的访问。实质上,我们的应用程序将是一个Activity类的扩展。在本文中读者将会通过例子学习到如何使用Activity类来编 写Android程序。下面是一个简单的继承Activity的例子。

java">{
publicvoidonCreate(Bundleparams){
super.onCreate(params);
setContentView(R.layout.main);
}
publicbooleanonKeyDown(intkeyCode,KeyEventevent){
returntrue;
}
}

三 View类
View类是Android的一个超类,这个类几乎包含了所有的屏幕类型。但它们之间有一些不同。每一个view都有一个用于绘画的画布。这个画布可以用 来进行任意扩展。本文为了方便起见,只涉及到了两个主要的View类型:定义View和Android的XML内容View。在上面的代码中,使用的是 “Hello World” XML View,它是以非常自然的方式开始的。
如果我们查看一下新的Android工程,就会发现一个叫main.xml的文件。在这个文件中,通过一个简单的XML文件,描述了一个屏幕的布局。这个 简单的xml文件的内容如下:

<?xmlversion="1.0"encoding="utf-8"?>
<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"
androidrientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerHoriz
android:text=""
/>
</RelativeLayout>

上面的内容的功能看起来非常明显。这个特殊文件定义了一个相关的布局,这就意味着通过一个元素到另一个元素的关系或是它们父元素的关系来描述。对于视图来 说,有一些用于布局的方法,但是在本文中只关注于上述的xml文件。
RealtiveLayout中包含了一个填充整个屏幕的文本框(也就是我们的LocateMe activity)。这个LocateMe activity在默认情况下是全屏的,因此,文本框将继承这个属性,并且文本框将在屏幕的左上角显示。另外,必须为这个XML文件设置一个引用数,以便 Android可以在源代码中找到它。在默认情况下,这些引用数被保存在R.java中,代码如下:

publicfinalclassR{
publicstaticfinalclasslayout{
publicstaticfinalintmain=0x7f030001;
}
}

视图也可以被嵌套,但和J2ME不同,我们可以将定制的视图和Android团队发布的Widgets一起使用。在J2ME中,开发人员被迫选择 GameCanvas和J2ME应用程序画布。这就意味着如果我们想要一个定制的效果,就必须在GameCanvas上重新设计我们所有的widget。 Android还不仅仅是这些,视图类型也可以混合使用。Android还带了一个 widget库,这个类库包括了滚动条,文本实体,进度条以及其他很多控件。这些标准的widget可以被重载或被按着我们的习惯定制。现在让我们来进入 我们的例子。


四、Android实例

这个演示应用程序将演示了用户的当前的经度和纬度(在文本框中显示)。onCreate构造方法将和上面的例子基本相同,除了在其中加入了键盘处理,现在 让我们看一下onKeyDown的代码。

publicbooleanonKeyDown(intkeyCode,KeyEventevent){
if(keyCode!=KeyEvent.KEYCODE_DPAD_CENTER||m_bLoading)
{
returntrue;
}
m_bLoading=true;
getLocation();
returntrue;
}

下面让我们来解释一下这段代码,首先,这段代码检查了当前被按下的键,但还没有开始处理。而是在getLocation方法中处理这一切的。然后,将装载 flag标志以及调用getLocation方法,下面是getLocation方法的代码。

privatevoidgetLocation(){
Locationloc;
LocationManagerlocMan;
LocationProviderlocPro;
List<LocationProvider>proList;
setContentView(R.layout.laoding);
locMan=(LocationManager)getSystemService(LOCATION_SERVICE);
proList=locMan.getProviders();
locPro=proList.get(0);
loc=locMan.getCurrentLocation(locPro.getName());
Lat=(float)loc.getLatitude();
Lon=(float)loc.getLongitude();
CreateView();
setContentView(customView);
}

到这为止,程序开始变得更有趣了。但是不幸的是,Google关于之方面的文档还是比较少了。在程序的变量声明之后,我们需要演示一些装载信息。 R.layout.loading符合了另一个简单的XML布局视图。通过简单地调用setContentView方法可以使用转载信息重绘屏幕。
读者要注意的是:在编译时,Android会预先将所有的XML布局数据包装起来。如果我们想在编译后变化布局属性,按着规定,我们必须在源程序中做这些 事。
获得LocationManager的唯一方法是通过getSystemService()方法的调用。通过使用LocationManager, 我们可以获得一个位置提供者的列表。在一个真实的手持设备中,这个列表包含了一些GPS服务。实际上,我们希望选择更强大,更精确,最后不带有其他附加服 务的GPS。现在,在模拟器中提供了一个用于测试的GPS,这个GPS来自San Francisco。定制的GPS文件可以可以被上传,并进行测试。如果我们要测试更复杂的应用,来自San Francisco的GPS可能并不适合。
目前我们可以使用位置管理器和位置提供者进行getCurrentLocation的调用。这个方法返回本机的当前位置的一个快照,这个快照将以 Location对象形式提供。在手持设备中,我们可以获得当前位置的经度和纬度。现在,使用这个虚拟的手持设备,我们可以获得这个例子程序的最终结果: 建立了显示一个定制的视图。

五、使用定制视图
在最简单的窗体中,一个Android中的视图仅仅需要重载一个onDraw方法。定制视图可以是复杂的3D实现或是非常简单的文本形式。下面的 CreateView方法列出了上面看到的内容。

publicvoidCreateView(){
customView=newCustomView(this);
}

这个方法简单地调用了CustomView对象的构造方法。CustomView类的定义如下:

{
LocateMeoverlord;
publicCustomView(LocateMepCtx){
super(pCtx);
overlord=pCtx;
}
publicvoidonDraw(Canvascvs){
Paintp=newPaint();
StringsLat="Latitude:"+overlord.getLat();
StringsLon="Longitude:"+overlord.getLon();
cvs.drawText(sLat,32,32,p);
cvs.drawText(sLon,32,44,p);
}
}

这个定制的Android视图获得了经度和违度的测试数据,并将这些数据显示在屏幕上。这要求一个指向LocateMe的指针,Activity类是整 个应用程序的核心。它的两个方法是构造方法和onDraw方法。这个构造方法调用了超类的构造方法以及引起了Activity指针的中断。onDraw方 法将建立一个新的Paint对象(这个对象封装了颜色、透明度以及其他的主题信息),这个对象将会访问颜色主题。在本程序中,安装了用于显示的字符串,并 使用画布指针将它们画到屏幕上。这个和我们了解的J2ME游戏的画布看起来非常类似。

六、Android展望
从纯粹的开发观点看,Android是一个非常强大的SDK。它使用基于XML的布局和定制视图联合了起来。并可以使用滚动条、地图以及其他的组件。所以 的这一切都可以被重载,或由开发人员来定制。但它所提供的文档非常粗糙。在文档中并没有象SMS等技术,但是从整体上来看Android SDK,还是非常有希望的。也非常符合Google承诺的“First Look”SDK。现在我们要做的就是等待Google发布第一个基于Android的手机,并使用它。


如若满意,请点击右侧【采纳答案】,如若还有问题,请点击【追问】


希望我的回答对您有所帮助,望采纳!


~O(∩_∩)O~

F. android虚拟定位怎么写

1、需要在代码中判断你是否已经打开了模拟位置的权限。
2、判断该应用是否设置成了模拟应用。
3、判断设备中是否有GPS模块。
4、打开模拟定位。打开模拟定位就是上面这四个方法。

G. Android中的GPS如何判断是否定位

我们在做手机开发的时候,往往需要获取用户当前的位置,以使用户获得更好的体验。这就需要我们在程序中写出判断用户是否打开GPS定位系统,并对用户做出提示。
判断用户是否打开GPS代码如下:

12345678910111213

public static final boolean isOPen(final Context context) { LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE); // 通过GPS卫星定位,定位级别可以精确到街(通过24颗卫星定位,在室外和空旷的地方定位准确、速度快) boolean gps = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER); // 通过WLAN或移动网络(3G/2G)确定的位置(也称作AGPS,辅助GPS定位。主要用于在室内或遮盖物(建筑群或茂密的深林等)密集的地方定位) boolean network = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER); if (gps || network) { return true; } return false; }

而有些时候用户并不能配合我们打开GPS系统,如果我的应用必须打开GPS(比如说一些租车、送餐类型APP需要获取用户的位置信息),就需要强制用户打开GPS定位。代码如下:

123456789101112

public static final void openGPS(Context context) { Intent GPSIntent = new Intent(); GPSIntent.setClassName("com.android.settings", "com.android.settings.widget.SettingsAppWidgetProvider"); GPSIntent.addCategory("android.intent.category.ALTERNATIVE"); GPSIntent.setData(Uri.parse("custom:3")); try { PendingIntent.getBroadcast(context, 0, GPSIntent, 0).send(); } catch (CanceledException e) { e.printStackTrace(); } }

附录:需要在Mainfast.xml中添加的权限

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.INTERNET" />

H. android开发如何用高德地图进行模拟定位.

一、 要实现高德地图定位呢,首先需要做好以下几步准备:
1. 在高德开放平台注册帐号

2. 在开发中下载Android平台下的地图SDK和定位SDK文件
进入相关下载下载自己想要的功能或文件,图只是截取了地图SDK的页面,定位SDK也是一样,按自己想要的文件下载。下载完成后解压得到:
- 3D地图包解压后得到:3D地图显示包“AMap_3DMap_VX.X.X_时间.jar”和库文件夹(包含armeabi、arm64-v8a等库文件)。
- 2D地图包解压后得到:2D地图显示包“AMap_2DMap_VX.X.X_时间.jar ”
- 定位SDK下载并解压得到定位包“AMap_Location_V2.x.x.jar“
3. 添加jar包,将jar包放入工程的libs目录下。

对于每个jar文件,右键-选择Add As Library,导入到工程中。或者使用菜单栏 选择 File ->Project Structure->Moles-> Dependencies。点击绿色的加号选择File dependency. 然后选择要添加的jar包即可,此时build.gradle中会自动生成如下信息。

创建自己的应用(创建过程内需要的SHA1已经的博客讲过)

开发环境已经配置好了,接下来就是敲代码了。
二、 首先我们要做的就是将地图显示出来,通过以下几步操作,即可在应用中使用高德地图SDK:
第一步:添加用户key 在工程的“ AndroidManifest.xml ”文件如下代码中添加您的用户 Key。
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"

android:theme="@style/AppTheme">
<meta-data
android:name="com.amap.api.v2.apikey"
android:value="" />123456789

第二步:添加所需权限 在工程的“ AndroidManifest.xml ”文件中进行添加。
//地图包、搜索包需要的基础权限

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

//定位包、导航包需要的额外权限(注:基础权限也需要)
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

<uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />
<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />1234567891011121314

第三步:在布局xml文件中添加地图控件。
<com.amap.api.maps2d.MapView
android:id="@+id/map_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />1234

第四步,创建地图Activity,管理地图生命周期。
public class MainActivity extends Activity {
private MapView mMapView = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//获取地图控件引用
mMapView = (MapView) findViewById(R.id.map_view);
//在activity执行onCreate时执行mMapView.o

mMapView.onCreate(savedInstanceState);
}
@Override
protected void onDestroy() {
super.onDestroy();
//在activity执行onDestroy时执行mMapView.onDestroy(),实现地图生命周期管理
mMapView.onDestroy();
}
@Override
protected void onResume() {
super.onResume();
//在activity执行onResume时执行mMapView.onResume (),实现地图生命周期管理
mMapView.onResume();
}
@Override
protected void onPause() {
super.onPause();
//在activity执行onPause时执行mMapView.onPause (),实现地图生命周期管理
mMapView.onPause();
}
@Override

protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
//在activity执行onSaveInstanceState时执行mMapView.onSaveInstanceState (outState),实现地图生命周期管理
mMapView.onSaveInstanceState(outState);
}

}

注意:一定要有mMapView.onCreate(savedInstanceState);
第二步:启动定位功能:
1. 在主线程中获得地图对象AMap,并设置定位监听且实现LocationSource接口:

public class MainActivity extends Activity implements LocationSource{1
if (aMap == null) {
aMap = mMapView.getMap();
//设置显示定位按钮 并且可以点击
UiSettings settings = aMap.getUiSettings();
aMap.setLocationSource(this);//设置了定位的监听,这里要实现LocationSource接口
// 是否显示定位按钮
settings.setMyLocationButtonEnabled(true);
aMap.setMyLocationEnabled(true);//显示定位层并且可以触发定位,默认是flase
}123456789
2. 配置定位参数,启动定位

//初始化定位

mLocationClient = new AMapLocationClient(getApplicationContext());
//设置定位回调监听,这里要实现AMapLocationListener接口,AMapLocationListener接口只有onLocationChanged方法可以实现,用于接收异步返回的定位结果,参数是AMapLocation类型。
mLocationClient.setLocationListener(this);
//初始化定位参数
mLocationOption = new AMapLocationClientOption();
//设置定位模式为Hight_Accuracy高精度模式,Battery_Saving为低功耗模式,Device_Sensors是仅设备模式
mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
//设置是否返回地址信息(默认返回地址信息)
mLocationOption.setNeedAddress(true);
//设置是否只定位一次,默认为false
mLocationOption.setOnceLocation(false);
//设置是否强制刷新WIFI,默认为强制刷新
mLocationOption.setWifiActiveScan(true);
//设置是否允许模拟位置,默认为false,不允许模拟位置
mLocationOption.setMockEnable(false);
//设置定位间隔,单位毫秒,默认为2000ms
mLocationOption.setInterval(2000);
//给定位客户端对象设置定位参数
mLocationClient.setLocationOption(mLocationOption);
//启动定位
mLocationClient.startLocation();

高精度定位模式:
在这种定位模式下,将同时使用高德网络定位和GPS定位,优先返回精度高的定位

低功耗定位模式:
在这种模式下,将只使用高德网络定位

仅设备定位模式:
在这种模式下,将只使用GPS定位。

3. 实现AMapLocationListener接口,获取定位结果:

public class MainActivity extends Activity implem

@Override
public void onLocationChanged(AMapLocation aMapLocation) {
if (aMapLocation != null) {
if (aMapLocation.getErrorCode() == 0) {
//定位成功回调信息,设置相关消息
aMapLocation.getLocationType();//获取当前定位结果来源,如网络定位结果,详见官方定位类型表
aMapLocation.getLatitude();//获取纬度
aMapLocation.getLongitude();//获取经度
aMapLocation.getAccuracy();//获取精度信息
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = new Date(aMapLocation.getTime());
df.format(date);//定位时间
aMapLocation.getAddress();//地址,如果option中设置isNeedAddress为false,则没有此结果,网络定位结果中会有地址信息,GPS定位不返回地址信息。
aMapLocation.getCountry();//国家信息
aMapLocation.getProvince();//省信息
aMapLocation.getCity();//城市信息
aMapLocation.getDistrict();//城区信息
aMapLocation.getStreet();//街道信息
aMapLocation.getStreetNum();//街道门牌号信息
aMapLocation.getCityCode();//城市编码
aMapLocation.getAdCode();//地区编码

// 如果不设置标志位,此时再拖动地图时,它会不断将地图移动到当前的位置
if (isFirstLoc) {
//设置缩放级别
aMap.moveCamera(CameraUpdateFactory.zoomTo(17));
//将地图移动到定位点
aMap.moveCamera(CameraUpdateFactory.changeLatLng(new LatLng(aMapLocation.getLatitude(), aMapLocation.getLongitude())));
//点击定位按钮 能够将地图的中心移动到定位点
mListener.onLocationChanged(aMapLocation);
//获取定位信息
StringBuffer buffer = new StringBuffer();
buffer.append(aMapLocation.getCountry() + ""
+ aMapLocation.getProvince() + ""
+ aMapLocation.getCity() + ""
+ aMapLocation.getProvince()
+ aMapLocation.getDistrict() + ""
+ aMapLocation.getStreet() + ""
+ aMapLocation.getStreetNum());
Toast.makeText(getApplicationContext(), buffer.toString(), Toast.LENGTH_LONG).show();
isFirstLoc = false;
}
} else {
//显示错误信息ErrCode是错误码,errInfo是错误信息,详见错误码表。
Log.e("AmapError", "location Error, ErrCode:"
+ aMapLocation.getErrorCode() + ", errInfo:"
+ aMapLocation.getErrorInfo());
Toast.makeText(getApplicationContext(), "定位失败", Toast.LENGTH_LONG).show();
}
}
}3839404142434445464748495051
4.关于停止定位

@Override
protected void onDestroy() {
super.onDestroy();
mapView.onDestroy();
//mLocationClient.stopLocation();//停止定位
mLocationClient.onDestroy();//销毁定位客户端。
//销毁定位客户端之后,若要重新开启定位请重新New一个AMapLocationClient对象。
}

//激活定位
@Override
public void activate(OnLocationChangedListener onLocationChangedListener) {
mListener = onLocationChangedListener;
}

@Override
public void deactivate() {
mListener = null;
}12345678910111213141516171819

I. android 手机 地图 定位 编程 如何获取 定位的地点 的图片 和 文字 信息

最近开发中使用的是网络地图android SDK:
1、初始化地图
2、注册定位监听器,初始化Location模块
3、初始化定位图层;启用定位,启用指南针,最后把定位图层添加到地图中。附简单代码:

//mMapView = (MapView) findViewById(R.id.bmapsView);
mMapView = new MapView(this); //初始化一个mapView 存放Map
mMapView.setBuiltInZoomControls(true);// 设置启用默认的缩放控件

locationManager = mBMapMan.getLocationManager();
/*
* 由于LocationListener获取第一个位置修正的时间会很长,为了避免用户等待,
* 在LocationListener获取第一个更精确的位置之前,应当使用getLocationInfo() 获取一个缓存的位置
*/
Location location = locationManager.getLocationInfo();
locationManager.requestLocationUpdates(this);

mMapController = mMapView.getController();// 得到mMapView的控制权,可以用它控制和驱动平移和缩放
if (location != null) {
mMapController.setCenter(new GeoPoint(
(int) (location.getLatitude() * 1E6), (int) (location
.getLongitude() * 1E6)));
}
mMapController.setZoom(12);// 设置地图zoom级别

MyLocationOverlay mylocTest = new MyLocationOverlay(this, mMapView);
mylocTest.enableMyLocation();
mylocTest.enableCompass();
mMapView.getOverlays().add(mylocTest);
@Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub

if (location != null) {
mMapController.animateTo(new GeoPoint(
(int) (location.getLatitude() * 1E6), (int) (location
.getLongitude() * 1E6)));
}
}

如果你想点击定位地点,显示信息,你需要去扩展MyLocationOverlay图层,它里面有个public boolean onTap(GeoPoint p, MapView map) {}方法,是处理点击事件的。

有问题,可以与我联系,共同探讨。

热点内容
无锡五洋赛德压缩机有限公司 发布:2024-05-07 03:28:05 浏览:150
安卓手机如何打开qrs 发布:2024-05-07 03:18:31 浏览:465
c语言给数组排序 发布:2024-05-07 03:18:30 浏览:75
职工个人所得税登录密码是多少 发布:2024-05-07 03:14:22 浏览:706
编译程序组成部分 发布:2024-05-07 03:02:07 浏览:96
服务器怎么做双机 发布:2024-05-07 02:37:25 浏览:36
ftp剪切 发布:2024-05-07 02:37:14 浏览:138
手机存储英文 发布:2024-05-07 02:34:31 浏览:640
php锁变量 发布:2024-05-07 02:09:02 浏览:49
股票涨的算法 发布:2024-05-07 01:59:22 浏览:23