gpt4 book ai didi

Android - 如何向此 fragment 添加 Google map ?

转载 作者:行者123 更新时间:2023-11-29 19:48:11 24 4
gpt4 key购买 nike

我想向这个 fragment 添加一个简单的 GoogleMap。

package se.adrianhansson.runpoint;

import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import com.google.android.gms.common.GooglePlayServicesNotAvailableException;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapView;
import com.google.android.gms.maps.MapsInitializer;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.CameraPosition;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;


/**
* A simple {@link Fragment} subclass.
* Activities that contain this fragment must implement the
* {@link HomeFragment.OnFragmentInteractionListener} interface
* to handle interaction events.
* Use the {@link HomeFragment#newInstance} factory method to
* create an instance of this fragment.
*/
public class HomeFragment extends Fragment implements View.OnClickListener {
// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";

// TODO: Rename and change types of parameters
private String mParam1;
private String mParam2;

private Button buttonStart;
private View view;

private OnFragmentInteractionListener mListener;

public HomeFragment() {
// Required empty public constructor
}

/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
* @param param1 Parameter 1.
* @param param2 Parameter 2.
* @return A new instance of fragment HomeFragment.
*/
// TODO: Rename and change types and number of parameters
public static HomeFragment newInstance(String param1, String param2) {
HomeFragment fragment = new HomeFragment();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}


@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
view = inflater.inflate(R.layout.fragment_home, container, false);
buttonStart = (Button) view.findViewById(R.id.buttonHomeStart);
return view;
}

// TODO: Rename method, update argument and hook method into UI event
//public void onButtonPressed(Uri uri) {
public void onButtonPressed(String string){
if (mListener != null) {
//mListener.onFragmentInteraction(uri);
mListener.onFragmentInteractionHome(string);
}
}

@Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof OnFragmentInteractionListener) {
mListener = (OnFragmentInteractionListener) context;
} else {
throw new RuntimeException(context.toString()
+ " must implement OnFragmentInteractionListener");
}
}

@Override
public void onDetach() {
super.onDetach();
mListener = null;
}

//no more use??
@Override
public void onClick(View v) {
mListener.onFragmentInteractionHome("Clicked");
// switch (v.getId()) {
// case R.id.buttonHomeStart:
// mListener.onFragmentInteractionHome("Clicked start");
// break;
// }
}

/**
* This interface must be implemented by activities that contain this
* fragment to allow an interaction in this fragment to be communicated
* to the activity and potentially other fragments contained in that
* activity.
* <p/>
* See the Android Training lesson <a href=
* "http://developer.android.com/training/basics/fragments/communicating.html"
* >Communicating with Other Fragments</a> for more information.
*/
public interface OnFragmentInteractionListener {
// TODO: Update argument type and name
void onFragmentInteractionHome(String string);
}
}

这是我的 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="se.adrianhansson.runpoint.HomeFragment">

<!-- TODO: Update blank fragment layout -->

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/hello_blank_fragment" />

<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="New Button"
android:id="@+id/buttonHomeStart"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginRight="10dp"
android:layout_marginLeft="10dp"
android:backgroundTint="@color/colorPrimary"
android:nestedScrollingEnabled="false" />
</RelativeLayout>

</FrameLayout>

我已经尝试了一百万种不同的方法。我遵循了教程,试图复制和修改我自己的代码(我从 Activity 移植过来的代码,但显然这与我的 fragment 不同)。我遇到了各种错误,无论我尝试什么,我的应用程序总是在我所有的尝试中崩溃。所以这里附上了我的 fragment 的“干净的石板”。现在..如何向其中添加谷歌地图?

所以我希望这里有人能帮助我。如何在 fragment 中添加简单的 Google map 作为“背景”?

最佳答案

如果您已经激活了 Maps API 并从开发者控制台获取了一个 key ,请将 MapView 添加到您当前的 fragment 布局中:

<com.google.android.gms.maps.MapView
android:id="@+id/myMapView"
android:layout_width="match_parent"
android:layout_height="match_parent"/>

然后确保你的 Fragment 实现了这个回调:

implements OnMapReadyCallback

声明一个全局的 private MapView mapView;

如果你需要一个全局谷歌地图private GoogleMap mMap;

然后在 fragment 的 onCreateView 中:
mapView = (MapView) view.findViewById(R.id.myMapView);
mapView.onCreate(savedInstanceState);
mapView.getMapAsync(this);

并实现回调方法:

@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
final LatLng PERTH = new LatLng(-31.90, 115.86); // Perth, Australia
Marker mymarker = googleMap.addMarker(new MarkerOptions().position(PERTH).title("Perth,Australia"));
mymarker.showInfoWindow();
googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(PERTH, 16));
googleMap.getUiSettings().setZoomControlsEnabled(true);
googleMap.getUiSettings().setAllGesturesEnabled(true);
googleMap.getUiSettings().setCompassEnabled(true);

}

关于Android - 如何向此 fragment 添加 Google map ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37399972/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com