I'm currently making a geoguessr replica using the street view API and maps SDK reliant on randomly generated coordinates. As one could guess, more often than not my coordinates are a location without a streetview image. My code is as follows:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SupportStreetViewPanoramaFragment streetViewPanoramaFragment = (SupportStreetViewPanoramaFragment)getSupportFragmentManager().findFragmentById(R.id.googlemapstreetview);
streetViewPanoramaFragment.getStreetViewPanoramaAsync(this);
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map_fragment);
mapFragment.getMapAsync(this);
}
@Override
public void onStreetViewPanoramaReady(StreetViewPanorama streetViewPanorama) {
Random random = new Random();
double lat = MIN_LATITUDE + (MAX_LATITUDE - MIN_LATITUDE) * random.nextDouble();
double lng = MIN_LONGITUDE + (MAX_LONGITUDE - MIN_LONGITUDE) * random.nextDouble();
randomLocation = new LatLng(lat,lng);
this.streetViewPanorama = streetViewPanorama;
this.streetViewPanorama.setPosition(randomLocation);
}
@Override
public void onMapReady(@NonNull GoogleMap googleMap) {
this.googleMap = googleMap;
MarkerOptions markerOptions = new MarkerOptions().position(randomLocation);
Marker marker = googleMap.addMarker(markerOptions);
googleMap.moveCamera(CameraUpdateFactory.newLatLng(randomLocation));
}
}
How should I go about making it so that my program keeps trying until it pulls coordinates with a street view location? Either that or maybe even rather just use the original coordinates and try to find the closest available streetview to those coordinates?