private Boolean updateMarkers(ReadableArray markerArray) {
    try {
      // First clear all markers from the map
      for (Marker marker : mapMarkers) {
        marker.remove();
      }
      mapMarkers.clear();
      markerLookup.clear();

      // All markers to map
      for (int i = 0; i < markerArray.size(); i++) {
        ReadableMap markerJson = markerArray.getMap(i);
        if (markerJson.hasKey("coordinates")) {
          Marker marker = map.addMarker(createMarker(markerJson));

          if (markerJson.hasKey("id")) {
            // As we have to lookup it either way, switch it around
            markerLookup.put(marker.getId(), markerJson.getString("id"));
            markerLookup.put(markerJson.getString("id"), marker.getId().replace("m", ""));
          }

          mapMarkers.add(marker);

        } else break;
      }

      return true;
    } catch (Exception e) {
      e.printStackTrace();
      return false;
    }
  }
  private Boolean zoomOnMarkers() {
    try {
      int padding = 150;

      LatLngBounds.Builder builder = new LatLngBounds.Builder();
      for (Marker marker : mapMarkers) {
        builder.include(marker.getPosition());
      }
      LatLngBounds bounds = builder.build();

      CameraUpdate cu = CameraUpdateFactory.newLatLngBounds(bounds, padding);
      map.animateCamera(cu);
      return true;
    } catch (Exception e) {
      e.printStackTrace();
      return false;
    }
  }
  private Boolean updateCenter() {
    WritableMap properties = getProperties();
    if (properties.hasKey(PROP_CENTER)) {
      try {
        CameraUpdate cameraUpdate;

        Double lng = properties.getMap(PROP_CENTER).getDouble("lng");
        Double lat = properties.getMap(PROP_CENTER).getDouble("lat");

        if (properties.hasKey(PROP_ZOOM_LEVEL)) {
          int zoomLevel = properties.getInt(PROP_ZOOM_LEVEL);
          mlastZoom = zoomLevel;
          Log.i(REACT_CLASS, "Zoom: " + Integer.toString(properties.getInt(PROP_ZOOM_LEVEL)));
          cameraUpdate = CameraUpdateFactory.newLatLngZoom(new LatLng(lat, lng), zoomLevel);
        } else {
          Log.i(REACT_CLASS, "Default Zoom.");
          /*
           * Changed from cameraUpdate = CameraUpdateFactory.newLatLng(new LatLng(lat, lng));
           * as it gave me "zoom" Bugs (defaulted to zoom factor 2) as soon as I put in
           * "real" LNG and LAT values...
           */
          cameraUpdate = CameraUpdateFactory.newLatLngZoom(new LatLng(lat, lng), mlastZoom);
        }

        map.animateCamera(cameraUpdate);

        return true;
      } catch (Exception e) {
        // ERROR!
        e.printStackTrace();
        return false;
      }
    } else {
      return false;
    }
  }
