在android上添加Google地图对象

时间:2013-03-14 04:38:49

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

所以我想在用户点击“添加圈子”按钮时将对象添加到我有地图的地图,但是当我尝试从xml获取地图时,我会在{{{{{{{{{{{ 1}} .getmap()下的任何修复或解决方法?示例代码很有用。

newCircle()

的xml:

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;

import com.google.android.gms.maps.*;
import com.google.android.gms.maps.model.CircleOptions;
import com.google.android.gms.maps.model.LatLng;

public class GoogleMaps extends FragmentActivity {
    GoogleMap mMap;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_google_maps);
        makeCircleButton();
    }

    private void makeCircleButton() {
        OnClickListener onClickListener = new OnClickListener() {
            @Override
            public void onClick(View v) {
                newCircle();
            }
        };

        Button button = (Button) findViewById(R.id.add_circular_area);
        button.setOnClickListener(onClickListener);
    }

    public void newCircle() {
        mMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map))
                .getMap();

        // Instantiates a new CircleOptions object and defines the center and
        // radius
        CircleOptions circleOptions = new CircleOptions().center(
                new LatLng(37.4, -122.1)).radius(1000); // In meters
        mMap.addCircle(circleOptions);

        // Get back the mutable Circle
        // Circle circle = mMap.addCircle(circleOptions);
    }

xml include:

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

    <include
        android:id="@+id/map"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        layout="@layout/fragment" />

    <ScrollView
        android:id="@+id/map_scrollview"
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:fadingEdge="none"
        android:scrollbarAlwaysDrawVerticalTrack="true" >

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

            <Button
                android:id="@+id/button1"
                android:layout_width="245dp"
                android:layout_height="wrap_content"
                android:text="Add rectangular area" />

            <Button
                android:id="@+id/add_circular_area"
                android:layout_width="245dp"
                android:layout_height="wrap_content"
                android:text="Add circular area" />

        </LinearLayout>

    </ScrollView>

</RelativeLayout>

1 个答案:

答案 0 :(得分:2)

您已将片段名称用作com.google.android.gms.maps.SupportMapFragment,因此无法获取MapFragment您应使用getSupportFragmentManager()代替getFragmentManager()并将其投放到SupportMapFragment

更改

  mMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map))
                .getMap();

mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
                .getMap();

希望它会有所帮助。