如何使用Google Map的V2 MapView for Android来显示操作栏? [英] How to use Google Map's V2 MapView for Android to display the actionbar?

查看:95
本文介绍了如何使用Google Map的V2 MapView for Android来显示操作栏?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在Google地图上方显示操作栏,但我意识到,一旦Google地图打开,它不会使用我的应用程序,而是Google的应用程序,因此标题栏消失。



我明白使用MapView可以实现这一点,但我不知道如何从使用片段转换到MapView,因为我得到了一个 NullPointerException



以下是AndroidManifest.xml:

 < xml version =1.0encoding =utf-8?> 
< manifest xmlns:android =http://schemas.android.com/apk/res/android
package =org.test>

<使用权限android:name =android.permission.INTERNET/>
< uses-permission android:name =android.permission.ACCESS_NETWORK_STATE/>
< uses-permission android:name =android.permission.WRITE_EXTERNAL_STORAGE/>
<使用权限android:name =com.google.android.providers.gsf.permission.READ_GSERVICES/>
< uses-permission android:name =android.permission.ACCESS_COARSE_LOCATION/>
< uses-permission android:name =android.permission.ACCESS_FINE_LOCATION/>

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

< 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 / google_maps_key/>

< activity
android:name =。MapActivity
android:label =@ string / title_activity_map>
< / activity>

< / application>

< / manifest>

我的activity_map.xml:

 < fragment xmlns:android =http://schemas.android.com/apk/res/android
xmlns:tools =http://schemas.android.com / tools
xmlns:map =http://schemas.android.com/apk/res-auto
android:layout_width =match_parent
android:layout_height =match_parent
android:id =@ + id / map
tools:context =org.test.MapActivity
android:name =com.google.android.gms.maps.SupportMapFragment
map:cameraTargetLat =35.25215
map:cameraTargetLng = - 112.24659
map:uiTiltGestures =true
map:uiZoomGestures =true
map:uiZoomControls =true/>

我的activity_mapview.xml:

 <?xml version =1.0encoding =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>

< com.google.android.gms.maps.MapView
android:id =@ + id / mapView
android:layout_width =match_parent
android:layout_height =match_parent
/>

< / LinearLayout>

我的MapActivity.java:

  public class MapActivity扩展FragmentActivity {
私有GoogleMap mMap;
私人MapView地图查看;
私人标记标记;

private static final LatLng ONE = new LatLng(32.882216,-117.222028);
private static final LatLng TWO = new LatLng(32.872000,-117.232004);
private static final LatLng THREE = new LatLng(32.880252,-117.233034);
private static final LatLng FOUR = new LatLng(32.885200,-117.226003);

private ArrayList< LatLng> coords = new ArrayList< LatLng>();
private static final int POINTS = 4;

@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mapview);
mapView =(MapView)findViewById(R.id.mapView);
mapView.onCreate(savedInstanceState);

setUpMapIfNeeded();
}

