Skip to content

Commit ea206ef

Browse files
committed
Initial commit
0 parents  commit ea206ef

File tree

18 files changed

+698
-0
lines changed

18 files changed

+698
-0
lines changed

app/.gitignore

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# built application files
2+
*.ap_
3+
4+
# files for the dex VM
5+
*.dex
6+
7+
# Java class files
8+
*.class
9+
10+
# generated files
11+
bin/
12+
gen/
13+
14+
# Local configuration file (sdk path, etc)
15+
local.properties
16+
17+
# Proguard folder generated by Eclipse
18+
proguard/
19+
20+
# Ignore gradle files
21+
.gradle/
22+
build/
23+
24+
# Eclipse project files
25+
.classpath
26+
.project
27+
.settings/
28+
29+
# Intellij project files
30+
*.iml
31+
*.ipr
32+
*.iws
33+
.idea/
34+
35+
# Mac system files
36+
.DS_Store
37+
38+
*.keystore

app/build.gradle

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
apply plugin: 'com.android.application'
2+
3+
android {
4+
compileSdkVersion 22
5+
buildToolsVersion "22.0.1"
6+
7+
defaultConfig {
8+
applicationId "itangqi.me.mygoogleplaces"
9+
minSdkVersion 16
10+
targetSdkVersion 22
11+
versionCode 1
12+
versionName "1.0"
13+
}
14+
buildTypes {
15+
release {
16+
minifyEnabled false
17+
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
18+
}
19+
}
20+
}
21+
22+
dependencies {
23+
compile fileTree(dir: 'libs', include: ['*.jar'])
24+
compile 'com.android.support:appcompat-v7:22.2.1'
25+
compile 'com.google.android.gms:play-services:7.5.0'
26+
}

app/proguard-rules.pro

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Add project specific ProGuard rules here.
2+
# By default, the flags in this file are appended to flags specified
3+
# in /Users/tangqi/android-dev/sdk/tools/proguard/proguard-android.txt
4+
# You can edit the include path and order by changing the proguardFiles
5+
# directive in build.gradle.
6+
#
7+
# For more details, see
8+
# http://developer.android.com/guide/developing/tools/proguard.html
9+
10+
# Add any project specific keep options here:
11+
12+
# If your project uses WebView with JS, uncomment the following
13+
# and specify the fully qualified class name to the JavaScript interface
14+
# class:
15+
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
16+
# public *;
17+
#}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package itangqi.me.mygoogleplaces;
2+
3+
import android.app.Application;
4+
import android.test.ApplicationTestCase;
5+
6+
/**
7+
* <a href="http://d.android.com/tools/testing/testing_android.html">Testing Fundamentals</a>
8+
*/
9+
public class ApplicationTest extends ApplicationTestCase<Application> {
10+
public ApplicationTest() {
11+
super(Application.class);
12+
}
13+
}

