【问题标题】:Android: How to implement com.google.android.gms.maps.MapView without using a map activityAndroid:如何在不使用地图活动的情况下实现 com.google.android.gms.maps.MapView
【发布时间】:2017-06-01 04:48:27
【问题描述】:

我尝试在线性布局中实现MapView,但此布局不属于MapActivity。此外,我没有使用fragment 来实现该过程。

我还从 Google Developer Console 获得了一个 api 密钥,并启用了一些与 Locations 和 Maps 相关的 api。

这是我在 android studio 中的 android manifest,

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="sec.asdf_solutions.biz.security">

    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

    <uses-feature
        android:glEsVersion="0x00020000"
        android:required="true" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/logo"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">

        <uses-library android:name="com.google.android.maps" />

        <activity android:name=".SplashScreenActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".LoginActivity"></activity>
        <activity
            android:name=".RosterActivity"
            android:configChanges="locale"
            android:label=""
            android:theme="@style/AppTheme.Light" />

        <meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" />        

        <meta-data
            android:name="com.google.android.maps.v2.API_KEY"
            android:value="@string/api_key" />        

    </application>

</manifest>

这是我的布局文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/content_roster_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginBottom="5dp"
    android:layout_marginTop="3dp"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context=".RosterActivity"
    tools:showIn="@layout/app_bar_roster">

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

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:orientation="vertical">

            <com.google.android.gms.maps.MapView
                android:id="@+id/roster_map_view"
                android:layout_width="match_parent"
                android:layout_height="300dp"
                android:apiKey="xxx--yyy-zzz-aaa-www"
                android:layout_weight="1"/>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/roster_map_view"
                android:layout_marginTop="4dp"
                android:layout_marginBottom="15dp"
                android:alpha="0.5"
                android:background="#765491"
                android:orientation="vertical">

                <TextView
                    android:id="@+id/roster_ac_date_text"
                    android:layout_width="match_parent"
                    android:layout_height="36dp"
                    android:layout_gravity="center"
                    android:background="#563e6b"
                    android:paddingTop="5dp"
                    android:paddingBottom="5dp"
                    android:text="5 th MAY 2017"
                    android:textStyle="bold"
                    android:textAlignment="center"
                    android:textColor="@android:color/white"
                    android:textSize="22sp" />

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="10dp"
                    android:orientation="horizontal">

                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:text="START TIME"
                        android:textStyle="bold"
                        android:textAlignment="center"
                        android:textColor="@android:color/white"
                        android:textSize="14sp" />

                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:text="END TIME"
                        android:textStyle="bold"
                        android:textAlignment="center"
                        android:textColor="@android:color/white"
                        android:textSize="14sp" />

                </LinearLayout>


                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal">

                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:id="@+id/roster_st_time_text"
                        android:text="08.30 AM"
                        android:textStyle="bold"
                        android:textAlignment="center"
                        android:textColor="@android:color/white"
                        android:textSize="20sp" />

                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:id="@+id/roster_ed_time_text"
                        android:text="05.30 PM"
                        android:textStyle="bold"
                        android:textAlignment="center"
                        android:textColor="@android:color/white"
                        android:textSize="20sp" />

                </LinearLayout>


                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:id="@+id/roster_location_text"
                    android:layout_marginRight="35dp"
                    android:layout_marginLeft="35dp"
                    android:layout_marginTop="8dp"
                    android:drawablePadding="5dp"
                    android:drawableLeft="@drawable/ic_place_small"
                    android:textAlignment="center"
                    android:maxLines="3"
                    android:lines="3"
                    android:clickable="true"
                    android:textSize="14sp"
                    android:text="Australian Strategic Partnership\n#9, School Lane, Kollpity, Colombo 3"
                    android:textColor="@android:color/white" />

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="18dp"
                    android:orientation="horizontal">

                    <android.support.v7.widget.AppCompatButton
                        android:id="@+id/roster_accept_btn"
                        style="@style/ButtonStyle"
                        android:layout_height="32dp"
                        android:background="@color/loginColor"
                        android:layout_marginBottom="10dp"
                        android:layout_marginLeft="10dp"
                        android:layout_marginRight="40dp"
                        android:textSize="16sp"
                        android:textStyle="bold"
                        android:layout_weight="1"
                        android:clickable="true"
                        android:text="ACCEPT" />

                    <android.support.v7.widget.AppCompatButton
                        android:id="@+id/roster_reject_btn"
                        style="@style/ButtonStyle"
                        android:layout_height="32dp"
                        android:background="@color/acceptedPurpleColour"
                        android:layout_marginBottom="10dp"
                        android:layout_marginLeft="40dp"
                        android:layout_marginRight="10dp"
                        android:textSize="16sp"
                        android:textStyle="bold"
                        android:clickable="true"
                        android:layout_weight="1"
                        android:text="REJECT" />

                </LinearLayout>

            </LinearLayout>

        </LinearLayout>

    </ScrollView>

