3
Hello, I am very layy in Java and need to use a 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 be the right way to do?
Note: I need to do this to compare several "points" within a "for", I think this is the best way...
--- EDIT ---
It returns me type errors:
- Unkown class: points
- Missing method body, or declare Abstract
--EDIT2-- (code, as Maniero 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 am using the 'Sphericalutil.computeDistanceBetween' function to calculate the distance from point 1 with the user’s position. This works normally, so... Now I needed a 'for' to calculate the distance of each point I list above in relation to the user’s position and then return me to the shortest distance... For this, I think I would need to put all the dots in a single array and test element by array element, but I don’t know how to declare the array up there...
And where’s the code? You can [Dit] the question and put it to us to analyze
– Maniero
Code added (:
– T. Lima
With that alone you can’t tell, or maybe the mistake is just having this :)
– Maniero
Basically only this in my map class, I just stopped listing the Imports and the package hehe
– T. Lima
I don’t understand your comparison to the C language...
– eightShirt
It is that in the C language, I can declare an array, for example, of type int with 11 elements (starts counting from 0)... It would look something like: int array[10]; E, right after, I could access position element 5 using: array[5];
– T. Lima