顺便写的一个经纬度测试:
主程序:
1 package com.amaker.ch14.app02; 2 3 import java.io.IOException; 4 import java.util.List; 5 import java.util.Locale; 6 7 import android.app.Activity; 8 import android.content.Context; 9 import android.location.Address; 10 import android.location.Geocoder; 11 import android.location.Location; 12 import android.location.LocationManager; 13 import android.os.Bundle; 14 import android.view.View; 15 import android.view.View.OnClickListener; 16 import android.widget.Button; 17 import android.widget.TextView; 18 19 public class MainActivity extends Activity { 20 // 声明TextView 21 private TextView tv; 22 // 声明Button 23 private Button b1,b2; 24 // 定位服务管理器实例 25 private LocationManager locationManager; 26 27 @Override 28 public void onCreate(Bundle savedInstanceState) { 29 // 设置当前Activity的界面布局 30 super.onCreate(savedInstanceState); 31 setContentView(R.layout.main); 32 // 定位服务常量 33 String locService = Context.LOCATION_SERVICE; 34 // 通过getSystemService方法获得LocationManager实例 35 locationManager = (LocationManager)getSystemService(locService); 36 // 通过findViewById方法获得TextView实例 37 tv = (TextView) findViewById(R.id.TextView01); 38 39 // 通过findViewById方法获得Button实例 40 b1 = (Button) findViewById(R.id.Button01); 41 // 通过findViewById方法获得Button实例 42 b2 = (Button) findViewById(R.id.Button02); 43 // 为按钮添加单击事件监听器 44 b1.setOnClickListener(new OnClickListener() { 45 public void onClick(View v) { 46 // 正向编码 47 forward(); 48 } 49 }); 50 51 // 为按钮添加单击事件监听器 52 b2.setOnClickListener(new OnClickListener() { 53 public void onClick(View v) { 54 // 反向编码 55 reverse(); 56 } 57 }); 58 } 59 60 // 正向编码 61 private void forward() { 62 // 实例化Geocoder 63 Geocoder gc = new Geocoder(this, Locale.getDefault()); 64 // 地址 65 String address = "山东"; 66 // 位置列表 67 List<Address> locations = null; 68 try { 69 // 获得位置类别 70 locations = gc.getFromLocationName(address, 10); 71 // 如果Locations不为空 72 if(locations.size()>0){ 73 // 获得Address实例 74 Address a = locations.get(0); 75 // 获得经纬度 76 double lat = a.getLatitude(); 77 double lng = a.getLongitude(); 78 // 显示信息 79 tv.setText(lat+":"+lng); 80 } 81 } catch (IOException e) {} 82 } 83 84 85 // 反向编码 86 private void reverse() { 87 // 经度 88 double lng = 117.46; 89 // 纬度 90 double lat = 36.92; 91 // 实例化Geocoder 92 Geocoder gc = new Geocoder(this, Locale.getDefault()); 93 // 声明地址列表 94 List<Address> addresses = null; 95 try { 96 // 获得Address实例列表 97 addresses = gc.getFromLocation(lat, lng, 10); 98 // 声明StringBuilder,保存位置信息 99 StringBuilder sb = new StringBuilder(); 100 // 判断addresses是否为空 101 if(addresses.size()>0){ 102 // 获得Address 103 Address a = addresses.get(0); 104 for (int i = 0; i < a.getMaxAddressLineIndex(); i++){ 105 // 地址 106 sb.append(a.getAddressLine(i)).append("\n"); 107 // 地点 108 sb.append(a.getLocality()).append("\n"); 109 // 邮编 110 sb.append(a.getPostalCode()).append("\n"); 111 // 国家名称 112 sb.append(a.getCountryName()); 113 } 114 tv.setText(sb.toString()); 115 } 116 } catch (IOException e) {} 117 } 118 }
xml文件:
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:orientation="vertical" 4 android:layout_width="fill_parent" 5 android:layout_height="fill_parent" 6 > 7 <TextView 8 android:text="显示位置信息:" 9 android:id="@+id/TextView01" 10 android:layout_width="wrap_content" 11 android:layout_height="wrap_content"></TextView> 12 13 <Button 14 android:text="正向编码" 15 android:id="@+id/Button01" 16 android:layout_width="wrap_content" 17 android:layout_height="wrap_content"></Button> 18 19 <Button 20 android:text="反向编码" 21 android:id="@+id/Button02" 22 android:layout_width="wrap_content" 23 android:layout_height="wrap_content"></Button> 24 25 </LinearLayout>
清单文件添加权限不在哆嗦