</RelativeLayout>

buid.gradle(Module:app)

apply plugin: 'com.android.application'

android {
    compileSdkVersion 23
    buildToolsVersion '25.0.0'

    defaultConfig {
        applicationId "sec.asdf_solutions.biz.security"
        minSdkVersion 18
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:23.4.0'
    compile 'com.android.support:design:23.4.0'
    compile 'com.prolificinteractive:material-calendarview:1.4.3'
    compile 'com.google.android.gms:play-services-maps:10.2.1'
    compile 'de.hdodenhof:circleimageview:2.1.0'
    compile 'com.squareup.retrofit2:retrofit:2.3.0'
    compile 'com.squareup.retrofit2:converter-gson:2.3.0'
    compile 'joda-time:joda-time:2.9.4'
    compile 'com.auth0.android:jwtdecode:1.1.0'
    compile 'com.android.support.constraint:constraint-layout:1.0.2'
    compile 'com.google.android.gms:play-services-location:10.2.1'
    compile 'com.android.support:support-annotations:24.2.0'
    compile 'com.google.firebase:firebase-messaging:10.2.0'
    compile 'com.google.maps:google-maps-services:0.1.20'
    testCompile 'junit:junit:4.12'
}

apply plugin: 'com.google.gms.google-services'

这是我实施上述流程的活动,

public class RosterActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {

    //protected MapView mMapView;

    android.support.v7.widget.AppCompatButton acceptButton;
    android.support.v7.widget.AppCompatButton rejectButton;
    private Context context;

    private String token;
    private long idLong;
    private Response responseBody;

    TextView rosterDateTextView;
    TextView rosterStartTimeTextView;
    TextView rosterEndTimeTextView;
    TextView rosterLocationTextView;

    View mainView;

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

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar_roster);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayShowHomeEnabled(true);

    }

    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        return true;
    }

}

特别说明:我没有使用 MapActivity。

有什么想法吗?

【问题讨论】:

    标签: java android google-maps android-mapview


    【解决方案1】:

    MapView's documentation:

    该类的用户必须将包含该视图的Activity或Fragment的所有生命周期方法转发到该类中对应的方法。特别是,您必须转发以下方法:

    • onCreate(Bundle)
    • onStart()
    • onResume()
    • onPause()
    • onStop()
    • onDestroy()
    • onSaveInstanceState()
    • onLowMemory()

    MapFragmentSupportMapFragment 所做的只是将所有这些回调转发到 MapView,因此如果您自己完成这项工作,则无需拥有特定的超类。

    例如:

    MapView rosterMapView;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_roster);
    
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar_roster);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayShowHomeEnabled(true);
    
        rosterMapView = (MapView) findViewById(R.id.roster_map_view);
        rosterMapView.onCreate(savedInstanceState);
    }
    
    @Override
    protected void onPause() {{
        super.onPause();
        rosterMapView.onPause();
    }
    

    等等 - 覆盖每个方法并将调用传递给您的MapView

    【讨论】:

    • 你能给我提供上述方法实现的代码sn-p吗?如果可以的话,对我有很大的帮助
    • 您只需覆盖每个回调并将调用传递给 MapView,就像它说的那样。我添加了一个示例。
    • 非常感谢您的正确回答。如果可以的话请给我一个标记
    • 正确答案,我挣扎了很久。实现回调后,我觉得 mapView 响应很慢,手势等,所以回到 Fragment 似乎非常高效和流畅
    【解决方案2】:

    您可以尝试这种方式。此外,您永远不应该发布/显示您的 API 密钥。尝试隐藏。

          <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">
    
                    <fragment
                    android:id="@+id/map"
                    android:name="com.google.android.gms.maps.MapFragment"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent" />
         </Linear Layout>
    
    
        //finding xml view from xml layout
               GoogleMap googleMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
    

    【讨论】:

    • 感谢您的建议。我忘记隐藏了
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多