Guest User

Untitled

a guest
Apr 1st, 2024
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.20 KB | None | 0 0
  1. "use client";
  2.  
  3. import React from "react";
  4. import { useState, useRef } from "react";
  5. import {
  6. APIProvider,
  7. Map,
  8. AdvancedMarker,
  9. Pin,
  10. InfoWindow,
  11. MapMouseEvent,
  12. } from "@vis.gl/react-google-maps";
  13.  
  14. import { Button } from "@/components/ui/button";
  15. import { Textarea } from "@/components/ui/textarea";
  16. import { Input } from "@/components/ui/input";
  17. import { Label } from "@/components/ui/label";
  18.  
  19. import {
  20. Dialog,
  21. DialogClose,
  22. DialogContent,
  23. DialogDescription,
  24. DialogHeader,
  25. DialogTitle,
  26. DialogTrigger,
  27. } from "@/components/ui/dialog";
  28.  
  29. interface MarkerCoords {
  30. lat: number;
  31. lng: number;
  32. }
  33.  
  34. const api_key = process.env.NEXT_PUBLIC_GOOGLE_MAPS_API_KEY || "";
  35. const mapId = process.env.NEXT_PUBLIC_MAP_ID;
  36.  
  37. export function MapComp() {
  38. const [markers, setMarkers] = useState<MarkerCoords[]>([]);
  39. const [message, setMessage] = useState<string>("");
  40. const [link, setLink] = useState<string>("");
  41. const [dialogOpen, setDialogOpen] = useState(false); // State for controlling dialog visibility
  42.  
  43.  
  44. const [selectedMarker, setSelectedMarker] = useState<MarkerCoords | null>(
  45. null
  46. );
  47.  
  48. const onMapClick = (event: MapMouseEvent) => {
  49. if (!dialogOpen) {
  50. if (event.detail.latLng) {
  51. const { lat, lng } = event.detail.latLng;
  52. if (lat !== undefined && lng !== undefined) {
  53. const newMarker = {
  54. lat: lat,
  55. lng: lng,
  56. };
  57. setSelectedMarker(newMarker);
  58. setMarkers((current) => [...current, newMarker]);
  59. setDialogOpen(true);
  60. }
  61. }
  62. }
  63. };
  64.  
  65. const handlePost = () => {
  66. console.log("Message:", message);
  67. console.log("Link:", link);
  68. };
  69.  
  70. return (
  71. <APIProvider apiKey={api_key}>
  72. <div style={{ height: "100vh", width: "100vw" }}>
  73. <Map
  74. defaultZoom={8}
  75. defaultCenter={{ lat: 53.54, lng: 10 }}
  76. minZoom={3}
  77. mapId={mapId}
  78. mapTypeControl={false}
  79. streetViewControl={false}
  80. clickableIcons={false}
  81. fullscreenControl={false}
  82. onClick={onMapClick}
  83. restriction={{
  84. latLngBounds: {
  85. north: 85,
  86. south: -85,
  87. west: -179.9999,
  88. east: 179.9999,
  89. },
  90. strictBounds: true,
  91. }}
  92. >
  93. {markers.map((marker, index) => (
  94. <AdvancedMarker
  95. key={index}
  96. position={{ lat: marker.lat, lng: marker.lng }}
  97. onClick={() => handleToggleInfoWindow(index)}
  98. >
  99. <Pin
  100. background={"black"}
  101. borderColor={"black"}
  102. glyphColor={"black"}
  103. />
  104. ...
  105. >
  106. Post
  107. </Button>
  108. )}
  109. </div>
  110. </InfoWindow>
  111. )}
  112. </AdvancedMarker>
  113. ))}
  114. <Dialog open={dialogOpen} onOpenChange={setDialogOpen}>
  115. <DialogContent>
  116. <DialogHeader>
  117. <DialogTitle>Map</DialogTitle>
  118. <DialogDescription>
  119. {selectedMarker?.lat.toFixed(3) +
  120. " °N " +
  121. selectedMarker?.lng.toFixed(3) +
  122. " °W"}
  123. </DialogDescription>
  124. <DialogClose asChild>Button</DialogClose>
  125. </DialogHeader>
  126. <div className="grid gap-4 py-4">
  127. <div className="grid grid-cols-4 items-center gap-4">
  128. <Label>Message</Label>
  129. <Input
  130. style={{ zIndex: 99 }}
  131. id="message"
  132. placeholder="Post."
  133. type="string"
  134. className="col-span-3"
  135. defaultValue={message}
  136. onChange={(e) => setMessage(e.target.value)}
  137. />
  138. </div>
  139. ...
  140. </div>
  141. <Button onClick={() => handlePost()}>Post</Button>
  142. </DialogContent>
  143. </Dialog>
  144. </Map>
  145. </div>
  146. </APIProvider>
  147. );
  148. }
Advertisement
Add Comment
Please, Sign In to add comment