gpt4 book ai didi

java - 在另一个 Activity 中实例化 Google map fragment

转载 作者:太空宇宙 更新时间:2023-11-04 14:08:50 25 4
gpt4 key购买 nike

我有主要 Activity 和一个 fragment 。在那个 fragment 中,我使用了谷歌地图。我想知道如何使用 java 将 map fragment 的实例传递给主 Activity 。因为我想将该 map 操纵到主要 Activity 中。

我在主要 Activity 中有这门课

public static class HomeFragment extends Fragment {

private static View view;

public HomeFragment() {
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

if(view != null)
{
ViewGroup parent = (ViewGroup) view.getParent();
if(parent != null)
{
parent.removeView(view);
}
}
try
{
view = inflater.inflate(R.layout.fragment_home, container, false);
}
catch(InflateException e){
// map is already there, just return view as it is
}
return view;
}

}

fragment_home.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"
android:padding="0dp"
tools:context="com.gaurav.googlemap.HomeMap" >

<fragment
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment" >
</fragment>

</RelativeLayout>

我想在主要 Activity 中实例化该行下方的 map ,

view = inflater.inflate(R.layout.fragment_home, container, false);

谢谢。

最佳答案

仅供引用,https://developers.google.com/maps/documentation/android/start#getting_the_google_maps_android_api_v2

你的 map .xml

<FrameLayout 
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="2dp"
>
<com.google.android.gms.maps.MapView
android:id="@+id/mapview"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</FrameLayout>

你的 Activity .class

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Log.i(TAG, "oncreate");
View rootView = inflater.inflate(R.layout.yourmap, container, false);

// Gets the MapView from the XML layout and creates it
mapView = (MapView) rootView.findViewById(R.id.mapview);
mapView.onCreate(savedInstanceState);
setCurrentLocationMarker();

return rootView;
}

public void setCurrentLocationMarker(){
try {
// Gets to GoogleMap from the MapView and does initialization stuff
map = mapView.getMap();
map.getUiSettings().setMyLocationButtonEnabled(false);
map.setMyLocationEnabled(true);

// Needs to call MapsInitializer before doing any CameraUpdateFactory calls
try {
MapsInitializer.initialize(getActivity());
} catch (Exception e) {
Log.e(TAG , "Exception: "+e.getMessage());
e.printStackTrace();
}

gps = new GPSTracker(getActivity());

if(gps.canGetLocation()) {

myLatitude = gps.getLatitude();
myLongitude = gps.getLongitude();

if(myLatitude==0 && myLongitude==0){
LocationManager lm = (LocationManager)getActivity().getSystemService(Context.LOCATION_SERVICE);
Location location = getLastKnownLocation(lm);
Log.i(TAG, "location : "+location);
if(location!=null){
myLatitude = location.getLatitude();
myLongitude = location.getLongitude();
}
}

} else {
// Can't get location.
// GPS or network is not enabled.
// Ask user to enable GPS/network in settings.
gps.showSettingsAlert();
Log.i(TAG, "gps.canGetLocation() : "+gps.canGetLocation());
return;
}

Log.i(TAG, "myLatitude :"+myLatitude+", myLongitude: "+myLongitude);


// Updates the location and zoom of the MapView
if(myLatitude>0 && myLongitude>0){
CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(new LatLng(myLatitude, myLongitude), 10);
map.animateCamera(cameraUpdate);

MarkerInfo markerinfo = new MarkerInfo("", "Current Location", myLatitude, myLongitude, true);
addMarker(markerinfo);
}


map.setOnMarkerClickListener(onClickListener);
map.setOnInfoWindowClickListener(onInfoWindowClickListener);
marker.showInfoWindow();

} catch (Exception e) {
Log.e(TAG, "Exception : "+e.getMessage());
}
}

关于java - 在另一个 Activity 中实例化 Google map fragment ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28575041/

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