gpt4 book ai didi

android - Google Map Api 中的 PlaceAutoComplete

转载 作者:行者123 更新时间:2023-11-30 00:52:48 26 4
gpt4 key购买 nike

我正在构建一个应用程序,它使用谷歌地图 API 来显示用户的当前位置。我放置了一个 PlcaeAutoComplete fragment ,用于通过以下链接搜索特定位置 https://developers.google.com/places/android-api/autocomplete#option_1_embed_a_placeautocompletefragment_or_a_supportplaceautocompletefragment

现在的问题是,当我开始在 PlaceAutoComplete 中输入内容时,它没有任何提示并自动取消我的搜索。这是我的主要 Activity

公共(public)类 MapsActivity 扩展 FragmentActivity 实现 OnlocReceive、OnMapReadyCallback、GoogleMap.OnMapClickListener、/* LocationListener、/GoogleMap.OnMarkerClickListener/、GoogleApiClient.OnConnectionFailedListener、GoogleApiClient.ConnectionCallbacks*/{

private GoogleMap mMap;

Marker m;

public static final String TAG = MapsActivity.class.getSimpleName();

private GoogleApiClient mGoogleApiClient;

private LocationRequest mLocationRequest;

Location location;

int PLACE_AUTOCOMPLETE_REQUEST_CODE = 1;

private final int MY_PERMISSIONS_REQUEST_CODE = 1;

private final static int CONNECTION_FAILURE_RESOLUTION_REQUEST = 9000;

public GoogleApiClient getmGoogleApiClient()
{
return mGoogleApiClient;
}

boolean bound = false;

LocationUpdateService locationService;


public ServiceConnection serviceConnection;

Intent serviceIntent;

PlaceAutocompleteFragment autocompleteFragment;

AutocompleteFilter typeFilter;



@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.newlayout);
// 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);

autocompleteFragment = (PlaceAutocompleteFragment)
getFragmentManager().findFragmentById(R.id.place_autocomplete_fragment);



typeFilter = new AutocompleteFilter.Builder().setTypeFilter(AutocompleteFilter.TYPE_FILTER_ADDRESS).build();

autocompleteFragment.setFilter(typeFilter);

autocompleteFragment.setBoundsBias(new LatLngBounds(
new LatLng(-33.880490, 151.184363),
new LatLng(-33.858754, 151.229596)));



autocompleteFragment.setOnPlaceSelectedListener(new PlaceSelectionListener() {
@Override
public void onPlaceSelected(Place place) {

// TODO: Get info about the selected place.
Log.i(TAG, "Place: ------------------------>" + place.getName());


Toast.makeText(MapsActivity.this, "place:"+place.getName(), Toast.LENGTH_SHORT).show();

PendingResult<AutocompletePredictionBuffer> result =
Places.GeoDataApi.getAutocompletePredictions(mGoogleApiClient,place.getName().toString(),
null, typeFilter);


}

@Override
public void onError(Status status) {

}
});

ConnectivityManager connManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo mWifi = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);

if (!mWifi.isConnected()) {
builALertMessageNoWifi();
}


final LocationManager manager = (LocationManager) getSystemService( Context.LOCATION_SERVICE );

if ( !manager.isProviderEnabled( LocationManager.GPS_PROVIDER ) ) {
buildAlertMessageNoGps();



}

serviceConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {

locationService = ((LocationUpdateService.localBinder)service).getservice();

locationService.passClassrefrence(MapsActivity.this);



Toast.makeText(MapsActivity.this, "bound", Toast.LENGTH_SHORT).show();

}

@Override
public void onServiceDisconnected(ComponentName name) {

bound = false;

Toast.makeText(MapsActivity.this, "not_bound", Toast.LENGTH_SHORT).show();

}
};




