googleMaps出错(java.lang.IllegalArgumentException:标记选项中没有位置)

时间:2015-06-23 21:09:22

标签: android google-maps

java.lang.IllegalArgumentException:标记选项中没有位置

我有这个重复出现的问题,即没有找到我在谷歌地图上的位置。加班我启动它崩溃的应用程序。我相信这个错误存在于某个地方 onRouteSucess方法。有人可以帮我解决这个问题吗?这变得非常令人头疼。

  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;

  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("Parking");
        //parkingobject.put("username","paul");
        //ParseGeoPoint point = new ParseGeoPoint(40.663845, -73.926407);
        //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;

        LatLng toLatlng = new LatLng(40.6937, -73.9859);

        float cars = distanceBetween(fromLatLng, toLatlng);

        Toast.makeText(getApplicationContext(), "Distance between the two is:" + cars + "meters", Toast.LENGTH_LONG).show();
        Routing routing = new Routing(Routing.TravelMode.DRIVING);
        routing.registerListener(this);
        routing.execute(fromLatLng, toLatlng);
    }
}


private float distanceBetween(LatLng latLng1, LatLng latLng2) {

    Location loc1 = new Location(LocationManager.GPS_PROVIDER);
    Location loc2 = new Location(LocationManager.GPS_PROVIDER);

    loc1.setLatitude(latLng1.latitude);
    loc1.setLongitude(latLng1.longitude);

    loc2.setLatitude(latLng2.latitude);
    loc2.setLongitude(latLng2.longitude);


    return loc1.distanceTo(loc2);
}





    //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.start_blue));
    mMap.addMarker(options);

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

 }

1 个答案:

答案 0 :(得分:0)

我想也许你想改变

LatLng fromLatLng = latLng;
LatLng toLatlng = new LatLng(40.6937, -73.9859);

start = latLng;
end = new LatLng(40.6937, -73.9859);

因为我不知道你在发布的代码中为起点和终点设置值在哪里!