ANDROID_MARS学习笔记_S03_005_Geocoder、AsyncTask

一、代码
1.xml
(1)AndroidManifest.xml

1     <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
2     <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>

 

2.java
(1)MainActivity.java

 1 package com.location3;
 2 
 3 import java.io.IOException;
 4 import java.util.List;
 5 
 6 import android.app.Activity;
 7 import android.location.Address;
 8 import android.location.Geocoder;
 9 import android.os.AsyncTask;
10 import android.os.Bundle;
11 import android.view.View;
12 import android.view.View.OnClickListener;
13 import android.widget.Button;
14 
15 public class MainActivity extends Activity {
16     private Button geocodingButton = null;
17     private Button reverseGeocodingButton = null;
18     
19     @Override
20     protected void onCreate(Bundle savedInstanceState) {
21         super.onCreate(savedInstanceState);
22         setContentView(R.layout.activity_main);
23         
24         geocodingButton = (Button)findViewById(R.id.getLocationButton);
25         reverseGeocodingButton = (Button)findViewById(R.id.getLocationNameButton);
26         
27         geocodingButton.setOnClickListener(new OnClickListener() {
28             @Override
29             public void onClick(View v) {
30                 new GeocodingTask().execute(new Integer(1));
31             }
32         });
33         
34         reverseGeocodingButton.setOnClickListener(new OnClickListener() {
35             @Override
36             public void onClick(View v) {
37                 new ReverseGeocodingTask().execute(new Integer(2));
38             }
39         });
40     }
41     
42     private class GeocodingTask extends AsyncTask<Integer, Integer, Integer> {
43         @Override
44         protected Integer doInBackground(Integer... params) {
45             //创建Geocoder对象,执行相关查询,但是存在未知BUG,导致服务不可用
46             Geocoder geocoder = new Geocoder(MainActivity.this);
47             try {
48                 //SFO是美国机场的一个简称
49                 List<Address> addresses = geocoder.getFromLocationName("SFO", 1);
50                 System.out.println("GeocodingTask addresses--->");
51                 for(Address address : addresses) {
52                     System.out.println(address);
53                 }
54             } catch (IOException e) {
55                 e.printStackTrace();
56             }
57             return null;
58         }
59     }
60     
61     private class ReverseGeocodingTask extends AsyncTask<Integer, Integer, Integer> {
62         @Override
63         protected Integer doInBackground(Integer... params) {
64             Geocoder geocoder = new Geocoder(MainActivity.this);
65             //根据给定的经纬度,查询符合条件的地址
66             try {
67                 List<Address> addresses = geocoder.getFromLocation(40.711442,-73.961452, 1);
68                 System.out.println("ReverseGeocodingTask addresses -->");
69                 for(Address address : addresses) {
70                     System.out.println(address);
71                 }
72             } catch (IOException e) {
73                 e.printStackTrace();
74             }
75             return null;
76         }
77     }
78 }

 

 

 

你可能感兴趣的:(ANDROID_MARS学习笔记_S03_005_Geocoder、AsyncTask)