简体   繁体   中英

Android google map v2 error at marker's icon

I try to implement google map v2 and all I get are errors. I've followed these tutorials: http://www.vogella.com/articles/AndroidGoogleMaps/article.html and http://blog-emildesign.rhcloud.com/?p=435 Well I've done everything like in the tutorials (I use google api 8 so sometimes I had to change something - eg SupportFragmentManager instead of FragmentManager) my activity (modified Lars Vogel's example):

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);
  }

}

my layout:

<?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> 

I got NPE at Marker hamburg. When I comment out this line I get next error at Marker kiel - ibitmapdescriptorfactory is not initialized. So I guess that's problem with .icon method on Marker - in the hamburg case it's null/default and in the second case is not initialized. I have no idea how to solve this.

you should check by this:

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)));
}

尝试......

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

I think all you need is a bunch of null checks for your map initialization, something like this.

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"));
                }
        }
}

Get the Latest One

// Declaration
    private GoogleMap googleMap;
    private SupportMapFragment mapFragment;

Activity

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();
    }
}

Initialization

/**
     * 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)));


  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM