在地图ANDROID上添加标记

时间:2014-07-30 21:47:04

标签: android eclipse api google-maps-android-api-2

晚安, 我在滑动选项卡中有一个带有地图的应用程序,但我希望该地图选择一个点并从该点开始。

提前谢谢。

附上JAVA:

public class Mapa extends Fragment {

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

    View android = inflater.inflate(R.layout.mapa_3, container, false);

    return android;
}

@Override
public void onDestroyView() {
    super.onDestroyView();
    Fragment f = getFragmentManager().findFragmentById(R.id.mapFragment);
    if (f != null)
        getFragmentManager().beginTransaction().remove(f).commit();
}

}

布局:

    <?xml version="1.0" encoding="utf-8"?>
<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:id="@+id/map_container"
    tools:context=".Mapa" >

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

</RelativeLayout>

1 个答案:

答案 0 :(得分:0)

我想你想在地图上添加一个标记并设置相机来查看它。以下是您可以实现的目标(更多信息herehere):

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

    View android = inflater.inflate(R.layout.mapa_3, container, false);

    final LatLng position = new LatLng(MARKER_LATITUDE, MARKER_LONGITUDE);

    // marker
    map = ((MapFragment) getSupportFragmentManager().findFragmentById(R.id.mapFragment)).getMap();
    map.addMarker(new MarkerOptions()
        .position(position)
        .title("Hello world"));

    // camera position
    map.setOnCameraChangeListener(new OnCameraChangeListener()
    {
        @Override
        public void onCameraChange(CameraPosition arg0)
        {
            CameraUpdate cu = CameraUpdateFactory.newLatLngZoom(position, 14);
            map.animateCamera(cu);
            map.setOnCameraChangeListener(null);
        }
    });
    return android;
}