将当前位置添加到 fragment.class
Add current location to a fragment.class
我有点困惑...在互联网上找不到任何东西。
我想在选项卡片段中显示 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)
它打开并点击 "current location" 按钮,它在 TextView 中显示纬度和经度。
但是我如何将这些信息提供给TabMap.class、googleMap.addMarker方法呢?
提前致谢!
将此代码放入 OnMapReady 方法中:(它会在蓝点中显示您的位置。)
@Override
public void onMapReady(GoogleMap googleMap) {
if (mMap != null) {
return;
}
mMap = googleMap;
if (mMap == null) {
return;
}
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
mMap.setMyLocationEnabled(true);
mMap.setOnMyLocationChangeListener(new mMap.OnMyLocationChangeListener() {
@Override
public void onMyLocationChange(Location location) {
if(location!=null){
mMap.addMarker(new MarkerOptions().position(new LatLng(location.getLatitude(), location.getLongitude())).title("Germany"));
CameraPosition de = CameraPosition.builder().target(new LatLng(location.getLatitude(), location.getLongitude())).zoom(6).bearing(0).build();
mMap.moveCamera(CameraUpdateFactory.newCameraPosition(de));
}
}
});
}
在 onMapReady
方法中替换此代码
@Override
public void onMapReady(GoogleMap googleMap) {
MapsInitializer.initialize(getContext());
mGoogleMap = googleMap;
googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
mGoogleMap.setMyLocationEnabled(true);
mGoogleMap.setOnMyLocationChangeListener(new GoogleMap.OnMyLocationChangeListener() {
@Override
public void onMyLocationChange(Location location) {
if(location!=null){
mGoogleMap.addMarker(new MarkerOptions().position(new LatLng(location.getLatitude(), location.getLongitude())).title("Germany"));
CameraPosition de = CameraPosition.builder().target(new LatLng(location.getLatitude(), location.getLongitude())).zoom(6).bearing(0).build();
mGoogleMap.moveCamera(CameraUpdateFactory.newCameraPosition(de));
}
}
});
}
你的问题是你无法让你的 activity 和片段进行通信。实现此目的的一种简单方法是使用库 EventBus :
https://github.com/greenrobot/EventBus
请阅读文档。
我有点困惑...在互联网上找不到任何东西。
我想在选项卡片段中显示 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) 它打开并点击 "current location" 按钮,它在 TextView 中显示纬度和经度。
但是我如何将这些信息提供给TabMap.class、googleMap.addMarker方法呢?
提前致谢!
将此代码放入 OnMapReady 方法中:(它会在蓝点中显示您的位置。)
@Override
public void onMapReady(GoogleMap googleMap) {
if (mMap != null) {
return;
}
mMap = googleMap;
if (mMap == null) {
return;
}
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
mMap.setMyLocationEnabled(true);
mMap.setOnMyLocationChangeListener(new mMap.OnMyLocationChangeListener() {
@Override
public void onMyLocationChange(Location location) {
if(location!=null){
mMap.addMarker(new MarkerOptions().position(new LatLng(location.getLatitude(), location.getLongitude())).title("Germany"));
CameraPosition de = CameraPosition.builder().target(new LatLng(location.getLatitude(), location.getLongitude())).zoom(6).bearing(0).build();
mMap.moveCamera(CameraUpdateFactory.newCameraPosition(de));
}
}
});
}
在 onMapReady
方法中替换此代码
@Override
public void onMapReady(GoogleMap googleMap) {
MapsInitializer.initialize(getContext());
mGoogleMap = googleMap;
googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
mGoogleMap.setMyLocationEnabled(true);
mGoogleMap.setOnMyLocationChangeListener(new GoogleMap.OnMyLocationChangeListener() {
@Override
public void onMyLocationChange(Location location) {
if(location!=null){
mGoogleMap.addMarker(new MarkerOptions().position(new LatLng(location.getLatitude(), location.getLongitude())).title("Germany"));
CameraPosition de = CameraPosition.builder().target(new LatLng(location.getLatitude(), location.getLongitude())).zoom(6).bearing(0).build();
mGoogleMap.moveCamera(CameraUpdateFactory.newCameraPosition(de));
}
}
});
}
你的问题是你无法让你的 activity 和片段进行通信。实现此目的的一种简单方法是使用库 EventBus : https://github.com/greenrobot/EventBus
请阅读文档。