English 中文(简体)
我的所在地在拖拉谷歌地图左边和右边时也走。
原标题:My location also moves when dragging the Google Map left and right

我开发了日托申请,因此,我想在谷歌地图上显示日托地点。 我将谷歌地图与React应用程序结合起来。 在装上申请时,正确显示地点。 然而,在拖拉或失眠时,我的所在地也随之移动。 地点应保持不变。 我是用No.js作为背后方表示的。

Here s a video of the issue

https://app.crcastification.com/v3/watch/DltGBIYDwrZY0xlJw6v9

This is my code

import React, { useState, useEffect } from "react";
import GoogleMapReact from  google-map-react ;
import { Icon } from "@iconify/react";
import locationIcon from "@iconify/icons-mdi/map-marker";

const LocationPin = ({ name }) => (
    <div className="pin">
        <Icon icon={locationIcon} className="pin-icon" />
        <p className="pin-text">{name}</p>
    </div>
);

const Map = ({ daycares }) => {
    const [latitude, setLatitude] = useState(43.77887);
    const [longitude, setLongitude] = useState(-79.17282);

    useEffect(() => {
        navigator.geolocation.getCurrentPosition(
            (position) => {
                setLatitude(position.coords.latitude);
                setLongitude(position.coords.longitude);
            },
            (error) => {
                console.error(error);
            }
        );
    }, []); // Empty dependency array means this effect runs only once after the initial render

    const renderMap = () => {
        if (!daycares || daycares.length === 0) {
            return null; // Return early if daycares is empty or undefined
        }

        return (
            <div style={{ height: "80vh", width: "100%" }}>
                <GoogleMapReact
                    bootstrapURLKeys={{ key: "AIzaSyAzEQgX_hi-_Qnv6aWWIQDAdcLYnFPqQSQ" }}
                    defaultCenter={{
                        lat: latitude,
                        lng: longitude
                    }}
                    defaultZoom={12}
                >
                    {daycares.map((daycare) => (
                        <LocationPin
                            key={daycare.id}
                            lat={parseFloat(daycare.latitude)}
                            lng={parseFloat(daycare.longitude)}
                            name={daycare.childcare_name}
                        />
                    ))}
                </GoogleMapReact>
            </div>
        );
    };

    return (
        <div>
            {renderMap()}
        </div>
    );
};

export default Map;

const LocationPin = ({ lat, lng, name }) => (
  <div className="pin" style={{ position:  absolute , transform:  translate(-50%, -50%)  }}>
    <Icon icon={locationIcon} className="pin-icon" />
    <p className="pin-text">{name}</p>
  </div>
);

export default LocationPin;

所在地始终应当保持不变。

My mock Data, {childcare_name: East Side Preschool , address: 458 Fairall Street , city: Ajax , region: Ontario , postalcode: L1S 1R6 , country: CANADA , latitude: "43.75991", longitude:"-79.18827", owner_name: Jessica Lee , age_range: 2-6 , years: 4 , infant_capacity: 8 , toddler_capacity: 12 , preschool_capacity: 18 , contact_phone: 555-9012 , contact_email: [email protected] }, { childcare_name: "Kepalen Angelic Child Care Daycare", address: "1301 Neilson Rd", city: "Scarborough", region: "Ontario", postalcode: "M1B 3C2", country: "Canada", latitude: "43.80716", longitude: "-79.21856", owner_name: "Nabhila", age_range: "0-6", years: "3", infant_capacity: "15", toddler_capacity: "20", preschool_capacity: "25", contact_phone: "555-1357", contact_email: "[email protected]" }

问题回答

正如我就你的问题发表评论时所建议的那样,你应当只使用一个不同的图书馆,支持。 提 交 人 标记使你能够把标记装上光彩的标记。

在这种情况下,我建议你使用@vis.gl/react-google-maps及其各自的 Part。

为了实施这一计划,你应紧跟上述图书馆的文件,说明如何装载地图和先进的标识。

