Я НОВИНКА для разработки Android и Java, поэтому, пожалуйста, будьте терпеливы со мной :) Я пытаюсь получить GPS-координаты своего телефона, а затем обновлять TextView с широтой (мне просто нужна широта на данный момент). Вот то, что я до сих пор:Как обновить TextView с GPS-координатами
main.xml:
[...]
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Get my location"
android:id="@+id/button2"
android:layout_gravity="center_horizontal"
android:onClick="returnMyCoordinates" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="This is where the lat value will show up"
android:id="@+id/latitudeResult" />
MapsActivity.java:
public void returnMyCoordinates(View v, Location location){
double latiTude;
latiTude = location.getLatitude();
String lat_value;
lat_value = Double.toString(latiTude);
TextView latView = (TextView) findViewById(R.id.latitudeResult);
latView.setText(lat_value);
}
Это мое понимание того, что я не могу объединить «View V» с другими параметрами в объявлении метода. Если это так, то почему? И как еще я мог это сделать?
Вот весь файл для контекста:
package com.example.googlemapsandroidapi;
import android.content.Context;
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback,LocationListener{
private GoogleMap mMap = null;
protected LocationManager locationManager;
Double lat;
Double long1;
TextView txtLat;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void returnMyCoordinates(View v, Location location){
double latiTude;
latiTude = location.getLatitude();
String lat_value;
lat_value = Double.toString(latiTude);
TextView latView = (TextView) findViewById(R.id.latitudeResult);
latView.setText(lat_value);
}
public void showLocation(View v) {
setContentView(R.layout.activity_maps);
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(MapsActivity.this);
ActivityCompat.requestPermissions(MapsActivity.this,
new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION},
1);
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
try {
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 250000, 0, this);
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 250000, 0, this);
if(mMap!=null){
mMap.setMyLocationEnabled(true);
}
Log.e("TAG","Location Manager Called Successfully.");
}catch (SecurityException e){
e.printStackTrace();
Log.e("TAG","SecurityException:"+e.getMessage());
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
switch (requestCode) {
case 1:
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
try {
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 250000, 0, MapsActivity.this);
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 250000, 0, MapsActivity.this);
if(mMap!=null){
mMap.setMyLocationEnabled(true);
}
Log.e("TAG","Location Manager Called Successfully.");
}catch (SecurityException e){
e.printStackTrace();
Log.e("TAG","SecurityException:"+e.getMessage());
}
} else {
Toast.makeText(MapsActivity.this,"Sorry We Need All Permissions Open App Again and Allow All.",Toast.LENGTH_LONG).show();
MapsActivity.this.finish();
}
return;
}
}
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
// Add a marker in Sydney and move the camera
/*LatLng sydney = new LatLng(-34, 151);
mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));*/
}
@Override
public void onStatusChanged(String s, int i, Bundle bundle) {
}
@Override
public void onProviderEnabled(String s) {
}
@Override
public void onProviderDisabled(String s) {
}
}
Я не думаю, что вам нужно добавить 'setContentView (R.layout.activity_maps);' –