2015-09-17 38 views
0

我试图将Google Places API整合到我的应用程序中。Google Places API - 地方选择器不等待地方选择

我也做了初步的东西,但由于某些原因,当我通过

startActivityForResult(Intent, Int);

应用程序将简单地弹出这个地方选择器的意图,但是用户之前将关闭有机会选择启动活动一个位置并返回一个'不好'的Activity.Result。

这里是设置了活动几乎完全一样的东西发射联系人选择意图和做工精细沿码,

mApiClient = new GoogleApiClient 
     .Builder(this) 
     .addApi(Places.GEO_DATA_API) 
     .addApi(Places.PLACE_DETECTION_API) 
     .addConnectionCallbacks(this) 
     .addOnConnectionFailedListener(this) 
     .build(); 

Intent intent = getIntent(); 
getIntentData(intent); 

/*Contact Select, works fine*/ 
int resulter = 3; 
Intent intenter = new Intent(Intent.ACTION_PICK, Uri.parse("content://contact")); 
intenter.setType(ContactsContract.CommonDataKinds.Phone.CONTENT_TYPE); 
startActivityForResult(intenter, resulter); 

try { 
    PlacePicker.IntentBuilder builder = new PlacePicker.IntentBuilder(); 
    startActivityForResult(builder.build(this), PLACE_PICKER_REQUEST); 
} catch (GooglePlayServicesRepairableException e) { 
    Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show(); 
} catch (GooglePlayServicesNotAvailableException e) { 
    e.printStackTrace(); 
} 

,这里是回调,这将返回该联系人的正确信息选择(请求代码= 3),但不正确的信息的地方选择器(请求代码= 2 [这部分是正确的,但结果的代码是不正确]),

protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    if (requestCode == PLACE_PICKER_REQUEST) { 
     if (resultCode == Activity.RESULT_OK) { 
      Place place = PlacePicker.getPlace(data, this); 
      String toastMsg = String.format("Place: %s", place.getName()); 
      Toast.makeText(this, toastMsg, Toast.LENGTH_LONG).show(); 
     } 
    } else if (requestCode == 3) { 
     Toast.makeText(this, "wwhhhahahahahahahahahaah", Toast.LENGTH_LONG).show(); 
    } 
} 

我的Android清单:

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.hytexsoftware.dummy" > 
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/> 

    <application 
     android:allowBackup="true" 
     android:icon="@mipmap/ic_launcher" 
     android:label="@string/app_name" 
     android:theme="@style/AppTheme" > 
     <meta-data 
      android:name="com.google.android.geo.API_KEY" 
      android:value="API_KEY"/> 
     <activity 
      android:name=".MainActivity" 
      android:label="@string/app_name" > 
     </activity> 
     <activity 
      android:name=".LoadScreen" 
      android:label="@string/title_activity_load_screen" > 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
     <activity 
      android:name=".MapView" 
      android:label="@string/title_activity_map_view" > 
     </activity> 
     <activity 
      android:name=".DisplayLocations" 
      android:label="@string/title_activity_display_locations" > 
     </activity> 
     <activity 
      android:name=".util.ShowLocationOnMap" 
      android:label="@string/title_activity_show_location_on_map" > 
     </activity> 

    </application>  
</manifest> 
+0

显示您的清单文件,问题可能就在那里。 –

+0

您需要遵循注册和API密钥说明[此处](https://developers.google.com/places/android-api/signup)并将API密钥添加到您的AndroidManifest。 –

+0

我已经添加了API密钥,请参阅刚添加的清单内容。或者至少我相信我已经做到了这一点。我遵循了精确的教程来做到这一点。 – TheRealKernel

回答

0

转到Google Developer Console并为您的项目启用适用于Android的Google Places API