然后用你的数据使用array.map(。 可以通过你的数据加以利用,将这些数据输入你的高级市场组成部分。 你们的法典应当这样看:

import React from "react";
import { createRoot } from "react-dom/client";

import { AdvancedMarker, APIProvider, Map } from "@vis.gl/react-google-maps";

const API_KEY =
  globalThis.GOOGLE_MAPS_API_KEY ?? (process.env.GOOGLE_MAPS_API_KEY as string);

const App = () => {
  var childCareData = [
    {
      childcare_name: "East Side Preschool",
      address: "458 Fairall Street",
      city: "Ajax",
      region: "Ontario",
      postalcode: " L1S 1R6",
      country: "CANADA",
      latitude: "43.75991",
      longitude: "-79.18827",
      owner_name: "Jessica Lee",
      age_range: "2-6",
      years: "4",
      infant_capacity: "8",
      toddler_capacity: "12",
      preschool_capacity: "18",
      contact_phone: "555-9012",
      contact_email: "[email protected]",
    },
    {
      childcare_name: "Kepalen Angelic Child Care Daycare",
      address: "1301 Neilson Rd",
      city: "Scarborough",
      region: "Ontario",
      postalcode: "M1B 3C2",
      country: "Canada",
      latitude: "43.80716",
      longitude: "-79.21856",
      owner_name: "Nabhila",
      age_range: "0-6",
      years: "3",
      infant_capacity: "15",
      toddler_capacity: "20",
      preschool_capacity: "25",
      contact_phone: "555-1357",
      contact_email: "[email protected]",
    },
  ];
  return (
    <APIProvider apiKey={API_KEY} libraries={["marker"]}>
      <Map
        mapId={"bf51a910020fa25a"}
        defaultZoom={12}
        defaultCenter={{ lat: 43.77887, lng: -79.17282 }}
        gestureHandling={"greedy"}
        disableDefaultUI
      >
        {childCareData &&
          childCareData.map((childCare, key) => {

            /* Convert your longitude and latitude into a float first
             since your data stored it as a string. */

            const floatLat = parseFloat(childCare.latitude);
            const floatLng = parseFloat(childCare.longitude);
            return (
              <AdvancedMarker
                key={key}
                position={{ lat: floatLat, lng: floatLng }}
                title={"AdvancedMarker with custom html content."}
              >
                <div
                  style={{
                    width: 16,
                    height: 16,
                    position: "absolute",
                    background: "#1dbe80",
                    border: "2px solid #0e6443",
                    borderRadius: "50%",
                    transform: "translate(-50%, -50%)",
                  }}
                >
                  <h3
                    style={{
                      padding: 12,
                    }}
                  >
                    {childCare.childcare_name}
                  </h3>
                </div>
              </AdvancedMarker>
            );
          })}
      </Map>
    </APIProvider>
  );
};

export default App;

export function renderToDom(container: HTMLElement) {
  const root = createRoot(container);

  root.render(
    <React.StrictMode>
      <App />
    </React.StrictMode>,
  );
}

Then this should work as expected without the issue of locations following around as you move the map.

Map with marker sample

Here s a proof of concept codesandbox that you can try out and check yourself.





相关问题
what is wrong with this mysql code

$db_user="root"; $db_host="localhost"; $db_password="root"; $db_name = "fayer"; $conn = mysqli_connect($db_host,$db_user,$db_password,$db_name) or die ("couldn t connect to server"); // perform query ...

Users asking for denormalized database

I am in the early stages of developing a database-driven system and the largest part of the system revolves around an inheritance type of relationship. There is a parent entity with about 10 columns ...

Easiest way to deal with sample data in Java web apps?

I m writing a Java web app in my free time to learn more about development. I m using the Stripes framework and eventually intend to use hibernate and MySQL For the moment, whilst creating the pages ...

join across databases with nhibernate

I am trying to join two tables that reside in two different databases. Every time, I try to join I get the following error: An association from the table xxx refers to an unmapped class. If the ...

How can I know if such value exists in database? (ADO.NET)

For example, I have a table, and there is a column named Tags . I want to know if value programming exists in this column. How can I do this in ADO.NET? I did this: OleDbCommand cmd = new ...

Convert date to string upon saving a doctrine record

I m trying to migrate one of my PHP projects to Doctrine. I ve never used it before so there are a few things I don t understand. In my current code, I have a class similar to this: class ...

热门标签