Mat*_*ini 16 android google-maps google-maps-api-3
我正在尝试使用导航抽屉和地图片段制作应用程序。我正在尝试有一个自动完成片段,但是当我搜索一个地方时,我只会收到name和id属性。像这样:
I/PLACE: Place{address=null, attributions=[], id=ChIJVXealLU_xkcRja_At0z9AGY, latLng=null, name=Amsterdam, openingHours=null, phoneNumber=null, photoMetadatas=null, plusCode=null, priceLevel=null, rating=null, types=null, userRatingsTotal=null, viewport=null, websiteUri=null}
Run Code Online (Sandbox Code Playgroud)
这是我的MapsFragment
public class MapsFragment extends Fragment implements OnMapReadyCallback, LocationListener {
MapView mapView;
GoogleMap nMap;
AutocompleteSupportFragment autocompleteFragment;
PlacesClient placesClient;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
final View rootView = inflater.inflate(R.layout.activity_maps, container, false);
...
mapView = (MapView) rootView.findViewById(R.id.mapView);
mapView.onCreate(savedInstanceState);
mapView.getMapAsync(this);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
autocompleteFragment.onActivityResult(requestCode, resultCode, data);
}
...
@Override
public void onMapReady(GoogleMap googleMap) {
Log.i("DEBUG", "onMapReady");
nMap = googleMap;
Context mContext = getActivity();
if (mContext != null){
Places.initialize(mContext, "*********MY API KEY*********");
}else{
Log.i("PLACE", "ERROR");
}
placesClient = Places.createClient(getActivity());
autocompleteFragment = (AutocompleteSupportFragment)
getChildFragmentManager().findFragmentById(R.id.place_autocomplete_fragment);
autocompleteFragment.setPlaceFields(Arrays.asList(Place.Field.ID, Place.Field.NAME));
autocompleteFragment.setOnPlaceSelectedListener(new PlaceSelectionListener() {
@Override
public void onPlaceSelected(@NonNull Place place) {
Log.i("PLACE", place.toString());
// Define a Place ID.
//nMap.moveCamera(CameraUpdateFactory.newLatLng(place.getLatLng()));
//nMap.animateCamera(CameraUpdateFactory.newLatLngZoom(place.getLatLng(), 12.0f));
}
@Override
public void onError(@NonNull Status status) {
Log.i("PLACE", status.toString());
}
});
autocompleteFragment.setUserVisibleHint(true);
autocompleteFragment.setHint("Select your city");
if (ContextCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_COARSE_LOCATION)
== PackageManager.PERMISSION_GRANTED) {
nMap.setMyLocationEnabled(true);
} else {
// Show rationale and request permission.
Log.d(TAG, "error permission denied");
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的map.xml
...
<fragment
android:id="@+id/place_autocomplete_fragment"
android:name="com.google.android.libraries.places.widget.AutocompleteSupportFragment"
android:layout_width="match_parent"
android:layout_height="56dp" />
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.gms.maps.MapView
android:id="@+id/mapView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>
...
Run Code Online (Sandbox Code Playgroud)
我将我的API KEY放入了代码(查看地图片段),googles_maps_api.xml以及gradle.properties中
PLACES_API_KEY="MY API KEY"
Run Code Online (Sandbox Code Playgroud)
这是我对谷歌地图的依赖
dependencies {
...
implementation 'com.google.android.libraries.places:places:1.0.0'
}
Run Code Online (Sandbox Code Playgroud)
我按照每个步骤激活我的API KEY
我也尝试使用android限制来保护我的KEY,但没有任何变化
我的计费系统已启用。
Sha*_*nny 33
您没有在setPlaceFields中允许您想要什么。例如,我想要latlang,所以我添加了LAT_LNG。见下文。
autocompleteFragment.setPlaceFields(Arrays.asList(Place.Field.ID, Place.Field.NAME,Place.Field.LAT_LNG));
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2140 次 |
| 最近记录: |