ANDROID_MARS 학습노트S03_005_Geocoder、AsyncTask

11260 단어
1. 코드 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 }

 
 

좋은 웹페이지 즐겨찾기