2017-04-30 13 views
0

小さなアプリにGoogle API Place Pickerを使用しようとしています。場所ピッカーが開いていないという問題があり、数秒後にシャットダウンします。 スタックオーバーフローのソリューションの多くは、最も一般的な原因が悪いマニフェストであると言いますが、私は私がそうではないと思います。ここ 私のマニフェスト:Google Place Pickerが開いていない

<?xml version="1.0" encoding="utf-8"?> 

<!-- 
    The ACCESS_COARSE/FINE_LOCATION permissions are not required to use 
    Google Maps Android API v2, but you must specify either coarse or fine 
    location permissions for the 'MyLocation' functionality. 
--> 
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> 

<application 
    android:allowBackup="true" 
    android:icon="@mipmap/ic_launcher" 
    android:label="@string/app_name" 
    android:roundIcon="@mipmap/ic_launcher_round" 
    android:supportsRtl="true" 
    android:theme="@style/AppTheme"> 

    <!-- 
     The API key for Google Maps-based APIs is defined as a string resource. 
     (See the file "res/values/google_maps_api.xml"). 
     Note that the API key is linked to the encryption key used to sign the APK. 
     You need a different API key for each encryption key, including the release key that is used to 
     sign the APK for publishing. 
     You can define the keys for the debug and release targets in src/debug/ and src/release/. 
    --> 
    <meta-data 
     android:name="com.google.android.geo.API_KEY" 
     android:value="@string/google_maps_key" /> 
    <meta-data 
     android:name="com.google.android.gms.version" 
     android:value="@integer/google_play_services_version"/> 
    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/> 
    <uses-permission android:name="android.permission.INTERNET"/> 
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/> 

    <activity 
     android:name=".MapsActivity" 
     android:label="@string/title_activity_maps"> 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 

      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 
</application> 

ここでは私の主な活動:

package com.example.lorenzo.mysimplemap; 

import android.support.v4.app.FragmentActivity; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.View; 
import android.widget.Button; 

import com.google.android.gms.common.GooglePlayServicesNotAvailableException; 
import com.google.android.gms.common.GooglePlayServicesRepairableException; 
import com.google.android.gms.maps.CameraUpdateFactory; 
import com.google.android.gms.maps.GoogleMap; 
import com.google.android.gms.maps.OnMapReadyCallback; 
import com.google.android.gms.maps.SupportMapFragment; 
import com.google.android.gms.maps.model.LatLng; 
import com.google.android.gms.maps.model.MarkerOptions; 
import com.google.android.gms.location.places.ui.PlacePicker; 

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback { 

    int PLACE_PICKER_REQUEST = 1; 

    private GoogleMap mMap; 
    Button pickPlaceButton; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_maps); 
     // Obtain the SupportMapFragment and get notified when the map is ready to be used. 
     SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager() 
       .findFragmentById(R.id.map); 
     mapFragment.getMapAsync(this); 

     pickPlaceButton = (Button)findViewById(R.id.pick_place_button); 
     pickPlaceButton.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       try{ 
        PlacePicker.IntentBuilder builder = new PlacePicker.IntentBuilder(); 
        startActivityForResult(builder.build(MapsActivity.this), PLACE_PICKER_REQUEST); 
       } catch(GooglePlayServicesRepairableException | GooglePlayServicesNotAvailableException e){ 
        Log.v("MOOSECA","Problemi nell'avvio di place picker di google"); 
       } 
      } 
     }); 

    } 


    /** 
    * Manipulates the map once available. 
    * This callback is triggered when the map is ready to be used. 
    * This is where we can add markers or lines, add listeners or move the camera. In this case, 
    * we just add a marker near Sydney, Australia. 
    * If Google Play services is not installed on the device, the user will be prompted to install 
    * it inside the SupportMapFragment. This method will only be triggered once the user has 
    * installed Google Play services and returned to the app. 
    */ 
    @Override 
    public void onMapReady(GoogleMap googleMap) { 
     mMap = googleMap; 

     // Add a marker in Sydney and move the camera 
     LatLng sydney = new LatLng(-34, 151); 
     mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney")); 
     mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney)); 
    } 
} 

ここに私のGradleファイル:

apply plugin: 'com.android.application' 

android { 
    compileSdkVersion 25 
    buildToolsVersion "25.0.1" 
    defaultConfig { 
     applicationId "com.example.lorenzo.mysimplemap" 
     minSdkVersion 16 
     targetSdkVersion 25 
     versionCode 1 
     versionName "1.0" 
     testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 
    } 
    buildTypes { 
     release { 
      minifyEnabled false 
      proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 
     } 
    } 
} 

dependencies { 
    compile fileTree(dir: 'libs', include: ['*.jar']) 
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { 
     exclude group: 'com.android.support', module: 'support-annotations' 
    }) 
    compile 'com.android.support:appcompat-v7:25.3.1' 
    compile 'com.google.android.gms:play-services-maps:10.2.4' 
    compile 'com.google.android.gms:play-services-places:10.2.4' 
    testCompile 'junit:junit:4.12' 
} 

ここでの主な活動のための私のレイアウト:任意の助けを事前に

<FrameLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_height="match_parent" 
android:layout_width="match_parent"> 

<Button 
    android:id="@+id/pick_place_button" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_gravity="top|center" 
    android:text="scegli un posto" /> 
<fragment xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:map="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/map" 
    android:name="com.google.android.gms.maps.SupportMapFragment" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context="com.example.lorenzo.mysimplemap.MapsActivity" /> 

ありがとう!

+0

あなたは、GoogleがデベロッパーコンソールページにAPIを配置可能にしましたか? –

+0

Ops、私は知らない...この手続きを私に書いてもらえますか? –

答えて

0

後藤デベロッパーコンソールページ - > API->は、プロジェクトのためにそれをGoogleがAPIを置き選択し、イネーブル

1

問題を解決しました(Muhib Piraniのおかげで) 開発者用コンソールページでGoogleプレイスAPIを有効にする必要がありました。 その後、私のアプリでプレースピッカーが動作します。

関連する問題