我们获取Location的目的之一肯定是有获取这个位置的详细地址,而我们有了Location在来获取Address就相对简单多了,因为GoogleApi已经封装好了方法,我们只需呀通过Location获取GeoPoint,然后在通过GeoPoint来获取我们想要的Address.下面是我做的一个简单的Demo.
第一步新建一个Android工程LocationDemo,注意这里选用的是(Google APIs),下面是文件目录结构:
第二步: 修改main.xml(相比第十四节增加了一个address的TextView),代码如下:
<?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:"
/>
<TextView
android:id="@+id/address"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
第三步:修改LocationDemo.java(增加了两个方法)代码如下:
package com.android.tutor;
import java.util.List;
import java.util.Locale;
import com.google.android.maps.GeoPoint;
import android.app.Activity;
import android.content.Context;
import android.location.Address;
import android.location.Geocoder;
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;
private TextView address;
@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);
address = (TextView)findViewById(R.id.address);
Location mLocation = getLocation(this);
GeoPoint gp = getGeoByLocation(mLocation);
Address mAddress = getAddressbyGeoPoint(this, gp);
longitude.setText("Longitude: " + mLocation.getLongitude());
latitude.setText("Latitude: " + mLocation.getLatitude());
address.setText("Address: " + mAddress.getCountryName()+"," + mAddress.getLocality());
}
//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;
}
//通过Location获取GeoPoint
public GeoPoint getGeoByLocation(Location location) {
GeoPoint gp = null;
try {
if (location != null) {
double geoLatitude = location.getLatitude() * 1E6;
double geoLongitude = location.getLongitude() * 1E6;
gp = new GeoPoint((int) geoLatitude, (int) geoLongitude);
}
} catch (Exception e) {
e.printStackTrace();
}
return gp;
}
//通过GeoPoint来获取Address
public Address getAddressbyGeoPoint(Context cntext, GeoPoint gp) {
Address result = null;
try {
if (gp != null) {
Geocoder gc = new Geocoder(cntext, Locale.CHINA);
double geoLatitude = (int) gp.getLatitudeE6() / 1E6;
double geoLongitude = (int) gp.getLongitudeE6() / 1E6;
List<Address> lstAddress = gc.getFromLocation(geoLatitude,
geoLongitude, 1);
if (lstAddress.size() > 0) {
result = lstAddress.get(0);
}
}
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
}
第四步:最重要一步在AndroidManiefest.xml中导入Google Api(第14行代码)库,代码如下:
<?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>
<uses-library android:name="com.google.android.maps" />
</application>
<uses-sdk android:minSdkVersion="7" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
</manifest>
第五步:运行上述工程,效果如下图如示:
本文转自
http://blog.csdn.net/Android_Tutor/archive/2010/06/24/5692517.aspx
- 大小: 25.8 KB
- 大小: 28.2 KB
分享到:
相关推荐
Android开发—通过Location获取Address,获取手机一些信息
Location 在Android 开发中还是经常用到的,比如 通过经纬度获取天气,根据Location 获取所在地区详细Address (比如Google Map 开发).等。而在Android 中通过LocationManager 来获取Location .通常获取Location 有...
Location 在Android 开发中还是经常用到的,如通过经纬度获取天气,根据Location 获取所在地区详细Address (比如Google Map 开发)等。而在Android 中通过LocationManager来获取Location .通常获取Location 有GPS ...
IP2Location Go套餐此Go软件包可快速查找国家,地区,城市,纬度,经度,邮政编码,时区,ISP,域名,连接类型,IDD代码,区号,气象站代码,站名,MCC,MNC,移动电话使用IP2Location数据库从IP地址获取品牌,海拔...
com-amap-location 描述 this is a plugin get location info by use GaoDe Map...#### address ### country #### province #### city #### district #### street #### locationDetail 失败回调参数属性 #### code ##
Android高手进阶教程之----通过Location获取Address的使用.doc Android基础教程之----Android ProgressBar的使用.doc Android基础教程之----Android中两种设置全屏的方法!!.doc Android基础教程之----Android状态栏...
获取设备的物理位置并将其输出为json。 OVERVIEW: Output your current location USAGE: location OPTIONS: --address, -a Lookup reverse geocoded address --follow, -f Keep polling for new location ...
废话不多说了,直接给大家贴js代码了。具体代码如下所示: function Location(){}; Location.prototype.getLocation = function(callback){ var options = { enableHighAccuracy: true, ... function(address){
中国省市县三级政府驻地详细数据(含经纬度),省市县行政区由高德开发者中心获取,通过兴趣点查询接口,爬取经纬度等详细信息,整理成excel表,供行政区驻地配图等场景使用。 2、字段信息: typecode 省市县类型 ...
iplocation 获取IP位置信息。安装npm install iplocation用法const ipLocation = require ( "iplocation" ) ;...提供者iplocation当前使用进行IP查找。 他们的免费API每天最多只能处理1000个请求。有关的此模块的CLI。
//获取收件人地址集合 message.Subject = Theme;//邮箱主题 message.Body = Content;//内容 message.BodyEncoding = System.Text.Encoding.UTF8; message.IsBodyHtml = true; try { client.Send(message); ...
从可用的内存中获取一部分内存。 向可用内存池(pool)中返回部分内存,以使其可以由程序的其他部分或者其他程序使用。 实现这些需求的程序库称为分配程序(allocators),因为它们负责分配和回收内存。程序的...
该服务提供的API可以从地址获取地理位置以及日出和日落时间。 Scenario : Get Location Given I've an address When I call the location service Then I should get a geo location And I should get the sun...
文将对 Linux™ 程序员可以使用的内存管理技术进行概述,虽然关注的重点是 C 语言,但同样也适用于其他语言。文中将为您提供如何管理内存的细节,然后将进一步展示如何手工管理内存,如何使用引用计数或者内存池来半...
GET 获取我的所有车辆/address PUT 新增地址 GET 获取我的所有地址/order POST 更新订单 PUT 新增订单/signin POST 使用手机号验证码登录/signout/vcode GET 获取验证码/cartype GET 搜索车型提示/location/latlng/:...
我们使用SELF(Simple Embedded Linux Framework)来展示如何建立一个开发环境(包括通过NFS挂载的根文件系统)和一个嵌入式目标板配置(从基于busybox的ramdisk映像文件中运行)。 本文档不会给出如何把U-Boot或者...
Get the list of major cities of a state in India.Get list of States and Union Territories of India.Get city/area from the STD code in India.Get STD dialing code for any location in India.Get address,...
使用Ipify的API,该库使使用dart语言获取public IP address或给定IP地址的精确geographical details变得非常简单。 安装 dependencies : dart_ipify : ^1.1.0 用法 使用该库非常简单。 这是一个简单的例子: IP地址...
获取iPhone SDK 5 提供反馈 5 其他参考资料 6 iOS 开发相关信息 6 iOS 架构 6 iPhone SDK 包含哪些东西? 8 您可以在 iOS 创建什么应用程序? 9 如何使用参考库? 9 Cocoa Touch 层 12 高级特性 12 多任务 12 数据...
* getHost 获取目标邮箱服务器类型 * * @param sender * 是String类型,传入邮件发送者邮箱地址信息 * @return String 返回目标邮箱服务器类型 */ private String getHost(String sender) { String ...