简体   繁体   English

谷歌地图标记大小与反应

[英]Google maps marker size with react

I am currently working on a google-maps project using google-maps-react and I have come across a problem.我目前正在使用 google-maps-react 进行 google-maps 项目,但遇到了一个问题。 I am using props to get the values of my marker (lat, lng, marker icon) however I am having trouble changing the size of the marker.我正在使用道具来获取我的标记的值(纬度、经度、标记图标),但是我无法更改标记的大小。 I am left with an enormous marker and not able to change this.我留下了一个巨大的标记,无法改变这一点。 I have tries to pass the size as a prop but without success.我试图将尺寸作为道具传递,但没有成功。
Thank you in advance for any help.预先感谢您的任何帮助。

Here is my code:这是我的代码:

MAPS.JS MAPS.JS

render() {
    /*--- initial code that i passed into props*/
     /*var icon = {
     url: "https://loc8tor.co.uk/wp-content/uploads/2015/08/stencil.png", // url
    scaledSize: new this.props.google.maps.Size(90, 42), // scaled size
    */
     }
 return (
      <div className="map-container">
        <Map
          google={this.props.google}
          className={"map"}
          zoom={4}
          initialCenter={this.props.center}
        >
          {this.props.places.map((place, i) => {
              //pass an array of markers as a child to the GoogleMap component and map over them
            return (
              <Marker
                onClick={this.onMarkerClick}
                key={place.id}
                place_={place}
                position={{ lat: place.lat, lng: place.lng }}
                icon={place.url}
                //Size={place.scaledSize} : Not working
              />
            );
          })}
          <InfoWindowEx
            marker={this.state.activeMarker}
            visible={this.state.showingInfoWindow}
          >
            <div>
              <h3>{this.state.selectedPlace.name}</h3>
              <button
                type="button"
                onClick={this.showDetails.bind(this, this.state.selectedPlace)}
              >

                Show details
              </button>
            </div>
          </InfoWindowEx>
        </Map>
      </div>
    );

INDEX.JS索引.JS

import React from "react";
import ReactDOM from "react-dom";
import Map from "./Map";

import "./styles.css";

const data = [
  {
    name: "Hello",
    title: "Sydney",
    lat: -33.847927,
    lng: 150.6517938,
    id: 1,
    color: "blue",
    url: "https://loc8tor.co.uk/wp-content/uploads/2015/08/stencil.png",
    // scaledSize: Size(90, 42), // scaled size - Not working//
     },
  {

The scaledSize parameter must be added to your marker's icon property along with the url parameter as per the library's docs and Google's docs .根据库的文档和 Google 的文档,必须将scaledSize参数与url参数一起添加到标记的icon属性中。

So if you have this object containing both parameters:因此,如果您的 object 包含两个参数:

var icon = {
  url: "https://loc8tor.co.uk/wp-content/uploads/2015/08/stencil.png",
  scaledSize: new this.props.google.maps.Size(90, 42)
};

Pass them both to your marker's icon property together as an object as follows:将它们作为 object 一起传递给标记的icon属性,如下所示:

<Marker
  onClick={this.onMarkerClick}
  key={place.id}
  place_={place}
  position={{ lat: place.lat, lng: place.lng }}
  icon={icon}
/>

I have tested your code with the above changes and your blue marker displays correctly at the specified size.我已经使用上述更改测试了您的代码,并且您的蓝色标记以指定的大小正确显示。 So hope this helps you!所以希望这对你有帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM