我想在此片段中添加一个简单的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移植,但显然这并没有工作相同的i片段)。我遇到了各种各样的错误,无论我尝试什么,我的应用总是在我的所有尝试中崩溃。 所以这里贴了一块干净的石板&#34;我的片段。现在..如何添加谷歌地图?
所以我希望有人可以帮助我。如何添加简单的Google地图作为&#34;背景&#34;在我的片段中?
答案 0 :(得分:2)
如果您已激活Maps API并从Developer控制台获取密钥,请将MapView添加到当前的片段布局中:
<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;
以及全球GoogleMap(如果您需要一个private GoogleMap mMap;
然后在你片段的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);
}