我有点困惑......在互联网上找不到任何东西。
我想在标签片段中显示谷歌地图。它运作良好。但是如何设置片段的当前经度和纬度?
这是xml:
compile 'com.android.support:appcompat-v7:25.3.1'
compile 'com.android.support:design:25.3.1'
地图片段
<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>
}
location.class
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) 它打开并点击“当前位置”按钮,它在TextView中显示纬度和经度。
但是我如何将这些信息传递给TabMap.class,googleMap.addMarker方法?
提前致谢!
答案 0 :(得分:1)
将此代码放在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));
}
}
});
}
答案 1 :(得分:1)
在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));
}
}
});
}
答案 2 :(得分:1)
您的问题是您无法进行活动和片段通信。实现此目的的一种简单方法是使用库EventBus: https://github.com/greenrobot/EventBus
请阅读文档。