文档章节

Android高手进阶教程(十五)之---通过Location获取Address的使用!

o
 osc_1ee7cxmx
发布于 2018/08/06 14:42
字数 560
阅读 0
收藏 0

精选30+云产品,助力企业轻松上云!>>>

我们获取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>

第五步:运行上述工程,效果如下图如示:

o
粉丝 0
博文 500
码字总数 0
作品 0
私信 提问
加载中
请先登录后再评论。

暂无文章

MongoDB入门系列——3.可视化工具篇

点击上方,轻松关注!! 前面我们已经介绍了MongoDB怎么安装,接下来要安装他的可视化工具——Studio 3T。 先到这下载一个压缩包,百度网盘,https://pan.baidu.com/s/1M8mlWo334KE8I1_UA2Da...

学习Java的小姐姐
2018/11/08
17
0
分层图的绘制 python(来自国外课程)

Exercise 10: Hierarchical clustering of the grain data In the video, you learnt that the SciPy linkage() function performs hierarchical clustering on an array of samples. Use th......

齐勇cn
42分钟前
13
0
微信小程序超简单的双向绑定(类似vue的v-model)

<input model:value="{{value}}" />

祖达
43分钟前
9
0
为什么AngularJS在select中包含一个空选项? - Why does AngularJS include an empty option in select?

问题: I've been working with AngularJS for the last few weeks, and the one thing which is really bothering me is that even after trying all permutations or the configuration de......

技术盛宴
46分钟前
13
0
centos宝塔面板安装及常见错误处理(超级详细)

原文连接:https://www.wjcms.net/archives/centos%E5%AE%9D%E5%A1%94%E9%9D%A2%E6%9D%BF%E5%AE%89%E8%A3%85%E5%8F%8A%E5%B8%B8%E8%A7%81%E9%94%99%E8%AF%AF%E5%A4%84%E7%90%86%E8%B6%85%E7%......

神兵小将
今天
17
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部