【发布时间】:2017-05-23 09:51:46
【问题描述】:
我正在尝试创建一个具有两个选项卡的应用程序。 第一个选项卡包含我的所有联系人及其工作正常。第二个选项卡应包含一个地图,显示所有已保存位置的联系人的标记。
谷歌地图正确呈现,没有任何错误,但是如何在其中放置标记?
这是 Viewpager 片段:
package com.example.contacts_mapapp;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
public class ContactMap extends Fragment implements OnMapReadyCallback{
// 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;
// Google Map
private SupportMapFragment googleMap;
View view;
public ContactMap() {
// 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 ContactMap.
*/
// TODO: Rename and change types and number of parameters
public static ContactMap newInstance(String param1, String param2) {
ContactMap fragment = new ContactMap();
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_contact_map, container, false);
try {
// Loading map
FragmentManager fragmentManager = getChildFragmentManager();
googleMap = ((SupportMapFragment) fragmentManager.findFragmentById(R.id.map));
googleMap.getMapAsync((OnMapReadyCallback) getActivity());
// check if map is created successfully or not
if (googleMap == null) {
googleMap = SupportMapFragment.newInstance();
fragmentManager.beginTransaction().replace(R.id.map,googleMap).commit();
Toast.makeText(getActivity().getApplicationContext(),
"Sorry! unable to create maps", Toast.LENGTH_SHORT)
.show();
}
} catch (Exception e) {
e.printStackTrace();
}
return view;
}
@Override
public void onMapReady(GoogleMap googleMap) {
LatLng sydney = new LatLng(-33.852, 151.211);
googleMap.addMarker(new MarkerOptions().position(sydney)
.title("Marker in Sydney"));
googleMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
}
}
XML 文件:
<RelativeLayout 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="com.example.contacts_mapapp.ContactMap">
<!-- TODO: Update blank fragment layout -->
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:layout="@layout/fragment_contact_map"/>
</RelativeLayout>
【问题讨论】:
-
您已经在
onMapReady中添加标记,有什么问题? -
我还是看不到标记
标签: android google-maps android-viewpager maps fragment