如何使用Android应用程序上的按钮打开谷歌地图

如何使用Android应用程序上的按钮打开谷歌地图,android,button,maps,onclicklistener,Android,Button,Maps,Onclicklistener,我正在尝试制作一个名为Map的应用程序,一旦按下按钮,就会显示城市地图。到目前为止,我已经制作了一个包含“地图”按钮的应用程序和一个单独的“我的地图”应用程序,它使用谷歌开发者教程在应用程序打开时显示地图。我在尝试合并这两个应用程序时遇到问题。我正在使用OnClickListener,但我不知道应该放置什么以便在按下按钮后显示地图 编辑 下面是我的地图片段类: public class MapFragment extends FragmentActivity {

我正在尝试制作一个名为Map的应用程序,一旦按下按钮,就会显示城市地图。到目前为止,我已经制作了一个包含“地图”按钮的应用程序和一个单独的“我的地图”应用程序,它使用谷歌开发者教程在应用程序打开时显示地图。我在尝试合并这两个应用程序时遇到问题。我正在使用OnClickListener,但我不知道应该放置什么以便在按下按钮后显示地图

编辑

下面是我的地图片段类:

    public class MapFragment extends FragmentActivity
    {
        private GoogleMap mMap;
        private LatLngBounds ILOILO = new LatLngBounds(new LatLng(10.6, 122.466), new LatLng(10.716, 122.566));

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

        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
        {
            View v = inflater.inflate(R.layout.map_layout, container, false);

            mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
            mMap.setMyLocationEnabled(true);
            mMap.setMapType(GoogleMap.MAP_TYPE_TERRAIN);
            mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(ILOILO.getCenter(), 10));

            if (mMap == null)
            {
                 Toast.makeText(this, "Google Maps not available", 
                     Toast.LENGTH_LONG).show();
            }

            return v;
        }
    }
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:map="http://schemas.android.com/apk/res-auto"
    android:id="@+id/map"
    map:uiCompass="false"
    map:uiTiltGestures="true"
    map:uiZoomControls="true"
    map:uiZoomGestures="true"
    map:uiRotateGestures="true"
    map:uiScrollGestures="false"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    class="com.google.android.gms.maps.SupportMapFragment"/>
以下是我的地图XML文件:

    public class MapFragment extends FragmentActivity
    {
        private GoogleMap mMap;
        private LatLngBounds ILOILO = new LatLngBounds(new LatLng(10.6, 122.466), new LatLng(10.716, 122.566));

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

        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
        {
            View v = inflater.inflate(R.layout.map_layout, container, false);

            mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
            mMap.setMyLocationEnabled(true);
            mMap.setMapType(GoogleMap.MAP_TYPE_TERRAIN);
            mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(ILOILO.getCenter(), 10));

            if (mMap == null)
            {
                 Toast.makeText(this, "Google Maps not available", 
                     Toast.LENGTH_LONG).show();
            }

            return v;
        }
    }
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:map="http://schemas.android.com/apk/res-auto"
    android:id="@+id/map"
    map:uiCompass="false"
    map:uiTiltGestures="true"
    map:uiZoomControls="true"
    map:uiZoomGestures="true"
    map:uiRotateGestures="true"
    map:uiScrollGestures="false"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    class="com.google.android.gms.maps.SupportMapFragment"/>
我还修改了清单,因为我无法找到显式活动类com.example.ipp.MapFragment;您是否在AndroidManifest.xml中声明了此活动?错误下面是我的舱单

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >

        <activity
            android:name="MapFragment" >
        </activity>

        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
问题


当我尝试单击地图按钮时,它会显示我的UI,而不是显示我准备好的地图。

我在代码中的任何地方都看不到R.id.Map…@shkschneider这是我的地图xml文件这是按钮:ImageButton mapButton=ImageButton FindViewByDr.id.mapButton;mapButton.setOnClickListenermapListener;我不知道该为听众做些什么。我不知道你对R.id.Map的意思是否就是这个,所以你有两个应用程序。第一个应用程序工作正常,您可以查看地图。现在,您需要从其他应用程序访问按钮单击第一个应用程序并在此处显示地图。我没听错吧?@Anatol,是的。我试过像我在网上找到的大多数教程一样使用MapView,但当我按下地图按钮而不是显示我准备好的地图时,它会在我的设备上打开谷歌地图应用程序。@Sher Ann Vicsie Acasio,我现在不知道你想完成什么教程。首先,你应该检查官方文件。接下来,如果你想在片段中使用谷歌地图,你应该检查这个。第二个答案是15分,这有助于我做到这一点。祝你好运