5

Android123的Google Places API中的自動填充小部件服務未按預期工作。我已將該片段包含在我的xml頁面中,並將偵聽器活動添加到了我的onCreate()
然後我開始執行自動完成片段,我點擊我想要搜索的單詞的第一個字母,然後突然顯示onError()得到執行並且它關閉。Android中的PlaceAutoCompleteFragment在輸入第一個字母后沒有顯示建議並關閉

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <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.akhil.maplocation.MapsActivity" /> 
    <fragment 
     android:id="@+id/place_autocomplete_fragment" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:name="com.google.android.gms.location.places.ui.PlaceAutocompleteFragment" 
     /> 
</RelativeLayout> 

這在MainActivity是:

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback { 

    private GoogleMap mMap; 
    LatLng fromadd; 
    @Override 
     protected void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.activity_maps); 
      PlaceAutocompleteFragment autocompleteFragment = (PlaceAutocompleteFragment) 
        getFragmentManager().findFragmentById(R.id.place_autocomplete_fragment); 


      autocompleteFragment.setOnPlaceSelectedListener(new PlaceSelectionListener() { 
       @Override 
       public void onPlaceSelected(Place place) { 
        // TODO: Get info about the selected place. 
        fromadd = place.getLatLng(); 
        double lat = fromadd.latitude; 
        double lng = fromadd.longitude;   
        gotoLocation(lat, lng, 15); 
       } 

       @Override 
       public void onError(Status status) { 
        // TODO: Handle the error. 
        Toast.makeText(getBaseContext(),"failure",Toast.LENGTH_LONG).show(); 
       } 
      }); 
mMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() { 
      @Override 
      public void onMapClick(LatLng latLng) { 
       MarkerOptions markerOptions = new MarkerOptions(); 
       markerOptions.position(latLng); 
       fromadd = latLng; 
       markerOptions.title(latLng.latitude + ":" + latLng.longitude + " " + latLng.toString()); 
       mMap.clear(); 
       mMap.animateCamera(CameraUpdateFactory.newLatLng(latLng)); 
       mMap.addMarker(markerOptions); 
      } 
     }); 


     mapFragment.getMapAsync(this); 
} 

@Override 
    public void onMapReady(GoogleMap googleMap) { 
     mMap = googleMap; 

     if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {     
      return; 
     } 
     mMap.setMyLocationEnabled(true); 

}

+0

什麼是錯誤從placeautocmplete記錄錯誤,並在這裏發表日誌 –

+0

狀態{=的StatusCode PLACES_API_ACCESS_NOT_CONFIGURED,分辨率= NULL} – Akhil

+0

這可能幫助您 - HTTP:// stackoverflow.com/questions/30434238/place-picker-automatically-close-after-launch 如果這不會通知我。我會發佈一個完整的答案。 –

回答

7

你可能會錯過的API密鑰,這是需要使用谷歌API的地方的。檢查出this link。當你在鏈接指定的所有事情,也將此代碼添加到您的AndroidManifest

<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="YOUR_KEY" /> 
+0

我確實有地圖的API KEY代碼。我應該創建一個新的API密鑰還是可以使用相同的密鑰? – Akhil

+0

我得到這個時,我登錄:狀態{statusCode = PLACES_API_ACCESS_NOT_CONFIGURED,分辨率= null} – Akhil

+1

我認爲API密鑰應該沒問題。但請確保您在開發人員控制檯中啓用您的應用程序。您必須提供您的應用程序包名稱和應用程序的SHA-1。有關在開發人員控制檯中啓用應用程序和[此鏈接](http://stackoverflow.com/questions/15727912/sha-1-fingerprint-of-keystore-certificate)的信息,請參閱答案中的鏈接以獲取有關如何獲取SHA- 1。 –

0

MMAP需要使用mMap.setOnMapClickListener(....之前要initialied;在gotoLocation(緯度,經度,15) ?具有如下看:

private void gotoLocation(double lat, double lng, int i) { 
       // TODO Auto-generated method stub 

       Marker myMarker = map.addMarker(new MarkerOptions() 
       .icon(BitmapDescriptorFactory.fromResource(R.drawable.home)) 
       .position(p1) 
       .snippet("Lat:" + lat + "Lng:" + lng) 
       .title("HOME")); 

       map = ((MapFragment) getFragmentManager() 
         .findFragmentById(R.id.map2)).getMap(); 
       map.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(lat, lng), i));     
      }  
相關問題