Cdacians

Cdacians
Cdacians

Tuesday 7 August 2012

Start Google Maps using intent with specified location


Start Google Maps using intent with specified location

Modify from the last post get latitude and longitude from android.location.Address, start a intent of ACTION_VIEW, with location; to start Google Maps on the location.

Start Google Maps using intent with specified location

?
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package com.AndroidgetFromLocationName;
 
import java.io.IOException;
import java.util.List;
 
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.location.Address;
import android.location.Geocoder;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
 
public class AndroidgetFromLocationNameActivity extends Activity {
 
 EditText searchText;
 Button searchButton;
 ListView listviewResult;
 Button goButton;
  
 Geocoder myGeocoder;
 final static int MAX_RESULT = 10;
 final static String DEFAULT_SEARCH = "London";
  
 double golat, golon;
 boolean govalid;
  
    /** 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);
     listviewResult = (ListView)findViewById(R.id.listviewresult);
     goButton = (Button)findViewById(R.id.gobutton);
      
     searchButton.setOnClickListener(searchButtonOnClickListener);
     goButton.setOnClickListener(goButtonOnClickListener);
      
     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();
     }
      
     listviewResult.setOnItemClickListener(listviewResultOnItemClickListener);
      
     govalid = false;
    }
     
    Button.OnClickListener goButtonOnClickListener
    = new Button.OnClickListener(){
 
  @Override
  public void onClick(View arg0) {
   // TODO Auto-generated method stub
   if(govalid){
 
    final String zoom = "12";
    String stringLoc = "geo:" + golat + "," + golon + "?z=" + zoom;
 
    Intent intent = new Intent(android.content.Intent.ACTION_VIEW, Uri.parse(stringLoc));
    startActivity(intent);
 
   }
  }};
     
    OnItemClickListener listviewResultOnItemClickListener
    = new OnItemClickListener(){
 
  @Override
  public void onItemClick(AdapterView<?> parent, View view, int position,
    long id) {
   // TODO Auto-generated method stub
    
   double lat = ((Address)parent.getItemAtPosition(position)).getLatitude();
   double lon = ((Address)parent.getItemAtPosition(position)).getLongitude();
   String loc = "lat: " + lat + "\n" + "lon: " + lon;
   Toast.makeText(AndroidgetFromLocationNameActivity.this,
     loc,
     Toast.LENGTH_LONG).show();
    
   govalid = true;
   golat = lat;
      golon = lon;
  }
   
 };
     
    Button.OnClickListener searchButtonOnClickListener
    = new Button.OnClickListener(){
 
  @Override
  public void onClick(View arg0) {
   // TODO Auto-generated method stub
   String searchString = searchText.getText().toString();
   searchFromLocationName(searchString);
    
   govalid = false;
  }};
   
 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{
 
    MyArrayAdapter adapter = new MyArrayAdapter(this,
          android.R.layout.simple_list_item_1, result);
    listviewResult.setAdapter(adapter);
     
    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();
  }
 }
  
 public class MyArrayAdapter extends ArrayAdapter<Address> {
  Context mycontext;
  public MyArrayAdapter(Context context, int textViewResourceId,
    List<Address> objects) {
   super(context, textViewResourceId, objects);
   // TODO Auto-generated constructor stub
   mycontext = context;
  }
 
  @Override
  public View getView(int position, View convertView, ViewGroup parent) {
   // TODO Auto-generated method stub
    
   int maxAddressLineIndex = getItem(position).getMaxAddressLineIndex();
   String addressLine = "";
    
   for (int j = 0; j <= maxAddressLineIndex; j++){
    addressLine += getItem(position).getAddressLine(j) + ",";
   }
    
   TextView rowAddress = new TextView(mycontext);
   rowAddress.setText(addressLine);
    
   return rowAddress;
 
  }
 
 }
  
}


akm
www.cdacians.com

No comments:

Post a Comment