Я немного запутался ... Не могу найти что-либо в интернете.
Я хочу показать карту Google во фрагменте вкладки.Работает хорошо.Но как я могу установить текущую долготу и широту для фрагмента?
Вот XML:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".TabMap"
android:background="#DBDBDB">
<com.google.android.gms.maps.MapView
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="60dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="10dp">
</com.google.android.gms.maps.MapView>
Фрагмент карты
public class TabMap extends Fragment implements OnMapReadyCallback{
GoogleMap mGoogleMap ;
MapView mMapView;
View mView;
Location location = new Location();
public TabMap() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
mView = inflater.inflate(R.layout.fragment_tab_map, container, false);
return mView;
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
mMapView = (MapView) mView.findViewById(R.id.map);
if(mMapView != null) {
mMapView.onCreate(null);
mMapView.onResume();
mMapView.getMapAsync(this);
}
}
@Override
public void onMapReady(GoogleMap googleMap) {
MapsInitializer.initialize(getContext());
mGoogleMap = googleMap;
googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
googleMap.addMarker(new MarkerOptions().position(new LatLng(51.1657, 10.4515)).title("Germany"));
CameraPosition de = CameraPosition.builder().target(new LatLng(51.1657, 10.4515)).zoom(6).bearing(0).build();
googleMap.moveCamera(CameraUpdateFactory.newCameraPosition(de));
}
}
location.class
public class Location extends AppCompatActivity {
private static final int REQUEST_CODE = 1000;
TextView txtLocation;
Button btnLocation, btnStop;
AutoCompleteTextView gMap;
FusedLocationProviderClient fusedLocationProviderClient;
LocationRequest locationRequest;
LocationCallback locationCallback;
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode) {
case REQUEST_CODE: {
if (grantResults.length > 0) {
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
} else if (grantResults[0] == PackageManager.PERMISSION_DENIED) {
}
}
}
}
}
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.locations);
Button button = (Button) findViewById(R.id.button3);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(v.getContext(), MainActivity.class);
startActivityForResult(intent, 0);
}
});
txtLocation = (TextView) findViewById(R.id.txtLocation);
btnLocation = (Button) findViewById(R.id.btnLocation);
btnStop = (Button) findViewById(R.id.btnStop);
if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.ACCESS_FINE_LOCATION))
{
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, REQUEST_CODE);
} else {
buildLocationRequest();
buildLocationCallBack();
fusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(this);
btnLocation.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (ActivityCompat.checkSelfPermission(Location.this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
&& ActivityCompat.checkSelfPermission(Location.this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(Location.this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, REQUEST_CODE);
return;
}
fusedLocationProviderClient.requestLocationUpdates(locationRequest, locationCallback, Looper.myLooper());
btnLocation.setEnabled(!btnLocation.isEnabled());
btnStop.setEnabled(!btnStop.isEnabled());
}
});
btnStop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (ActivityCompat.checkSelfPermission(Location.this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
&& ActivityCompat.checkSelfPermission(Location.this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(Location.this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, REQUEST_CODE);
return;
}
fusedLocationProviderClient.removeLocationUpdates(locationCallback);
btnLocation.setEnabled(!btnLocation.isEnabled());
btnStop.setEnabled(!btnStop.isEnabled());
}
});
}
}
public void buildLocationCallBack() {
locationCallback = new LocationCallback(){
@Override
public void onLocationResult(LocationResult locationResult) {
for(android.location.Location location:locationResult.getLocations())
txtLocation.setText(String.valueOf(location.getLatitude())
+ "/" +
String.valueOf(location.getLongitude()));
}
};
}
private void buildLocationRequest() {
locationRequest = new LocationRequest();
locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
locationRequest.setInterval(5000);
locationRequest.setFastestInterval(3000);
locationRequest.setSmallestDisplacement(10);
}
}
Итак.У меня есть панель навигации с возможностью добавления местоположений (location.class). Она открывается, и при нажатии кнопки «текущее местоположение» она показывает широту и долготу в TextView.
Но как я получу эту информацию в метод TabMap.class, метод googleMap.addMarker?
Заранее спасибо!