将标记添加到onClick上的位置时应用崩溃

时间:2017-05-31 22:40:50

标签: android google-maps google-maps-markers

我尝试将Marker添加到我从EditText输入纬度和经度的位置,这个我的代码我不知道为什么应用程序崩溃。

public class MainActivity extends AppCompatActivity implements OnMapReadyCallback {
private TextView source;
private TextView destination;
private EditText sLatitude1;
private EditText sLongtiude1;
private EditText dLatitude2;
private EditText dLongtiude2;
private Button button;

 private GoogleMap mMap;
boolean mapReady = false;
MarkerOptions elsedaway;
MarkerOptions Elrob3;
Location location;


static final CameraPosition elfayoum = CameraPosition.builder()
        .target(new LatLng(29.309324, 30.842973))
        .zoom(1)
        .bearing(6)
        .tilt(45)
        .build();
double lati1;
double longi1;
double lati2;
double longi2;
TextView textView;
   @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // find text that display distance
    textView = (TextView) findViewById(R.id.distance);
    MapFragment mapFragment = (MapFragment) getFragmentManager().findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);
    // find edit text and text view
    source = (TextView) findViewById(R.id.sourc);
    destination = (TextView) findViewById(R.id.destination);
    sLatitude1 = (EditText) findViewById(R.id.lat1);
    sLongtiude1 = (EditText) findViewById(R.id.long1);
    dLatitude2 = (EditText) findViewById(R.id.lat2);
    dLongtiude2 = (EditText) findViewById(R.id.long2);
    // find button
    button = (Button) findViewById(R.id.getDistance);

    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // find string from edittext
            String lat1 = sLatitude1.getText().toString();
            // parse string to double
            lati1 = ParseDouble(lat1);
            String lon1 = sLongtiude1.getText().toString();
            longi1 = ParseDouble(lon1);
            String lat2 = dLatitude2.getText().toString();
            lati2 = ParseDouble(lat2);
            String lon2 = dLongtiude2.getText().toString();
            longi2 = ParseDouble(lon2);
            Log.i("**lat", lat2);

            double xy1 = distanceBetween(new LatLng(lati1, longi1), new LatLng(lati2, longi2));
            String distanceis = fmt(xy1) + "meter";
            textView.setText(distanceis);

// 这里我添加标记

            elsedaway= new MarkerOptions().position(new LatLng(lati1,longi1))
                    .title("place").icon(BitmapDescriptorFactory.fromResource(R.drawable.bubble_mask));

            mMap.addPolyline(new PolylineOptions().geodesic(true)
                    .add(new LatLng(lati1, longi1))
                    .add(new LatLng(lati2, lati2)));
            mMap.addCircle(new CircleOptions()
                    .center(new LatLng(lati1, longi1))
                    .radius(5000)
                    .strokeColor(Color.GREEN)
                    .fillColor(Color.argb(54, 99, 255, 0)));
        }

    });


}

double ParseDouble(String strNumber) {
    if (strNumber != null && strNumber.length() > 0) {
        try {
            return Double.parseDouble(strNumber);
        } catch (Exception e) {
            return -1;   // or some value to mark this field is wrong. or make a function validates field first ...
        }
    } else return 0;
}

// get distance
public static Double distanceBetween(LatLng point1, LatLng point2) {
    if (point1 == null || point2 == null) {
        return null;
    }
    double vw = SphericalUtil.computeDistanceBetween(point1, point2);
    Log.i("distance isby utillib ", String.valueOf(vw));
    return vw;
}
public String fmt(double d) {

    return String.format("%s", d);
}

@Override
public void onMapReady(GoogleMap googleMap) {
    mapReady = true;
    mMap = googleMap;
      if(elsedaway!=null){
       mMap.addMarker(elsedaway);};
   mMap.addMarker(Elrob3);

    flyTo(elfayoum);
}

我还在我的onClick需要帮助中添加摄像头位置。

0 个答案:

没有答案