我是Google Maps&Stuff的新手。我正在为学位课程做我的“最终项目”,并且正面临这个问题。我正在尝试从Places Api处获得答复,我已经按照官方文档构建了URL,但是经过所有努力,我无法从google获得结果。我正在使用Retrofit2获取结果。
我通过更改半径来更改了我的网址。通过使用浏览器密钥更改API密钥(找到了一个建议这样做的链接),所有更改均未返回结果。 我在地图上的当前位置正常。帮助将不胜感激。谢谢
这是我的Retrofit API:
public interface GoogleApiService {
@Headers("Content-Type: application/json")
@GET
Call<Places> getNearByPlaces(@Url String url);
}
RetrofitClient:
public class RetrofitClient {
private static Retrofit retrofit = null;
public static Retrofit getClient(String baseUrl)
{
if (retrofit == null)
{
retrofit = new Retrofit.Builder()
.baseUrl(baseUrl)
.addConverterFactory(GsonConverterFactory.create())
.build();
}
return retrofit;
}
}
GoogleMapsClass:
public class NearbyBusStand extends FragmentActivity implements OnMapReadyCallback,
GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, LocationListener {
private static final int MY_PERMISSION_CODE = 1000;
private GoogleMap mMap;
private GoogleApiClient mGoogleApiClient;
private double latitude, longitude;
private Location mLastLocation;
private Marker mMarker;
private LocationRequest mLocationRequest;
GoogleApiService mService;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_nearby_bus_stand);
// 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);
mService = URLEndPoints.getGoogleApiService();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
checkLocationPermission();
}
BottomNavigationView bottomNavigationView = findViewById(R.id.bottom_navigation);
bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
switch (menuItem.getItemId()) {
case R.id.bus_stop:
nearByPlaces("bus_station");
break;
case R.id.market:
nearByPlaces("market");
break;
case R.id.restaurant:
nearByPlaces("restaurant");
break;
case R.id.school:
nearByPlaces("school");
break;
default:
break;
}
return true;
}
});
}
private void nearByPlaces(String placeType) {
mMap.clear();
String url = getUrl(latitude, longitude, placeType);
mService.getNearByPlaces(url)
.enqueue(new Callback<Places>() {
@Override
public void onResponse(Call<Places> call, Response<Places> response) {
if (response.isSuccessful()) {
for (int i = 0; i < response.body().getResults().length; i++) {
MarkerOptions markerOptions = new MarkerOptions();
Results googlePlaces = response.body().getResults()[i];
double lat = Double.parseDouble(googlePlaces.getGeometry().getLocation().getLat());
double lng = Double.parseDouble(googlePlaces.getGeometry().getLocation().getLng());
String placeName = googlePlaces.getName();
String vicinity = googlePlaces.getVicinity();
LatLng latLng = new LatLng(lat, lng);
markerOptions.position(latLng);
markerOptions.title(placeName);
switch (placeName) {
case "bus_station":
markerOptions.icon(BitmapDescriptorFactory.fromResource(R.drawable.bus_marker));
break;
case "market":
markerOptions.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE));
break;
case "restaurant":
markerOptions.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE));
break;
case "school":
markerOptions.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE));
break;
default:
markerOptions.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE));
break;
}
mMap.addMarker(markerOptions);
mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
mMap.animateCamera(CameraUpdateFactory.zoomTo(11));
}
}
}
@Override
public void onFailure(Call<Places> call, Throwable t) {
}
});
}
private String getUrl(double latitude, double longitude, String placeType) {
StringBuilder googlePlacesUrl = new StringBuilder("https://maps.googleapis.com/maps/api/place/nearbysearch/json?");
googlePlacesUrl.append("loaction=" + latitude + "," + longitude);
googlePlacesUrl.append("&radius=" + 10000);
googlePlacesUrl.append("&type=" + placeType);
googlePlacesUrl.append("&sensor=true");
googlePlacesUrl.append("&key=" + getResources().getString(R.string.browser_key));
Log.d("url", googlePlacesUrl.toString());
return googlePlacesUrl.toString();
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
switch (requestCode) {
case MY_PERMISSION_CODE: {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
== PackageManager.PERMISSION_GRANTED) {
if (mGoogleApiClient == null)
buildGoogleApiClient();
mMap.setMyLocationEnabled(true);
}
} else
Toast.makeText(this, "Permission Denied", Toast.LENGTH_SHORT).show();
}
break;
}
}
private boolean checkLocationPermission() {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED && ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION)
!= PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.ACCESS_FINE_LOCATION) &&
ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.ACCESS_COARSE_LOCATION))
ActivityCompat.requestPermissions(this, new String[]{
Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION
}, MY_PERMISSION_CODE);
else
ActivityCompat.requestPermissions(this, new String[]{
Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION
}, MY_PERMISSION_CODE);
return false;
} else {
return true;
}
}
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED
&& ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
buildGoogleApiClient();
mMap.setMyLocationEnabled(true);
}
} else {
buildGoogleApiClient();
mMap.setMyLocationEnabled(true);
}
}
private synchronized void buildGoogleApiClient() {
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
mGoogleApiClient.connect();
}
@Override
public void onConnected(@Nullable Bundle bundle) {
mLocationRequest = new LocationRequest();
mLocationRequest.setInterval(1000);
mLocationRequest.setFastestInterval(1000);
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
== PackageManager.PERMISSION_GRANTED) {
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
}
}
@Override
public void onConnectionSuspended(int i) {
mGoogleApiClient.connect();
}
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
}
@Override
public void onLocationChanged(Location location) {
mLastLocation = location;
if (mMarker != null)
mMarker.remove();
latitude = location.getLatitude();
longitude = location.getLongitude();
LatLng latLng = new LatLng(latitude, longitude);
MarkerOptions markerOptions = new MarkerOptions()
.position(latLng)
.title("Current Position")
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN));
mMarker = mMap.addMarker(markerOptions);
mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
mMap.animateCamera(CameraUpdateFactory.zoomTo(11));
if (mGoogleApiClient != null)
LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);
}
}
生成的网址:
https://maps.googleapis.com/maps/api/place/nearbysearch/json?loaction=32.2408243,74.1493718&radius=10000&type=hospital&sensor=true&key=MYAPIKEY
浏览器响应:
带有类的项目目录:
答案 0 :(得分:0)
在您生成的URL中,有一个错字...就是这样!
https // maps.googleapis.com / maps / api / place / nearbysearch / json?位置 = 32.2408243,74.1493718&radius = 10000&type = hospital&sensor = true&key = MYAPIKEY
将位置更改为位置。