@Override
protected void onStop(){
super.onStop();


@Override
protected void onResume(){
super.onResume();
mapView.onResume();
setUpMapIfNeeded();


@Override
protected void onDestroy(){
super.onDestroy();
mapView.onDestroy();


@Override
public void onLowMemory(){
super.onLowMemory();
mapView.onLowMemory();


@Override
protected void onPause(){
super.onPause();
mapView.onPause();
}

@Override
protected void onSaveInstanceState(Bundle outState){
super.onSaveInstanceState(outState);
mapView.onSaveInstanceState(outState);
}

private void setUpMapIfNeeded(){
//做一个空检查来确认我们还没有实例化地图。
if(mMap == null){
Log.d(Test,GO HERE);
mMap =((SupportMapFragment)getSupportFragmentManager()。findFragmentById(R.id.map))。getMap();
Log.d(Test,GO THERE);
if(mMap!= null){
coords.add(ONE);
coords.add(TWO);
coords.add(THREE);
coords.add(FOUR);

setUpMap();



$ b private void setUpMap(){
for(int i = 0; i< POINTS; i ++){
mMap.addMarker(new MarkerOptions()。position(coords.get(i))
.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED)));
}
}
}

styles.xml:

 <资源> 

<! - 基本应用程序主题。 - >
< style name =AppThemeparent =Theme.AppCompat.Light.DarkActionBar>
<! - 在这里自定义您的主题。 - >
< / style>

< style name =FullscreenThemeparent =android:Theme.NoTitleBar>
< item name =android:windowContentOverlay> @ null< / item>
< item name =android:windowBackground> @ null< / item>
< item name =metaButtonBarStyle> @ style / ButtonBar< / item>
< item name =metaButtonBarButtonStyle> @ style / ButtonBarButton< / item>
< / style>

<! - 向后兼容版本?android:attr / buttonBarStyle - >
< style name =ButtonBar>
< item name =android:paddingLeft> 2dp< / item>
< item name =android:paddingTop> 5dp< / item>
< item name =android:paddingRight> 2dp< / item>
< item name =android:paddingBottom> 0dp< / item>
< item name =android:background> @android:drawable / bottom_bar< / item>
< / style>

<! - 向后兼容版本?android:attr / buttonBarButtonStyle - >
< style name =ButtonBarButton/>


基于做一些日志语句,它看起来好像是 GO HERE 被打印出来,但它永远不会到达 GO THERE ,所以问题在于下面的代码,但我不知道如何修复它,因为我是新的Android程序设计:

$ $ p $ lt; code> mMap =((SupportMapFragment)getSupportFragmentManager()。findFragmentById(R.id .map))
.getMap();

Logcat:

  08-21 01:29:20.593 30698-30698 / org.test E / AndroidRuntime:FATAL EXCEPTION:main 
java.lang.RuntimeException:无法启动活动ComponentInfo {org./org.test .MapActivity}:java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
at android.app.ActivityThread.access $ 1500(ActivityThread.java:117)
at android.app.ActivityThread $ H.handleMessage(ActivityThread.java:931)
at android.os.Handler.dispatchMessage (Handler.java:99)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3683)
at java.lang.reflect.Method.invokeNative(本地方法)$ b $在java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:839)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
在dalvik.system.NativeStart.main(本地方法)
引起:java.lang.NullPointerException

org.test.MapActivity.setUpMapIfNeeded(MapActivity.java:183)
at
org.test.MapActivity.onCreate(MapActivity.java:77)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
at android.app。在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
(在android.app.ActivityThread.access
$(ActivityThread.java:117)$)
(ActivityThread.java:1611) b $ b at android.app.ActivityThread $ H.handleMessage(ActivityThread.java:931)
在android.os.Handler.dispatchMessage(Handler.java:99)
在android.os.Looper.loop(Looper.java:130)
在android.app.ActivityThread.main(ActivityThread.java :3683)
at java.lang.reflect.Method.invokeNative(Native Method)$ b $在java.lang.reflect.Method.invoke(Method.java:507)
at com.android .internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:839)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
at dalvik.system.NativeStart .main(Native Method)

谢谢。

解决方案

在您的build.gradle中定义依赖关系

  compile'c​​om.android.support :appcompat-v7:23.0.0'
compile'c​​om.google.android.gms:play-services:7.8.0'

获取标题从AppCompatActivity扩展您的活动并将标题设置为 setTitle()



对于使用MapView,您不需要在onResume中进行任何检查。

只需通过onCreate中的异步方法获取您的地图。

<$ c

  public class MapActivity extends AppCompatActivity {$ c $> c> setUpMap  
私有GoogleMap mMap;
私人MapView地图查看;
私人标记标记;

private static final LatLng ONE = new LatLng(32.882216,-117.222028);
private static final LatLng TWO = new LatLng(32.872000,-117.232004);
private static final LatLng THREE = new LatLng(32.880252,-117.233034);
private static final LatLng FOUR = new LatLng(32.885200,-117.226003);

private ArrayList< LatLng> coords = new ArrayList< LatLng>();
private static final int POINTS = 4;

@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mapview);
setTitle(我的地图);
mapView =(MapView)findViewById(R.id.mapView);
mapView.onCreate(savedInstanceState);
mapView.getMapAsync(new OnMapReadyCallback(){
@Override
public void onMapReady(GoogleMap googleMap){
setUpMap(googleMap);
}
} );
}
@Override
protected void onDestroy(){
super.onDestroy();
mapView.onDestroy();

@Override
protected void onResume(){
super.onResume();
mapView.onResume();

@Override
protected void onPause(){
super.onPause();
mapView.onPause();

@Override
public void onLowMemory(){
super.onLowMemory();
mapView.onLowMemory();
}
@Override
protected void onSaveInstanceState(Bundle outState){
super.onSaveInstanceState(outState);
mapView.onSaveInstanceState(outState);
}
private void setUpMap(GoogleMap map){
mMap = map;

coords.add(ONE);
coords.add(TWO);
coords.add(THREE);
coords.add(FOUR);
for(int i = 0; i< POINTS; i ++){
mMap.addMarker(new MarkerOptions()
.position(coords.get(i))
。图标(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED)));
}
}
}


I am trying to display the actionbar above Google Maps, but I realize that once Google Maps open, it is not using my app, but Google's app, therefore the title bar disappears.

I understand that using a MapView allows me to achieve this but I'm not sure how to transition from using a fragment to a MapView as I am getting a NullPointerException.

Here's the AndroidManifest.xml:

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

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

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

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

        <activity
            android:name=".MapActivity"
            android:label="@string/title_activity_map" >
        </activity>

    </application>

</manifest>

My activity_map.xml:

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:map="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/map"
    tools:context="org.test.MapActivity"
    android:name="com.google.android.gms.maps.SupportMapFragment"
    map:cameraTargetLat="35.25215"
    map:cameraTargetLng="-112.24659"
    map:uiTiltGestures="true"
    map:uiZoomGestures="true"
    map:uiZoomControls="true" />

My activity_mapview.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">

    <com.google.android.gms.maps.MapView
        android:id="@+id/mapView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        />

</LinearLayout>

My MapActivity.java:

public class MapActivity extends FragmentActivity {
  private GoogleMap mMap;
  private MapView mapView;
  private Marker marker;

  private static final LatLng ONE = new LatLng(32.882216, -117.222028);
  private static final LatLng TWO = new LatLng(32.872000, -117.232004);
  private static final LatLng THREE = new LatLng(32.880252, -117.233034);
  private static final LatLng FOUR = new LatLng(32.885200, -117.226003);

  private ArrayList<LatLng> coords = new ArrayList<LatLng>();
  private static final int POINTS = 4;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_mapview);
    mapView = (MapView) findViewById(R.id.mapView);
    mapView.onCreate(savedInstanceState);

    setUpMapIfNeeded();
  }

  @Override
  protected void onStop() {
    super.onStop();
  }

  @Override
  protected void onResume() {
    super.onResume();
    mapView.onResume();
    setUpMapIfNeeded();
  }

  @Override
  protected void onDestroy() {
    super.onDestroy();
    mapView.onDestroy();
  }

  @Override
  public void onLowMemory() {
    super.onLowMemory();
    mapView.onLowMemory();
  }

  @Override
  protected void onPause() {
    super.onPause();
    mapView.onPause();
  }

  @Override
  protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    mapView.onSaveInstanceState(outState);
  }

  private void setUpMapIfNeeded() {
    // Do a null check to confirm that we have not already instantiated the map.
    if (mMap == null) {
      Log.d("Test", "GO HERE");
      mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
      Log.d("Test", "GO THERE");
      if (mMap != null) {
        coords.add(ONE);
        coords.add(TWO);
        coords.add(THREE);
        coords.add(FOUR);

        setUpMap();
      }
    }
  }

  private void setUpMap() {
    for (int i = 0; i < POINTS; i++) {
      mMap.addMarker(new MarkerOptions().position(coords.get(i))
          .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED)));
    }
  }
}