app/src/main/AndroidManifest.xml

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3+
package="itangqi.me.mygoogleplaces">
4+
5+
<!-- PlacePicker requires the ACCESS_FINE_LOCATION permission and a geo API key.
6+
-->
7+
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
8+
<uses-permission android:name="android.permission.INTERNET" />
9+
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
10+
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
11+
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
12+
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
13+
14+
<!-- PlacePicker also requires OpenGL ES version 2 -->
15+
<uses-feature
16+
android:glEsVersion="0x00020000"
17+
android:required="true" />
18+
19+
<application
20+
android:allowBackup="true"
21+
android:icon="@mipmap/ic_launcher"
22+
android:label="@string/app_name"
23+
android:theme="@style/AppTheme">
24+
25+
<meta-data
26+
android:name="com.google.android.gms.version"
27+
android:value="@integer/google_play_services_version" />
28+
29+
<meta-data
30+
android:name="com.google.android.geo.API_KEY"
31+
android:value="AIzaSyDWF5Al83s2a9P1JctqM3um9usNXEpVa2U" />
32+
33+
<activity
34+
android:name=".MainActivity"
35+
android:label="@string/app_name">
36+
<intent-filter>
37+
<action android:name="android.intent.action.MAIN" />
38+
39+
<category android:name="android.intent.category.LAUNCHER" />
40+
</intent-filter>
41+
</activity>
42+
</application>
43+
44+
</manifest>
Lines changed: 232 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,232 @@
1+
/*
2+
* Copyright (C) 2015 Google Inc. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package itangqi.me.mygoogleplaces;
18+
19+
import android.content.res.Resources;
20+
import android.net.Uri;
21+
import android.os.Bundle;
22+
import android.support.v7.app.AppCompatActivity;
23+
import android.text.Html;
24+
import android.text.Spanned;
25+
import android.util.Log;
26+
import android.view.View;
27+
import android.widget.AdapterView;
28+
import android.widget.AutoCompleteTextView;
29+
import android.widget.Button;
30+
import android.widget.TextView;
31+
import android.widget.Toast;
32+
33+
import com.google.android.gms.common.ConnectionResult;
34+
import com.google.android.gms.common.api.GoogleApiClient;
35+
import com.google.android.gms.common.api.PendingResult;
36+
import com.google.android.gms.common.api.ResultCallback;
37+
import com.google.android.gms.location.places.Place;
38+
import com.google.android.gms.location.places.PlaceBuffer;
39+
import com.google.android.gms.location.places.PlaceLikelihoodBuffer;
40+
import com.google.android.gms.location.places.Places;
41+
import com.google.android.gms.maps.model.LatLng;
42+
import com.google.android.gms.maps.model.LatLngBounds;
43+
44+
public class MainActivity extends AppCompatActivity
45+
implements GoogleApiClient.OnConnectionFailedListener {
46+
/**
47+
* GoogleApiClient wraps our service connection to Google Play Services and provides access
48+
* to the user's sign in state as well as the Google's APIs.
49+
*/
50+
private static final int GOOGLE_API_CLIENT_ID = 0;
51+
52+
protected GoogleApiClient mGoogleApiClient;
53+
54+
private PlaceAutocompleteAdapter mAdapter;
55+
56+
private AutoCompleteTextView mAutocompleteView;
57+
58+
private TextView mPlaceDetailsText;
59+
60+
private TextView mPlaceDetailsAttribution;
61+
62+
private Button mCurrentLocation;
63+
64+
private static final String TAG = "MainActivity";
65+
66+
private static final LatLngBounds BOUNDS_GREATER_SYDNEY = new LatLngBounds(
67+
new LatLng(-34.041458, 150.790100), new LatLng(-33.682247, 151.383362));
68+
69+
@Override
70+
protected void onCreate(Bundle savedInstanceState) {
71+
super.onCreate(savedInstanceState);
72+
73+
// Construct a GoogleApiClient for the {@link Places#GEO_DATA_API} using AutoManage
74+
// functionality, which automatically sets up the API client to handle Activity lifecycle
75+
// events. If your activity does not extend FragmentActivity, make sure to call connect()
76+
// and disconnect() explicitly.
77+
mGoogleApiClient = new GoogleApiClient.Builder(this)
78+
.enableAutoManage(this, GOOGLE_API_CLIENT_ID /* clientId */, this)
79+
.addApi(Places.GEO_DATA_API)
80+
.addApi(Places.PLACE_DETECTION_API)
81+
.build();
82+
83+
setContentView(R.layout.activity_main);
84+
85+
86+
// Retrieve the AutoCompleteTextView that will display Place suggestions.
87+
mAutocompleteView = (AutoCompleteTextView)
88+
findViewById(R.id.autocomplete_places);
89+
90+
// Register a listener that receives callbacks when a suggestion has been selected
91+
mAutocompleteView.setOnItemClickListener(mAutocompleteClickListener);
92+
93+
// Retrieve the TextViews that will display details and attributions of the selected place.
94+
mPlaceDetailsText = (TextView) findViewById(R.id.place_details);
95+
mPlaceDetailsAttribution = (TextView) findViewById(R.id.place_attribution);
96+
97+
// CurrentLocation
98+
mCurrentLocation = (Button) findViewById(R.id.ll_current_location);
99+
mCurrentLocation.setOnClickListener(mOnClickListener);
100+
101+
// Set up the adapter that will retrieve suggestions from the Places Geo Data API that cover
102+
// the entire world.
103+
mAdapter = new PlaceAutocompleteAdapter(this, android.R.layout.simple_list_item_1,
104+
mGoogleApiClient, BOUNDS_GREATER_SYDNEY, null);
105+
mAutocompleteView.setAdapter(mAdapter);
106+
}
107+
108+
109+
private View.OnClickListener mOnClickListener = new View.OnClickListener() {
110+
@Override
111+
public void onClick(View view) {
112+
PendingResult<PlaceLikelihoodBuffer> result = Places.PlaceDetectionApi
113+
.getCurrentPlace(mGoogleApiClient, null);
114+
result.setResultCallback(new ResultCallback<PlaceLikelihoodBuffer>() {
115+
@Override
116+
public void onResult(PlaceLikelihoodBuffer likelyPlaces) {
117+
if (!likelyPlaces.getStatus().isSuccess()) {
118+
// Request did not complete successfully
119+
Log.e(TAG, "Place query did not complete. Error: " + likelyPlaces.getStatus().toString());
120+
likelyPlaces.release();
121+
return;
122+
}
123+
String placeName = String.format("%s", likelyPlaces.get(0).getPlace().getName());
124+
mPlaceDetailsText.setText(placeName);
125+
likelyPlaces.release();
126+
}
127+
});
128+
}
129+
};
130+
131+
132+
/**
133+
* Listener that handles selections from suggestions from the AutoCompleteTextView that
134+
* displays Place suggestions.
135+
* Gets the place id of the selected item and issues a request to the Places Geo Data API
136+
* to retrieve more details about the place.
137+
*
138+
* @see com.google.android.gms.location.places.GeoDataApi#getPlaceById(com.google.android.gms.common.api.GoogleApiClient,
139+
* String...)
140+
*/
141+
private AdapterView.OnItemClickListener mAutocompleteClickListener
142+
= new AdapterView.OnItemClickListener() {
143+
@Override
144+
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
145+
/*
146+
Retrieve the place ID of the selected item from the Adapter.
147+
The adapter stores each Place suggestion in a PlaceAutocomplete object from which we
148+
read the place ID.
149+
*/
150+
final PlaceAutocompleteAdapter.PlaceAutocomplete item = mAdapter.getItem(position);
151+
final String placeId = String.valueOf(item.placeId);
152+
Log.i(TAG, "Autocomplete item selected: " + item.description);
153+
154+
/*
155+
Issue a request to the Places Geo Data API to retrieve a Place object with additional
156+
details about the place.
157+
*/
158+
PendingResult<PlaceBuffer> placeResult = Places.GeoDataApi
159+
.getPlaceById(mGoogleApiClient, placeId);
160+
placeResult.setResultCallback(mUpdatePlaceDetailsCallback);
161+
Log.i(TAG, "Called getPlaceById to get Place details for " + item.placeId);
162+
}
163+
};
164+
165+
166+
/**
167+
* Callback for results from a Places Geo Data API query that shows the first place result in
168+
* the details view on screen.
169+
*/
170+
private ResultCallback<PlaceBuffer> mUpdatePlaceDetailsCallback
171+
= new ResultCallback<PlaceBuffer>() {
172+
@Override
173+
public void onResult(PlaceBuffer places) {
174+
if (!places.getStatus().isSuccess()) {
175+
// Request did not complete successfully
176+
Log.e(TAG, "Place query did not complete. Error: " + places.getStatus().toString());
177+
places.release();
178+
return;
179+
}
180+
// Get the Place object from the buffer.
181+
final Place place = places.get(0);
182+
183+
// Format details of the place for display and show it in a TextView.
184+
mPlaceDetailsText.setText(formatPlaceDetails(getResources(), place.getName(),
185+
place.getId(), place.getAddress(), place.getPhoneNumber(),
186+
place.getWebsiteUri()));
187+
188+
// Display the third party attributions if set.
189+
final CharSequence thirdPartyAttribution = places.getAttributions();
190+
if (thirdPartyAttribution == null) {
191+
mPlaceDetailsAttribution.setVisibility(View.GONE);
192+
} else {
193+
mPlaceDetailsAttribution.setVisibility(View.VISIBLE);
194+
mPlaceDetailsAttribution.setText(Html.fromHtml(thirdPartyAttribution.toString()));
195+
}
196+
197+
Log.i(TAG, "Place details received: " + place.getName());
198+
199+
places.release();
200+
}
201+
};
202+
203+
private static Spanned formatPlaceDetails(Resources res, CharSequence name, String id,
204+
CharSequence address, CharSequence phoneNumber, Uri websiteUri) {
205+
Log.e(TAG, res.getString(R.string.place_details, name, id, address, phoneNumber,
206+
websiteUri));
207+
return Html.fromHtml(res.getString(R.string.place_details, name, id, address, phoneNumber,
208+
websiteUri));
209+
210+
}
211+
212+
/**
213+
* Called when the Activity could not connect to Google Play services and the auto manager
214+
* could resolve the error automatically.
215+
* In this case the API is not available and notify the user.
216+
*
217+
* @param connectionResult can be inspected to determine the cause of the failure
218+
*/
219+
@Override
220+
public void onConnectionFailed(ConnectionResult connectionResult) {
221+
222+
Log.e(TAG, "onConnectionFailed: ConnectionResult.getErrorCode() = "
223+
+ connectionResult.getErrorCode());
224+
225+
// TODO(Developer): Check error code and notify the user of error state and resolution.
226+
Toast.makeText(this,
227+
"Could not connect to Google API Client: Error " + connectionResult.getErrorCode(),
228+
Toast.LENGTH_SHORT).show();
229+
MainActivity.this.finish();
230+
}
231+
232+
}

0 commit comments

Comments
 (0)