今天说说Location , Location 在Android 开发中还是经常用到的,比如 通过经纬度获取天气,根据Location 获取所在地区详细Address (比如Google Map 开发).等。而在Android 中通过LocationManager 来获取Location .通常获取Location 有GPS 获取,WIFI 获取。
我今天做一个简单的小Demo ,来教大家如何获取Location ,从而获取经纬度。下一节将教大家通过Location 来获取Address .
首先第一步:
创建一个Android 工程命名为LocationDemo .
第二步:修改main.xml 代码如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:id="@+id/longitude"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="longitude:"
/>
<TextView
android:id="@+id/latitude"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="latitude:"
/>
</LinearLayout>
第三步:修改LocationDemo.java ,代码如下:
package com.android.tutor;
import android.app.Activity;
import android.content.Context;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.TextView;
public class LocationDemo extends Activity {
private TextView longitude;
private TextView latitude;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
longitude = (TextView)findViewById(R.id.longitude);
latitude = (TextView)findViewById(R.id.latitude);
Location mLocation = getLocation(this);
longitude.setText("Longitude: " + mLocation.getLongitude());
latitude.setText("Latitude: " + mLocation.getLatitude());
}
//Get the Location by GPS or WIFI
public Location getLocation(Context context) {
LocationManager locMan = (LocationManager) context
.getSystemService(Context.LOCATION_SERVICE);
Location location = locMan
.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location == null) {
location = locMan
.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
}
return location;
}
}
第四步:增加权限,修改AndroidManifest.xml 代码如下(第16行为所增行):
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.android.tutor"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".LocationDemo"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-sdk android:minSdkVersion="7" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
</manifest>
分享到:
相关推荐
一些很不错的Android开发技巧,这个项目翻译自 android-tips-tricks 去掉了一些我认为不重要的,对我使用过的东东做了评价,同时翻译了一些自己没有注意到的知识点的文章。 :heart: star 支持一下 欢迎协作 了解你的...
Android开发—通过Location获取Address,获取手机一些信息
Android高手进阶教程之----Android Location的使用!! .doc Android高手进阶教程之----Android PopupWindow的使用!!! .doc Android高手进阶教程之----Android 数据库SQLiteDatabase的使用!.doc Android高手进阶...
将Location的相关API封装,提供出更加简便的调用方式。
NULL 博文链接:https://913.iteye.com/blog/1489637
android-location-tracker Android Simple Location Tracker is an Android library that helps you get user location with a object named LocationTracker Installation Add this to your build.gradle file ...
Android实用源代码下载里面包含Location 还有二维码扫描功能
Android Fused Location Provider: on how to use fused location API in Android
android location 数据标准输出,原始输出和卫星状态的变化。标准输出就是由系统api已经封装好进行输出经纬度等等。原始数据有很多也可以解出来经纬度,解状态,卫星状态颗数。什么卫星组成,信噪比等等。卫星状态...
使用Android Studio 工具开发的ArcGIS For Android10.2.6 SDK, Location Demo
android 模拟位置信息Location使用示例,。我的博客地址:http://blog.csdn.net/qq_16064871。欢迎关注,源码多多的博客。
Android_Location_V1.3.0.jar: 在分析工程时,发现LocationManagerProxy找不到,后来才知道高德地图的版本问题.
LocationMapViewer An app that can display geografic info in a map. It has support for gpx and ...Minimal requirements: Android 2.3.7 (API 10), internet/wifi-connection to download geodata and a SD-Card t
继续上一篇的内容,讲GPS以外的东西,比如说Service的使用。比如说gps监控,它并不需要任何UI,在后台默默地运行就行。为什么不做成Service呢?悄悄地向服务器发送用户的位置坐标是一个不错的想法,因为它完全不需要...
介绍android本地服务类的使用方法,附源码。
由于高德地图API升级的太快,导致之前能用的几乎都没了,这个LocationManagerProxy类在Android_Location_V1.3.0还能用,好不容找到,这个,现在分享一下这个包
Android 位置地图示例,使用eclipse或android studio打开。
Built for the ARCore Android SDK. Version 1.x is now for SceneForm projects, and will not be compatible with old versions of ARCore. If you are still using the older version of ARCore, change branch ...
Android GPS 和 Location Manager
location app for android