1
public class MapViewFragment extends Fragment implements LocationListener
public MapViewFragment() {
// Required empty public constructor
}
public static MapViewFragment newInstance(String tabSelected) {
MapViewFragment fragment = new MapViewFragment();
Bundle args = new Bundle();
args.putString(Constants.FRAG_D, tabSelected);
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
}
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
MapsInitializer.initialize(getActivity());
if (mapView != null) {
mapView.onCreate(savedInstanceState);
}
initializeMap();
}
private void initializeMap() {
if (googleMap == null && mapsSupported) {
mapView = (MapView) getActivity().findViewById(R.id.map);
googleMap = mapView.getMap();
//setup markers etc...
}
}
@Override
public void onResume() {
super.onResume();
Marker m1,m2,m3;
m1 = googleMap.addMarker(new MarkerOptions()
.position(new LatLng(38.609556, -1.139637))
.anchor(0.5f, 0.5f)
.title("Title1")
.snippet("Snippet1"));
m2 = googleMap.addMarker(new MarkerOptions()
.position(new LatLng(40.4272414,-3.7020037))
.anchor(0.5f, 0.5f)
.title("Title2")
.snippet("Snippet2"));
mapView.onResume();
initializeMap();
}
@Override
public void onPause() {
super.onPause();
mapView.onPause();
}
@Override
public void onDestroy() {
super.onDestroy();
mapView.onDestroy();
}
@Override
public void onLowMemory() {
super.onLowMemory();
mapView.onLowMemory();
}
@Override
public void onLocationChanged(Location location) {
}
我的代碼如上所示。我可以在Android Studio中手動設置兩個標記,但是我想從當前位置獲取一個標記。 任何幫助,或任何想法?使用片段我想要在Android中獲取當前位置
感謝koksalb的答案 –