So I am a student and I am really new to programming. We are making a game for our android phones and I need some help, I want a menu (which I can make myself) and there you as a user can choose a specific radius around you, lets say 1km radius around you, then I want randomized markers placed around you within that radius.
I have the map, I have some markers put on the location in school, I have so when you walk on them you "pick them up" and get one point. Now I want to make randomized markers so you can play the game where ever. Any ideas?
Here is my code:
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback,
GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener,
LocationListener {
GoogleMap mGoogleMap;
LocationRequest mLocationRequest;
GoogleApiClient mGoogleApiClient;
Location mLastLocation;
public static final int PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION = 1;
private static final int DEFAULT_ZOOM = 18;
private List<MarkerOptions> targets;
private MarkerOptions myMarker;
private static final String TAG = MapsActivity.class.getSimpleName();
private int score = 0;
private TextView scoreTV;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
scoreTV = (TextView) findViewById(R.id.scoreTV);
scoreTV.setText("Score: 0");
// 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);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
}
@Override
public void onMapReady(GoogleMap googleMap)
{
mGoogleMap = googleMap;
UiSettings settings = mGoogleMap.getUiSettings();
settings.setZoomControlsEnabled(true);
settings.setCompassEnabled(true);
setupTargets();
addTargetsToMap();
try {
// Customise the styling of the base map using a JSON object defined
// in a raw resource file.
boolean success = googleMap.setMapStyle(
MapStyleOptions.loadRawResourceStyle(
this, R.raw.style_json));
if (!success) {
Log.e(TAG, "Style parsing failed.");
}
} catch (Resources.NotFoundException e) {
Log.e(TAG, "Can't find style. Error: ", e);
}
//Initialize Google Play Services
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (ContextCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_FINE_LOCATION)
== PackageManager.PERMISSION_GRANTED) {
//Location Permission already granted
buildGoogleApiClient();
//mGoogleMap.setMyLocationEnabled(true);
} else {
//Request Location Permission
checkLocationPermission();
}
}
else {
buildGoogleApiClient();
//mGoogleMap.setMyLocationEnabled(true);
}
}
protected synchronized void buildGoogleApiClient() {
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
mGoogleApiClient.connect();
}
@Override
public void onConnected(Bundle bundle) {
startLocationUpdates();
}
private void startLocationUpdates() {
mLocationRequest = new LocationRequest();
mLocationRequest.setInterval(5000);
mLocationRequest.setFastestInterval(3000);
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 onLocationChanged(Location location)
{
Log.e("Location: ", location.toString());
mLastLocation = location;
Iterator<MarkerOptions> iter = targets.iterator();
while (iter.hasNext()) {
MarkerOptions target = iter.next();
Location targetLocation = new Location(LocationManager.GPS_PROVIDER);
targetLocation.setLatitude(target.getPosition().latitude);
targetLocation.setLongitude(target.getPosition().longitude);
float distance = location.distanceTo(targetLocation);
if (distance < 10.0f) {
Toast.makeText(this, "Target aquired, plus one point!", Toast.LENGTH_SHORT).show();
score++;
scoreTV.setText("Score: " + score);
iter.remove();
}
}
mGoogleMap.clear();
addTargetsToMap();
//Place current location marker
LatLng position = new LatLng(location.getLatitude(), location.getLongitude());
myMarker = new MarkerOptions();
myMarker.position(position);
myMarker.title("Me");
myMarker.icon(BitmapDescriptorFactory.fromResource(R.drawable.dansa));
mGoogleMap.addMarker(myMarker);
//move map camera
mGoogleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(position, DEFAULT_ZOOM));
}
private void checkLocationPermission() {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED) {
// Should we show an explanation?
if (ActivityCompat.shouldShowRequestPermissionRationale(this,
Manifest.permission.ACCESS_FINE_LOCATION)) {
// Show an explanation to the user *asynchronously* -- don't block
// this thread waiting for the user's response! After the user
// sees the explanation, try again to request the permission.
new AlertDialog.Builder(this)
.setTitle("Location Permission Needed")
.setMessage("This app needs the Location permission, please accept to use location functionality")
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
//Prompt the user once explanation has been shown
ActivityCompat.requestPermissions(MapsActivity.this,
new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION);
}
})
.create()
.show();
} else {
// No explanation needed, we can request the permission.
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION);
}
}
}
@Override
public void onRequestPermissionsResult(int requestCode,
@NonNull String permissions[], @NonNull int[] grantResults) {
switch (requestCode) {
case PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION: {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// permission was granted, yay! Do the
// location-related task you need to do.
if (ContextCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_FINE_LOCATION)
== PackageManager.PERMISSION_GRANTED) {
if (mGoogleApiClient == null) {
buildGoogleApiClient();
}
mGoogleMap.setMyLocationEnabled(true);
}
} else {
// permission denied, boo! Disable the
// functionality that depends on this permission.
Toast.makeText(this, "permission denied", Toast.LENGTH_LONG).show();
}
}
// other 'case' lines to check for other
// permissions this app might request
}
}
//=============== START TARGETS ====================
private void setupTargets() {
targets = new ArrayList<MarkerOptions>();
MarkerOptions target = new MarkerOptions();
target.position(new LatLng(58.393813, 15.564835)).title("Target 1");
targets.add(target);
target = new MarkerOptions();
target.position(new LatLng(58.394039, 15.564811)).title("Target 2");
targets.add(target);
target = new MarkerOptions();
target.position(new LatLng(58.394244, 15.565093)).title("Target 3");
targets.add(target);
}
private void addTargetsToMap() {
for(MarkerOptions target : targets){
mGoogleMap.addMarker(target);
}
}
//=============== END TARGETS ====================
//=============== START LIFECYCLE ====================
@Override
public void onPause() {
super.onPause();
//stop location updates when Activity is no longer active
if (mGoogleApiClient != null) {
LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);
}
}
@Override
protected void onResume() {
super.onResume();
if(mGoogleApiClient!=null) {
if(mGoogleApiClient.isConnected())
startLocationUpdates();
}
}
//=============== END LIFECYCLE ====================
//====================== NOT USED =======================
@Override
public void onConnectionSuspended(int i) {}
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {}
}