All files MapComponent.tsx

77.14% Statements 27/35
38.46% Branches 5/13
50% Functions 4/8
77.14% Lines 27/35
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 1532x 2x 2x   2x   2x 2x     2x 2x 2x                                           2x   6x 6x         6x 6x 6x                       1x 1x       1x     1x 1x 1x 1x 1x 1x                                                   7x               7x                                                                                               2x  
import * as React from 'react';
import _ from 'lodash';
import { View } from 'react-native';
// @ts-ignore - missing in definition
import MapView, { PROVIDER_GOOGLE } from 'react-native-maps';
import * as types from '../../types';
import LocationService from '../../services/location-service/LocationService';
import MapService from '../../services/map-service/MapService';
import { ILatLng } from '../../types';
// @ts-ignore
import MapViewDirections from 'react-native-maps-directions';
import UserLocationButton from '../../components/user-location-button/UserLocationButton';
import { styles, Colors } from 'react-native-cross-components';
import MapViewProps from 'react-native-maps';
import { MapViewDirectionsProps, IRegion } from '../../types';
 
interface IState {
  mapMargin: number;
  userLocation?: ILatLng;
}
 
interface IProps {
  mapViewProps?: MapViewProps;
  mapNavigationProps?: MapViewDirectionsProps;
  initialRegion?: IRegion;
  destination?: ILatLng | null;
  apikey?: string;
  goToLocation?(newLocation: types.ILatLng): void;
  onRegionChange?: (newRegion: IRegion) => void;
  onUserLocationChanged?: (newLocation: ILatLng) => void;
}
 
let map: any;
 
export class MapComponent extends React.Component<IProps, IState> {
  constructor(props: IProps) {
    super(props);
    this.state = {
      mapMargin: 1,
    };
 
    // this.setMap = this.setMap.bind(this);
    this.getPredefinedPlaces = this.getPredefinedPlaces.bind(this);
    this.goToUserLocation = this.goToUserLocation.bind(this);
    this.onMapReady = this.onMapReady.bind(this);
  }
 
  public async goToLocation(coordinate: types.ILatLng) {
    if (!map) {
      return;
    }
 
    MapService.goToUserLocation(map, coordinate);
  }
 
  public async goToUserLocation() {
    console.log('goToUserLocation has map? ' + !_.isNil(map));
    Iif (!map) {
      return;
    }
 
    console.log(
      'goToUserLocation has LocationService? ' + !_.isNil(LocationService)
    );
    const coordinate = await LocationService.getCurrentLocation();
    MapService.goToUserLocation(map, coordinate);
    this.setState({ userLocation: coordinate });
    Eif (this.props.onUserLocationChanged) {
      console.log('goToUserLocation calls onUserLocationChanged');
      this.props.onUserLocationChanged(coordinate);
    }
  }
 
  /**
   * Get list of predefined places available in autocomplete
   */
  getPredefinedPlaces(): Array<Object> {
    // const { userLocation } = this.state;
    // if (!_.isNil(userLocation)) {
    //   const userPlace = {
    //     description: 'Min nuvarande plats',
    //     geometry: {
    //       location: {
    //         lat: userLocation.latitude,
    //         lng: userLocation.longitude,
    //       },
    //     },
    //   };
    //   return [userPlace];
    // }
 
    return [];
  }
 
  setMap(ref: any) {
    map = ref;
  }
 
  onMapReady() {
    this.setState({ mapMargin: 0 });
  }
 
  render() {
    return (
      <View style={styles.container}>
        <MapView
          maxZoomLevel={18}
          zoomEnabled={true}
          loadingEnabled={false}
          showsBuildings={false}
          showsUserLocation
          showsIndoors={false}
          showsMyLocationButton={false}
          toolbarEnabled={false}
          onMapReady={this.onMapReady}
          onRegionChangeComplete={(region: types.IRegion) => {
            if (!_.isNil(this.props.onRegionChange)) {
              this.props.onRegionChange(region);
            }
          }}
          loadingIndicatorColor={Colors.CrossLightBlue}
          showsCompass={false}
          ref={this.setMap}
          style={[styles.container, { marginBottom: this.state.mapMargin }]}
          provider={PROVIDER_GOOGLE}
          {...this.props.mapViewProps}
        >
          {this.state.userLocation &&
          this.props.destination &&
          this.props.destination.latitude ? (
            <MapViewDirections
              origin={this.state.userLocation}
              destination={{
                latitude: Number(this.props.destination.latitude),
                longitude: Number(this.props.destination.longitude),
              }}
              //TODO PARAMS
              apikey={this.props.apikey}
              strokeColor="blue"
              strokeWidth={3}
              {...this.props.mapNavigationProps}
            />
          ) : null}
          {this.props.children}
        </MapView>
        <UserLocationButton onPress={this.goToUserLocation} />
      </View>
    );
  }
}
 
export default MapComponent;