if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED)
{

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
Toast.makeText(MapsActivity.this, "req permission", Toast.LENGTH_SHORT).show();
requestPermissions(new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION, android.Manifest.permission.ACCESS_COARSE_LOCATION}, MY_PERMISSIONS_REQUEST_CODE);

}





}
else
{
// location = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
serviceIntent = new Intent(MapsActivity.this,LocationUpdateService.class);
startService(serviceIntent);

bound= bindService(serviceIntent,serviceConnection,BIND_AUTO_CREATE);
}

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

    Toast.makeText(MapsActivity.this, "onMapReady", Toast.LENGTH_SHORT).show();

mMap.setMapType(GoogleMap.MAP_TYPE_TERRAIN);

}

private void handleNewLocation(Location location) {

Log.i(TAG, "in handle new location---------------------->.");


Log.d(TAG, location.toString());

double currentLatitude = location.getLatitude();
double currentLongitude = location.getLongitude();

Toast.makeText(MapsActivity.this, "lat:"+currentLatitude+" lang:"+currentLongitude, Toast.LENGTH_SHORT).show();

LatLng latLng = new LatLng(currentLatitude, currentLongitude);

//mMap.addMarker(new MarkerOptions().position(new LatLng(currentLatitude, currentLongitude)).title("Current Location"));
MarkerOptions options = new MarkerOptions()
.position(latLng)
.title("I am here!");
if(m!=null)
m.remove();

m = mMap.addMarker(options);
// mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(latLng, 17));

}

@Override
public void onreceive(Location l) {
handleNewLocation(l);


if(serviceIntent!=null && bound==true ) {


locationService.stopupdates();
stopService(serviceIntent);


Toast.makeText(MapsActivity.this, "stop service", Toast.LENGTH_SHORT).show();
}

}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);


if (requestCode == MY_PERMISSIONS_REQUEST_CODE) {
if (grantResults[0] == PackageManager.PERMISSION_GRANTED && grantResults[1] == PackageManager.PERMISSION_GRANTED)

{
Toast.makeText(MapsActivity.this, " granted " + grantResults[0] + "granted2" + grantResults[1], Toast.LENGTH_SHORT).show();

try {
// location = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);


Toast.makeText(MapsActivity.this, "in recieve request new location", Toast.LENGTH_SHORT).show();
serviceIntent = new Intent(MapsActivity.this,LocationUpdateService.class);
startService(serviceIntent);
bound = bindService(serviceIntent,serviceConnection,BIND_AUTO_CREATE);

} catch (SecurityException e) {

}
}


}
}

@Override
protected void onResume() {

Toast.makeText(MapsActivity.this, "on Resume", Toast.LENGTH_SHORT).show();

// stopService(serviceIntent);

super.onResume();

}

@Override
protected void onDestroy() {


if(serviceIntent!=null && bound ==true ) {
locationService.stopupdates();
stopService(serviceIntent);

// unbindService(serviceConnection);

}
super.onDestroy();

}

private void buildAlertMessageNoGps() {
final AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Your GPS seems to be disabled, do you want to enable it?")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(@SuppressWarnings("unused") final DialogInterface dialog, @SuppressWarnings("unused") final int id) {
startActivity(new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS));
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(final DialogInterface dialog, @SuppressWarnings("unused") final int id) {
dialog.cancel();
}
});
final AlertDialog alert = builder.create();
alert.show();
}

private void builALertMessageNoWifi()
{
final AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Your WIFI seeems to be disabled, do you want to enable it?")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
startActivity(new Intent(Settings.ACTION_WIFI_SETTINGS));

}
})

.setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});

final AlertDialog alert = builder.create();
alert.show();

}

最佳答案

通常,Google PlaceAutoComplete 无法正常工作是因为开发者控制台中启用了错误/不完整的 API。因此请确保您启用了正确的 API。您应该启用适用于 Android 的 Google Place API 和 Google Place API Web 服务。此外,请确保您使用的 API key 是浏览器 key 。有关地点自动完成的更多信息,请查看此 tutorial .

关于android - Google Map Api 中的 PlaceAutoComplete,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40694058/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com