Akh*_*hil 6 android google-maps google-maps-api-3 google-places-api google-maps-android-api-2
Google Places APIAndroid版中的自动完成小部件服务未按预期工作.我已将片段包含在我的xml页面中,并且还将侦听器活动添加到我的onCreate().
然后我开始执行自动完成片段,我点击我想要搜索的单词的第一个字母,突然而不是显示已onError()执行的建议并关闭.
<?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>
Run Code Online (Sandbox Code Playgroud)
对此的主要活动是:
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);
Run Code Online (Sandbox Code Playgroud)
}
小智 8
您可能会错过使用Google Places API所需的API密钥.看看这个链接.当您执行链接中指定的所有内容时,也请将此代码添加到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" />
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6400 次 |
| 最近记录: |