如何在相同的布局上显示谷歌地图和标记?

时间:2016-02-25 04:34:11

标签: android android-layout android-studio google-maps-api-3 marker

目标

当点击显示地图按钮时,我想开始一个新活动(我称之为MapActivity),其中需要显示谷歌地图和标记。

Main page

我在哪里

单击“显示地图”按钮时,将使用地图和标记启动新活动,但两个布局都会重叠。

Map and Marker Activity

MainActivity.class

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final Button button = (Button) findViewById(R.id.find);
        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {

                Spinner fromSpinner = (Spinner) findViewById(R.id.from);
                Spinner toSpinner = (Spinner) findViewById(R.id.to);

                Location fromLocation = (Location) fromSpinner.getSelectedItem();
                Location toLocation = (Location) toSpinner.getSelectedItem();

                new ShortestDistanceTask(fromLocation, toLocation).execute();
            }
        });

    }

    public void onShowMap(View v){
        if (v.getId() == R.id.showMap){
            Intent i = new Intent(MainActivity.this, MapActivity.class);
            startActivity(i);
        }
    }

MapActivity.class

public class MapActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.map_activity);

        FragmentManager FM = getFragmentManager();
        FragmentTransaction FT = FM.beginTransaction();
        Marker_Activity MA = new Marker_Activity();
        FT.add(R.id.maplayout   ,MA);
        FT.commit();
    }
}

Marker_Activity.class

public class Marker_Activity extends Fragment {
    GoogleMap googleMap;
    private static final LatLng Echostar = new LatLng(10,20);

    public void processMap(View v){
        if(googleMap == null){
            googleMap = ((MapFragment) getChildFragmentManager().findFragmentById(R.id.map)).getMap();
        }

        if(googleMap != null){
            googleMap.addMarker(new MarkerOptions().position(Echostar).title("This is the marked area"));
            googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(Echostar,100));
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.map_activity, container, false);
        processMap(v);
        return v;
    }
}
<?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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="innoday.echostar.com.echopath.MainActivity">

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentTop="true"
        android:layout_alignParentStart="true"
        android:weightSum="1">

        <Spinner
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/from"
            android:spinnerMode="dropdown" />

        <Spinner
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/to"
            android:layout_gravity="center_horizontal"
            android:spinnerMode="dropdown" />

        <Button
            style="?android:attr/buttonStyleSmall"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Find"
            android:id="@+id/find"
            android:layout_gravity="center_horizontal" />

        <Button
            style="?android:attr/buttonStyleSmall"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Show Map"
            android:id="@+id/showMap"
            android:layout_gravity="center_horizontal"
            android:onClick="onShowMap" />

        <TableLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="0.23"
            android:orientation="vertical"
            android:id="@+id/table">

            <TableRow
                android:layout_width="match_parent"
                android:layout_height="match_parent">

            </TableRow>
        </TableLayout>

    </LinearLayout>
</RelativeLayout>

map_activity.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="300dp"
        android:id="@+id/maplayout" />

    <fragment xmlns:android="http://schemas.android.com/apk/res/android"
        android:name="com.google.android.gms.maps.MapFragment"
        android:id="@+id/map"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
</RelativeLayout>

1 个答案:

答案 0 :(得分:0)

您应该有一个google_maps.xml文件

<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/mapviewfragment"/>

其中mapviewfragment.xml是

<?xml version="1.0" encoding="utf-8"?>


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <ImageView
            android:id="@+id/imageView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/googlemap" />
    </LinearLayout>


</LinearLayout>

通过这种方式,您的Google地图可以显示整个屏幕,并且可以在其上显示标记。

您的MapActivity应该实现

GoogleApiClient.ConnectionCallbacks

GoogleApiClient.OnConnectionFailedListener

OnMapReadyCallback

LocationListener

并覆盖至少

onMapReady()

onLocationChanged()

onConnected()方法。