The styles.xml:

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
    </style>

    <style name="FullscreenTheme" parent="android:Theme.NoTitleBar">
        <item name="android:windowContentOverlay">@null</item>
        <item name="android:windowBackground">@null</item>
        <item name="metaButtonBarStyle">@style/ButtonBar</item>
        <item name="metaButtonBarButtonStyle">@style/ButtonBarButton</item>
    </style>

    <!-- Backward-compatible version of ?android:attr/buttonBarStyle -->
    <style name="ButtonBar">
        <item name="android:paddingLeft">2dp</item>
        <item name="android:paddingTop">5dp</item>
        <item name="android:paddingRight">2dp</item>
        <item name="android:paddingBottom">0dp</item>
        <item name="android:background">@android:drawable/bottom_bar</item>
    </style>

    <!-- Backward-compatible version of ?android:attr/buttonBarButtonStyle -->
    <style name="ButtonBarButton" />

Based on doing some log statements, it appears as though GO HERE is printed out but it never reaches GO THERE, so the issue lies in the following code, but I'm not sure how to fix it, as I'm new to Android programming:

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

Logcat:

08-21 01:29:20.593  30698-30698/org.test E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.RuntimeException: Unable to start activity ComponentInfo{org./org.test.MapActivity}: java.lang.NullPointerException
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
            at android.app.ActivityThread.access$1500(ActivityThread.java:117)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:130)
            at android.app.ActivityThread.main(ActivityThread.java:3683)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:507)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.NullPointerException
            at 
org.test.MapActivity.setUpMapIfNeeded(MapActivity.java:183)
                at 
org.test.MapActivity.onCreate(MapActivity.java:77)
                at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
                at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
                at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
                at android.app.ActivityThread.access$1500(ActivityThread.java:117)
                at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
                at android.os.Handler.dispatchMessage(Handler.java:99)
                at android.os.Looper.loop(Looper.java:130)
                at android.app.ActivityThread.main(ActivityThread.java:3683)
                at java.lang.reflect.Method.invokeNative(Native Method)
                at java.lang.reflect.Method.invoke(Method.java:507)
                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
                at dalvik.system.NativeStart.main(Native Method)

Thanks.

解决方案

Define dependencies in your build.gradle

compile 'com.android.support:appcompat-v7:23.0.0'
compile 'com.google.android.gms:play-services:7.8.0'

For getting title extend your activity from AppCompatActivity and set title with setTitle()

For using MapView you don't need to make any checks in onResume.
Just get your map by async method in onCreate.
This setUpMap method will be called when map is ready.

public class MapActivity extends AppCompatActivity {
    private GoogleMap mMap;
    private MapView mapView;
    private Marker marker;

    private static final LatLng ONE = new LatLng(32.882216, -117.222028);
    private static final LatLng TWO = new LatLng(32.872000, -117.232004);
    private static final LatLng THREE = new LatLng(32.880252, -117.233034);
    private static final LatLng FOUR = new LatLng(32.885200, -117.226003);

    private ArrayList<LatLng> coords = new ArrayList<LatLng>();
    private static final int POINTS = 4;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mapview);
        setTitle("My Map");
        mapView = (MapView) findViewById(R.id.mapView);
        mapView.onCreate(savedInstanceState);
        mapView.getMapAsync(new OnMapReadyCallback() {
            @Override
            public void onMapReady(GoogleMap googleMap) {
                setUpMap(googleMap);
            }
        });
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        mapView.onDestroy();
    }
    @Override
    protected void onResume() {
        super.onResume();
        mapView.onResume();
    }
    @Override
    protected void onPause() {
        super.onPause();
        mapView.onPause();
    }
    @Override
    public void onLowMemory() {
        super.onLowMemory();
        mapView.onLowMemory();
    }
    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        mapView.onSaveInstanceState(outState);
    }
    private void setUpMap(GoogleMap map) {
        mMap = map;

        coords.add(ONE);
        coords.add(TWO);
        coords.add(THREE);
        coords.add(FOUR);
        for (int i = 0; i < POINTS; i++) {
            mMap.addMarker(new MarkerOptions()
                    .position(coords.get(i))
                    .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED)));
        }
    }
}

这篇关于如何使用Google Map的V2 MapView for Android来显示操作栏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