Пример #4
0
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.position);
    GoogleMap map =
        ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
    final Geocoder geocoder = new Geocoder(this, Locale.getDefault());
    final Button submit = (Button) findViewById(R.id.position_submit_button);
    submit.setEnabled(false);
    userProfile = ProfileManager.getInstance();

    Toast.makeText(getApplicationContext(), "Press and Hold Pin to Move", Toast.LENGTH_LONG).show();

    LatLng pgh = new LatLng(40.441814, -80.012794);
    map.moveCamera(CameraUpdateFactory.newLatLngZoom(pgh, 13));
    locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);

    if (locationManager != null) {
      boolean gpsIsEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
      boolean networkIsEnabled =
          locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
      if (gpsIsEnabled) {
        Log.d("GPS Enabled", "GPS Enabled");
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5000L, 10F, this);
        Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
        if (location != null) {
          pgh = new LatLng(location.getLatitude(), location.getLongitude());
        }
        map.moveCamera(CameraUpdateFactory.newLatLngZoom(pgh, 16));
        myIncident.setLat(Double.toString(pgh.latitude));
        myIncident.setLon(Double.toString(pgh.longitude));
        List<Address> addresses = null;
        try {
          addresses = geocoder.getFromLocation(pgh.latitude, pgh.longitude, 1);
          myIncident.setAddress(addresses.get(0).getAddressLine(0));
          city = addresses.get(0).getAddressLine(1);
          if (!city.contains("Pittsburgh")) {
            submit.setEnabled(false);
          } else {
            submit.setEnabled(true);
          }
        } catch (Exception e) {
          e.printStackTrace();
        }
      } else {
        if (networkIsEnabled) {
          Log.d("Network Position Enabled", "Network Position");
          locationManager.requestLocationUpdates(
              LocationManager.NETWORK_PROVIDER, 5000L, 10F, this);
          Location location =
              locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
          if (location != null) {
            pgh = new LatLng(location.getLatitude(), location.getLongitude());
          }
          map.moveCamera(CameraUpdateFactory.newLatLngZoom(pgh, 15));
          myIncident.setLat(Double.toString(pgh.latitude));
          myIncident.setLon(Double.toString(pgh.longitude));
          List<Address> addresses = null;
          try {
            addresses = geocoder.getFromLocation(pgh.latitude, pgh.longitude, 1);
            myIncident.setAddress(addresses.get(0).getAddressLine(0));
            city = addresses.get(0).getAddressLine(1);
            if (!city.contains("Pittsburgh")) {
              submit.setEnabled(false);
            } else {
              submit.setEnabled(true);
            }
          } catch (Exception e) {
            e.printStackTrace();
          }
        }
      }

    } else {

    }

    Marker issueLoc =
        map.addMarker(
            new MarkerOptions()
                .title("Issue Location")
                .snippet("Drag to Problem Location")
                .position(pgh)
                .draggable(true));

    map.setOnMarkerDragListener(
        new GoogleMap.OnMarkerDragListener() {
          @Override
          public void onMarkerDragStart(Marker marker) {}

          @Override
          public void onMarkerDrag(Marker marker) {}

          @Override
          public void onMarkerDragEnd(Marker marker) {
            double latitude = marker.getPosition().latitude;
            double longitude = marker.getPosition().longitude;
            myIncident.setLat(Double.toString(latitude));
            myIncident.setLon(Double.toString(longitude));
            List<Address> addresses = null;
            try {
              addresses = geocoder.getFromLocation(latitude, longitude, 1);
              myIncident.setAddress(addresses.get(0).getAddressLine(0));
              city = addresses.get(0).getAddressLine(1);
              if (!city.contains("Pittsburgh")) {
                submit.setEnabled(false);
              } else {
                submit.setEnabled(true);
              }
            } catch (Exception e) {
              e.printStackTrace();
            }
          }
        });
    submit.setOnClickListener(
        new View.OnClickListener() {
          @Override
          public void onClick(View v) {
            new uploadReport().execute();
          }
        });
  }
  @Override
  public MapView createViewInstance(ThemedReactContext context) {
    reactContext = context;
    mView = new MapView(context);
    mView.onCreate(null);
    mView.onResume();
    map = mView.getMap();

    if (map == null) {
      sendMapError("Map is null", "map_null");
    } else {
      map.getUiSettings().setMyLocationButtonEnabled(true);
      map.setMyLocationEnabled(true);

      // We need to be sure to disable location-tracking when app enters background, in-case some
      // other module
      // has acquired a wake-lock and is controlling location-updates, otherwise, location-manager
      // will be left
      // updating location constantly, killing the battery, even though some other location-mgmt
      // module may
      // desire to shut-down location-services.
      LifecycleEventListener listener =
          new LifecycleEventListener() {
            @Override
            public void onHostResume() {
              map.setMyLocationEnabled(true);
            }

            @Override
            public void onHostPause() {
              map.setMyLocationEnabled(false);
            }

            @Override
            public void onHostDestroy() {}
          };

      context.addLifecycleEventListener(listener);

      try {
        MapsInitializer.initialize(context.getApplicationContext());
        map.setOnCameraChangeListener(getCameraChangeListener());
        map.setOnMarkerClickListener(getMarkerClickListener());
        // add location button click listener
        map.setOnMyLocationButtonClickListener(
            new GoogleMap.OnMyLocationButtonClickListener() {
              @Override
              public boolean onMyLocationButtonClick() {
                CameraPosition position = map.getCameraPosition();
                mlastZoom = (int) position.zoom;
                return false;
              }
            });
      } catch (Exception e) {
        e.printStackTrace();
        sendMapError("Map initialize error", "map_init_error");
      }
    }

    return mView;
  }