我正在开发一个简单的 map 应用程序,我使用navigationDrawer
将我的 map 放在抽屉
的第一个选项中。现在的问题是当我将任何按钮放在 map 的同一 fragment
或同一布局中,它都不会执行我想要的操作!
例如:当我单击按钮
时,我需要放大我的位置。
我尝试了在网站上找到的一些解决方案,但没有成功..
xml 代码:
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment android:layout_height="match_parent"
android:layout_width="405dp" android:id="@+id/mapfragment"
class="com.google.android.gms.maps.MapFragment"
android:layout_marginEnd="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginRight="8dp"
/>
<RelativeLayout android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button
android:id="@+id/bt1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="46dp"
android:text="Button"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
android:onClick="onClick"
/>
</RelativeLayout>
</android.support.constraint.ConstraintLayout>
java代码:
public class maptest extends Fragment implements OnMapReadyCallback,View.OnClickListener {
GoogleMap mp;
@Nullable
View v1;
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
Button bt2=(Button)v1.findViewById(R.id.bt1);
bt2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(getActivity(), "ggg", Toast.LENGTH_SHORT).show();
}
});
return inflater.inflate(R.layout.newmapfragment, container,false);
}
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1)
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
MapFragment fragment= (MapFragment) getChildFragmentManager().findFragmentById(R.id.mapfragment);
fragment.getMapAsync(this);
}
@Override
public void onMapReady(GoogleMap mp) {
// Add a marker in Sydney, Australia,
// and move the map's camera to the same location.
LatLng sydney = new LatLng(31.163937, 35.710373);
mp.addMarker(new MarkerOptions().position(sydney)
.title("Marker in Sydney"));
mp.moveCamera(CameraUpdateFactory.newLatLng(sydney));
mp.addMarker(new MarkerOptions().position(new
LatLng(31.163937,35.710373)).title("mymap")).showInfoWindow();
mp.animateCamera(CameraUpdateFactory.newLatLngZoom(new
LatLng(31.163937,35.710373),16));
}
@Override
public void onClick(View view) {
Button b = (Button) view.findViewById(R.id.bt1);
b.setOnClickListener(this);
LatLng sydney = new LatLng(31.163937, 35.710373);
mp.addMarker(new MarkerOptions().position(sydney)
.title("Marker in Sydney"));
mp.moveCamera(CameraUpdateFactory.newLatLng(sydney));
mp.addMarker(new MarkerOptions().position(new
LatLng(31.163937,35.710373)).title("mymap")).showInfoWindow();
mp.animateCamera(CameraUpdateFactory.newLatLngZoom(new
LatLng(31.163937,35.710373),16));
}
}
任何帮助将不胜感激..
最佳答案
您可能会在 fragment 的 onCreateView 中忘记:
vi=inflater.inflate(your_fragment_layout_xml,container,false);
关于android - 在 map fragment 上添加按钮以在 map 上执行操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47591434/