我刚刚开始使用android studio并希望创建一个显示我当前位置的地图。我有一个" Map类"和"获取位置"类。我想要"获取位置"运行并将经度和纬度数据存储在静态变量中的类,以便我可以从" map类"中访问它们。我怎么做?我如何获得"获取位置"在map类之前运行的类?
这是我的代码:
地图类
import android.content.Intent;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
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.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
private GoogleMap mMap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
// 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);
}
/**
* Manipulates the map once available.
* This callback is triggered when the map is ready to be used.
* This is where we can add markers or lines, add listeners or move the camera. In this case,
* we just add a marker near Sydney, Australia.
* If Google Play services is not installed on the device, the user will be prompted to install
* it inside the SupportMapFragment. This method will only be triggered once the user has
* installed Google Play services and returned to the app.
*/
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
// Add a marker in Sydney and move the camera
LatLng sydney = new LatLng(GetLocation.latitude,GetLocation.longitude);
mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
}
}
GetLocation类:
public class GetLocation extends AppCompatActivity {
private Button button;
private TextView textView;
private LocationManager locationManager;
private LocationListener locationListener;
static double latitude;
static double longitude;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
locationListener = new LocationListener() {
@Override
public void onLocationChanged(Location location) {
latitude = location.getLatitude();
longitude = location.getLongitude();
Log.d("ADebugTag", "Value: " + Double.toString(latitude));
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(intent);
}
};
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
requestPermissions(new String[]{
Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION,
Manifest.permission.INTERNET
}, 10 );
}
return;
}else{
configureButton();
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
switch (requestCode){
case 10:
if (grantResults.length>0 && grantResults[0] == PackageManager.PERMISSION_GRANTED)
configureButton();
return;
}
}
private void configureButton() {
}
@Override
protected void onResume() {
super.onResume();
locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER, 5000, 0,
locationListener);
}
}
正如您所看到的,我正在尝试访问纬度和经度,但它们只是0.但是当我单独运行getLocation类时,它们会返回正确的位置。
答案 0 :(得分:0)
您需要创建函数并返回值,然后调用该函数。在“获取位置”类中,您可以执行一个函数并返回值纬度和经度。在“Map类”类中,您可以调用该函数来获取纬度和经度的值。
希望它会对你有所帮助。
答案 1 :(得分:0)
您需要一个辅助类来设置并获得所需的值:
public class GlobalVariables {
private static GlobalVariables instance = new GlobalVariables();
// Getter-Setters
public static GlobalVariables getInstance() {
return instance;
}
public static void setInstance(GlobalVariables instance) {
GlobalVariables.instance = instance;
}
private String notification_index;
private GlobalVariables() {
}
public String getValue() {
return notification_index;
}
public void setValue(String notification_index) {
this.notification_index = notification_index;
}
}