是否可以向GoogleMaps添加按钮?

时间:2015-06-26 21:37:37

标签: android google-maps android-layout

我是否有可能向googlemaps添加按钮我感到有点困惑。我试图创建一个相对布局,我将片段和图像按钮放在相同的布局中,但由于某种原因我的应用程序继续崩溃。这是我的XML的样子:

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MapsActivity" >

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


<ImageButton
    android:id="@+id/theimagebutton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/car"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"/>
  </RelativeLayout>

我得到此错误 java.lang.RuntimeException:无法恢复活动java.lang.NullPointerException:尝试调用虚拟方法&com ;google.android.gms.maps.GoogleMap com.google .android.gms.maps.SupportMapFragment.getMap()&#39;在空对象引用上

  import android.content.Context;
  import android.graphics.Color;
  import android.location.Criteria;
  import android.location.Location;
   import android.location.LocationManager;
  import android.os.Bundle;
  import android.support.v4.app.FragmentActivity;
  import android.widget.Toast;

 import com.google.android.gms.maps.CameraUpdate;
 import com.google.android.gms.maps.CameraUpdateFactory;
 import com.google.android.gms.maps.GoogleMap;
 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.MarkerOptions;
 import com.google.android.gms.maps.model.Polyline;
 import com.google.android.gms.maps.model.PolylineOptions;
 import com.parse.ParseGeoPoint;
 import com.parse.ParseObject;

 public class MapsActivity extends FragmentActivity implements RoutingListener {
protected GoogleMap mMap;
protected LatLng start;
protected LatLng end;

/**
 * This activity loads a map and then displays the route and pushpins on it.
 */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_maps);
    SupportMapFragment mMap = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);

    try {
        setUpMapIfNeeded();

    } catch (Exception e) {
        e.printStackTrace();
    }
}
@Override
protected void onResume() {
    super.onResume();
    setUpMapIfNeeded();
}

private void setUpMapIfNeeded() {
    // Do a null check to confirm that we have not already instantiated the map.
    if (mMap == null) {
        // Try to obtain the map from the SupportMapFragment.
        mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
                .getMap();
        // Check if we were successful in obtaining the map.
        if (mMap != null) {
            setUpMap();
        }
    }
}

/**
 * This is where we can add markers or lines, add listeners or move the camera. In this case, we
 * just add a marker near Africa.
 * <p/>
 * This should only be called once and when we are sure that {@link #mMap} is not null.
 */

private void setUpMap() {


    // Enable MyLocation Layer of Google Map
    mMap.setMyLocationEnabled(true);

    // Get LocationManager object from System Service LOCATION_SERVICE
    LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

    // Create a criteria object to retrieve provider
    Criteria criteria = new Criteria();

    // Get the name of the best provider
    String provider = locationManager.getBestProvider(criteria, true);

    // Get Current Location
    Location myLocation = locationManager.getLastKnownLocation(provider);

    // set map type
    mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);

    // Get latitude of the current location

    mMap.getUiSettings().setZoomControlsEnabled(true);
    mMap.getUiSettings().setMyLocationButtonEnabled(true);
    mMap.getUiSettings().setCompassEnabled(true);
    mMap.getUiSettings().setRotateGesturesEnabled(true);
    mMap.getUiSettings().setZoomGesturesEnabled(true);


    Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);

    if (location != null) {
        double longitude = location.getLongitude();
        double latitude = location.getLatitude();


        // Get longitude of the current location

        // Create a LatLng object for the current location
        LatLng latLng = new LatLng(latitude, longitude);


        ParseObject parkingobject = new ParseObject("Kitchen");
        parkingobject.put("username","Mike");
        ParseGeoPoint point = new ParseGeoPoint(latitude,longitude);  **THINK THE ERROR IS HERE**
        parkingobject.put("Location", point);

        // Show the current location in Google Map
        mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));

        // Zoom in the Google Map
        //LatLng myCoordinates = new LatLng(latitude, longitude);
        //CameraUpdate yourLocation = CameraUpdateFactory.newLatLngZoom(myCoordinates, 20);
        //mMap.animateCamera(yourLocation);
      //  mMap.animateCamera(CameraUpdateFactory.zoomTo(20));
        //mMap.addMarker(new MarkerOptions().position(new LatLng(latitude, longitude)).title("You are here!").snippet("Consider yourself located"));




        LatLng fromLatLng = latLng;
        start = latLng;


        Routing routing = new Routing(Routing.TravelMode.DRIVING);
        routing.registerListener(this);
        routing.execute(start, end);
    }
}








    //map = fm.getMap();






@Override
public void onRoutingFailure() {
    // The Routing request failed
}

@Override
public void onRoutingStart() {
    // The Routing Request starts
}

@Override
public void onRoutingSuccess(PolylineOptions mPolyOptions, Route route) {
    PolylineOptions polyOptions = new PolylineOptions();
    polyOptions.color(Color.BLUE);
    polyOptions.width(10);
    polyOptions.addAll(mPolyOptions.getPoints());
    mMap.addPolyline(polyOptions);

    // Start marker
    MarkerOptions options = new MarkerOptions();
    options.position(start);
    //options.icon(BitmapDescriptorFactory.fromResource(R.drawable.home));
    mMap.addMarker(options);

    // End marker
    options = new MarkerOptions();
    options.position(end);
    options.icon(BitmapDescriptorFactory.fromResource(R.drawable.cars));
    mMap.addMarker(options);
}

 }

有人可以指导我朝正确的方向发展吗?提前谢谢。

2 个答案:

答案 0 :(得分:1)

主要问题是您正在使用getSupportFragmentManager()并且您未在xml布局中使用SupportMapFragment。

class="com.google.android.gms.maps.MapFragment"替换class="com.google.android.gms.maps.SupportMapFragment"使其对我有用。

xml布局(注意我只使用drawable/common_signin_btn_icon_dark作为按钮图像):

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MapsActivity" >

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


    <ImageButton
        android:id="@+id/theimagebutton"
        android:text="button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/common_signin_btn_icon_dark"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"/>
</RelativeLayout>

Android Studio生成的默认代码:

public class MapsActivity extends FragmentActivity {

    private GoogleMap mMap;

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

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

    private void setUpMapIfNeeded() {
        // Do a null check to confirm that we have not already instantiated the map.
        if (mMap == null) {
            // Try to obtain the map from the SupportMapFragment.
            mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
                    .getMap();
            // Check if we were successful in obtaining the map.
            if (mMap != null) {
                setUpMap();
            }
        }
    }

    private void setUpMap() {
        mMap.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title("Marker"));
    }
}

结果:

enter image description here

答案 1 :(得分:0)

正如@Evan Bashir在评论中指出的那样,这个问题与您的布局相关的

您应该使用onMapReady()回调来获取GoogleMap对象的句柄。

首先实现对Activity的回调:

public class MainActivity extends FragmentActivity
    implements RoutingListener, OnMapReadyCallback {

找到您的MapFragment并在onCreate()中设置回调:

MapFragment mapFragment = (MapFragment) getFragmentManager()
    .findFragmentById(R.id.map);
mapFragment.getMapAsync(this);

最后在MainActivity中实现onMapReady():

@Override
public void onMapReady(GoogleMap map) {
    mMap = map;
    setUpMap();
}

当地图准备好使用时会触发此回调,因此您不会获得null。