标记图标上的Android谷歌地图v2错误

时间:2013-09-13 13:16:13

标签: android google-maps-api-2

我尝试实施谷歌地图v2,我得到的都是错误。我已按照以下教程进行操作:http://www.vogella.com/articles/AndroidGoogleMaps/article.htmlhttp://blog-emildesign.rhcloud.com/?p=435 好吧,我已经完成了教程中的所有内容(我使用google api 8,所以有时我不得不改变一些东西 - 例如SupportFragmentManager而不是FragmentManager) 我的活动(修改了L​​ars Vogel的例子):

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;

import com.google.android.gms.common.GooglePlayServicesNotAvailableException;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapsInitializer;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;



public class MapActivity extends FragmentActivity {

      static final LatLng HAMBURG = new LatLng(53.558, 9.927);
  static final LatLng KIEL = new LatLng(53.551, 9.993);
  private SupportMapFragment map;
  private GoogleMap mMap;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_map);
    try {
        MapsInitializer.initialize(this);
    } catch (GooglePlayServicesNotAvailableException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    map = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map));
    mMap = map.getMap();
    Marker hamburg = mMap.addMarker(new MarkerOptions().position(HAMBURG)
        .title("Hamburg"));
    Marker kiel = mMap.addMarker(new MarkerOptions()
        .position(KIEL)
        .title("Kiel")
        .snippet("Kiel is cool")
        .icon(BitmapDescriptorFactory
            .fromResource(R.drawable.ic_launcher)));


    // Move the camera instantly to hamburg with a zoom of 15.
    mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(HAMBURG, 15));

    // Zoom in, animating the camera.
    mMap.animateCamera(CameraUpdateFactory.zoomTo(10), 2000, null);
  }

}

我的布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
tools:context=".MapActivity" >

<fragment
    class="com.google.android.gms.maps.SupportMapFragment"
    android:id="@+id/map"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

</RelativeLayout> 

我在Marker汉堡获得了NPE。当我注释掉这一行时,我在Marker kiel得到了下一个错误 - ibitmapdescriptorfactory未初始化。所以我猜这是Marker上的.icon方法的问题 - 在hamburg案例中它是null / default而在第二种情况下没有初始化。我不知道如何解决这个问题。

4 个答案:

答案 0 :(得分:2)

你应该检查一下:

GoogleMap map;    

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

if (map !=null){
  Marker hamburg = map.addMarker(new MarkerOptions().position(HAMBURG)
      .title("Hamburg"));
  Marker kiel = map.addMarker(new MarkerOptions()
      .position(KIEL)
      .title("Kiel")
      .snippet("Kiel is cool")
      .icon(BitmapDescriptorFactory
          .fromResource(R.drawable.ic_launcher)));
}

答案 1 :(得分:1)

尝试......

mMap = ((SupportMapFragment) getFragmentManager().findFragmentById(R.id.map)).getExtendedMap();

答案 2 :(得分:1)

我认为您需要的是对地图初始化进行的一系列空检查,如下所示。

GoogleMap mMap = null;

@Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_map);
//map part 
        if(mMap == null) {
            mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
            if(gMap != null) {
                gMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
                    Marker hamburg = mMap.addMarker(new MarkerOptions().position(HAMBURG)
    .title("Hamburg"));
                }
        }
}

答案 3 :(得分:0)

获取最新的

// Declaration
    private GoogleMap googleMap;
    private SupportMapFragment mapFragment;

活动

public class GetDirection extends AppCompatActivity implements OnMapReadyCallback {

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

        // check for permission if required for Android MarsMallow
        // then 
        initilizeMap();
    }
}

初​​始化

/**
     * function to load map. If map is not created it will create it for you
     * */
    private void initilizeMap() {
        if (googleMap == null) {
            mapFragment = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(
                    R.id.map));
            try {
                mapFragment.getMapAsync(this);
            }
            catch (RuntimeRemoteException e)
            {
                // handle exception
            }


        }
    }

onMapReady

    //    It provides a non-null instance of GoogleMap
        @Override
        public void onMapReady(GoogleMap googleMap) {
            this.googleMap = googleMap;

            // Now add marker
            Marker kiel = googleMap.addMarker(new 
            MarkerOptions().position(HAMBURG)
           .title("Hamburg"));
            Marker kiel = googleMap.addMarker(new MarkerOptions()
           .position(KIEL)
           .title("Kiel")
           .snippet("Kiel is cool")
           .icon(BitmapDescriptorFactory
           .fromResource(R.drawable.ic_launcher)));


  }