oneframe-react
Version:
Oneframe React ## Components, Hooks, Helper Functions & State Management
75 lines (74 loc) • 3.04 kB
JavaScript
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
result["default"] = mod;
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
const react_1 = __importStar(require("react"));
const withMap_1 = require("./withMap");
const _utility_1 = require("./_utility");
const InforObj = [];
const markerCount = [];
const MarkerComponent = react_1.memo((props) => {
const { position = props.mapCenter || { lat: 0, lng: 0 }, address, onChange, clickable, draggable, content, map, setMarkerLatLng, } = props;
const [marker, setMarker] = react_1.useState();
const handleEvent = (e) => {
onChange && onChange({ lat: e.latLng.lat(), lng: e.latLng.lng() });
};
const closeOtherInfo = () => {
if (InforObj.length > 0) {
InforObj[0].set('marker', null);
InforObj[0].close();
InforObj.length = 0;
}
};
react_1.useEffect(() => {
if (map) {
const markerObject = new google.maps.Marker({
map,
draggable,
clickable,
});
setMarker(markerObject);
markerCount.push(markerObject);
markerObject.addListener('dragend', handleEvent);
if (content && clickable) {
const infoObject = new google.maps.InfoWindow({
content: content,
});
markerObject.addListener('click', function () {
closeOtherInfo();
infoObject.open(map, markerObject);
InforObj[0] = infoObject;
});
}
/*if (address) {
const geocoder:google.maps.Geocoder = new google.maps.Geocoder();
geocoder.geocode({ address }, function(results, status) {
if (status === 'OK') {
map.panTo(results[0].geometry.location);
map.setCenter(results[0].geometry.location);
markerObject.setPosition(results[0].geometry.location);
handleEvent({ latLng: results[0].geometry.location });
} else {
console.error('Address Not Found');
}
});
}*/
}
// eslint-disable-next-line
}, [map, address, clickable, content, draggable]);
react_1.useEffect(() => {
if (marker) {
if (_utility_1.isCoordinate(position)) {
marker.setPosition(position);
setMarkerLatLng && setMarkerLatLng(position, markerCount.length);
}
}
}, [position, marker, setMarkerLatLng]);
return react_1.default.createElement(react_1.default.Fragment, null);
});
exports.default = withMap_1.withMap(MarkerComponent);
;