Get full address from result of getFromLocationName()
In the last post, it was shown how to Get address from location name, using Geocoder. But in the example code, the address line is in-correct. Refer to the following code, to retrieve full address line.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
| package com.AndroidgetFromLocationName; import java.io.IOException; import java.util.List; import android.app.Activity; import android.location.Address; import android.location.Geocoder; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; import android.widget.Toast; public class AndroidgetFromLocationNameActivity extends Activity { EditText searchText; Button searchButton; TextView listResult; Geocoder myGeocoder; final static int MAX_RESULT = 5 ; //final static String DEFAULT_SEARCH = "1600 Amphitheatre Parkway Mountain View"; //final static String DEFAULT_SEARCH = "Times Square"; final static String DEFAULT_SEARCH = "蘋果" ; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.main); searchText = (EditText)findViewById(R.id.searchtext); searchText.setText(DEFAULT_SEARCH); searchButton = (Button)findViewById(R.id.searchbutton); listResult = (TextView)findViewById(R.id.result); searchButton.setOnClickListener(searchButtonOnClickListener); myGeocoder = new Geocoder( this ); //for API Level 9 or higher if (!Geocoder.isPresent()){ Toast.makeText(AndroidgetFromLocationNameActivity. this , "Sorry! Geocoder service not Present." , Toast.LENGTH_LONG).show(); } } Button.OnClickListener searchButtonOnClickListener = new Button.OnClickListener(){ @Override public void onClick(View arg0) { // TODO Auto-generated method stub String searchString = searchText.getText().toString(); searchFromLocationName(searchString); }}; private void searchFromLocationName(String name){ try { List<Address> result = myGeocoder.getFromLocationName(name, MAX_RESULT); if ((result == null )||(result.isEmpty())){ Toast.makeText(AndroidgetFromLocationNameActivity. this , "No matches were found or there is no backend service!" , Toast.LENGTH_LONG).show(); } else { String stringResult = "" ; for ( int i = 0 ; i < result.size(); i++){ String AddressLine = "" ; int maxAddressLineIndex = result.get(i).getMaxAddressLineIndex(); if (maxAddressLineIndex != - 1 ){ for ( int j = 0 ; j <= result.get(i).getMaxAddressLineIndex(); j++){ AddressLine += result.get(i).getAddressLine(j) + "\n" ; } } stringResult += "i: " + i + "\n" + "lat: " + result.get(i).getLatitude() + "\n" + "lon: " + result.get(i).getLongitude() + "\n" + AddressLine + "\n" ; } listResult.setText(stringResult); Toast.makeText(AndroidgetFromLocationNameActivity. this , "Finished!" , Toast.LENGTH_LONG).show(); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); Toast.makeText(AndroidgetFromLocationNameActivity. this , "The network is unavailable or any other I/O problem occurs!" , Toast.LENGTH_LONG).show(); } } }
akm www.cdacians.com |
Good sample, Thank you
ReplyDelete