我想在Android应用程序中为Google Map片段添加属性。我能正确找到地图吗?我将如何实施以下示例?源代码如下。
示例(我想添加类似内容)
@Override
public void onMapReady(GoogleMap map) {
map.addMarker(new MarkerOptions()
.position(new LatLng(0, 0))
.title("Marker"));
}
fragment1_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#D70B0D"
android:orientation="vertical" >
<TextView
android:id="@+id/textView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="5dp"
android:text="@string/textView6"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#FFFFFF"
android:textStyle="bold" />
<fragment
android:id="@+id/map"
android:name="com.google.android.gms.maps.MapFragment"
android:layout_width="match_parent"
android:layout_height="400dp"
android:layout_margin="5dp"/>
<Button
android:id="@+id/btnRespond"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:text="@string/btnRespond"
android:layout_gravity="center"
android:layout_marginBottom="5dp"
android:layout_marginTop="5dp"
android:textColor="#FFFFFF"
android:textStyle="bold"
android:background="@drawable/button_animation"
android:padding="5dp"/>
</LinearLayout>
Fragment1.java
package ie.itsligo.medication;
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 android.widget.Toast;
public class Fragment1 extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment1_layout, container, false);
Fragment map = (Fragment) getFragmentManager().findFragmentById(R.id.map);
Button btn = (Button) view.findViewById(R.id.btnRespond);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getActivity(), "You Are Now Active! Swipe Left For Individual Details & Medical Information", Toast.LENGTH_LONG).show();
}
});
return view;
}
}
答案 0 :(得分:0)
将fragment
投放到MapFragment
方法中的onCreateView
,然后像这样调用地图异步
MapFragment mapFragment = (MapFragment) getFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
取自谷歌地图文档here
然后在加载地图时会自动调用OnMapReady
。