Hello, I am very lay in Java and I need to use one variable storing several others. How can I do this? I have the following:
LatLng ponto1 = new LatLng(-19.924312,-43.931762);
LatLng ponto2 = new LatLng(-18.851388,-41.946910);
I'm used to C, where I could do something like:
LatLng pontos[2];
LatLng pontos[1] = new LatLng(-19.924312,-43.931762);
LatLng pontos[2] = new LatLng(-18.851388,-41.946910);
But when I try this in Android Studio, it returns me syntax errors ... How would the correct way to do it?
Note: I need to do this to compare several "points" inside a "for", I think this is the best way ...
--- EDIT ---
It returns me errors of the type:
- Unkown class: points
- Missing method body, or declare abstract
- EDIT2-- (code, as bigown requested)
public class Mapa extends FragmentActivity implements OnMapReadyCallback {
private GoogleMap mMap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mapa);
// 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);
}
LatLng ponto1 = new LatLng(-19.924312,-43.931762);
LatLng ponto2 = new LatLng(-18.851388,-41.946910);
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
if (ActivityCompat.checkSelfPermission(Mapa.this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(Mapa.this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(Mapa.this,new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION}, 1);
}else{
if(!mMap.isMyLocationEnabled())
mMap.setMyLocationEnabled(true);
LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
Location myLocation = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (myLocation == null) {
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_COARSE);
String provider = lm.getBestProvider(criteria, true);
myLocation = lm.getLastKnownLocation(provider);
}
if(myLocation!=null){
LatLng userLocation = new LatLng(myLocation.getLatitude(), myLocation.getLongitude());
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(userLocation, 14), 1500, null);
double distancia = SphericalUtil.computeDistanceBetween(ponto1, userLocation);
double valorLatitude = myLocation.getLatitude();
double valorLongitude = myLocation.getLongitude();
/*String stringLongitude = Double.toString(valorLongitude);
String stringLatitude = Double.toString(valorLatitude);*/
String stringDistancia = Double.toString(distancia);
TextView textLatitude = (TextView) findViewById(R.id.textLatitude);
textLatitude.setText(stringDistancia);
/*TextView textLongitude = (TextView) findViewById(R.id.textLongitude);
textLongitude.setText(stringLongitude);*/
}
}
}
I'm using the 'SphericalUtil.computeDistanceBetween' function to calculate the distance from point1 to the user's position. This works normally ... Now I needed a 'for' to calculate the distance from each point I listed above in relation to the user's position and then return me the shortest distance ... For this, I think I would need to put all the points in a single array and test element by element of the array, but I do not know how to declare the array up there ...