react-google-maps icon indicating copy to clipboard operation
react-google-maps copied to clipboard

How can I use custom marker (png file)

Open webdeveloper0429 opened this issue 9 years ago • 15 comments

Is there any simple props to use custom marker? How can I use custom marker?

webdeveloper0429 avatar Apr 30 '17 22:04 webdeveloper0429

https://developers.google.com/maps/documentation/javascript/reference#MarkerOptions

Type: string|Icon|Symbol Icon for the foreground. If a string is provided, it is treated as though it were an Icon with the string as url.

<Marker options={{icon: 'your_icon_url}} >

should work :)

MathieuCh avatar May 04 '17 13:05 MathieuCh

Thanks @MathieuCh would you clarify more option about resizing image I have use both of png and ico (png size is large)

webdeveloper0429 avatar May 04 '17 13:05 webdeveloper0429

https://developers.google.com/maps/documentation/javascript/reference#Icon

scaledSize : The size of the entire image after scaling, if any. Use this property to stretch/shrink an image or a sprite.

size : The display size of the sprite or image. When using sprites, you must specify the sprite size. If the size is not provided, it will be set when the image loads.

<Marker options={{ icon: { url: ...., scaledSize: ...., size: .... } }} >

MathieuCh avatar May 04 '17 20:05 MathieuCh

+1 @MathieuCh

Thanks very much

webdeveloper0429 avatar May 04 '17 20:05 webdeveloper0429

Hey @MathieuCh ,

I know you gave the answer above on how to scale the images/icons but you didn't specify the value type of the scaledSize and size

Google documentation states that you use new google.maps.size(45,56) I have tried the same on react-google-maps but I get google is not defined yet I have used it with withScripts. What do I pass? Is it a string, Object or other.

joeynimu avatar Jun 13 '18 14:06 joeynimu

@joeynimu use window.google.maps ... works for me

CodingToBeWithHer avatar Jul 03 '18 00:07 CodingToBeWithHer

This is how i fixe that

let iconMarker = new window.google.maps.MarkerImage(
                    url,
                    null, /* size is determined at runtime */
                    null, /* origin is 0,0 */
                    null, /* anchor is bottom center of the scaled image */
                    new window.google.maps.Size(32, 32)
                );
                return(
                    <Marker
                        icon={iconMarker}
                        key={marker.id}
                        onClick={onClick}
                        position={{ lat: parseInt(marker.latitude), lng: parseInt(marker.longitude)}}>
                        {props.selectedMarker === marker &&
                        <InfoWindow>
                            <div style={{'color':'black'}}>
                               Infowindow texte
                            </div>
                        </InfoWindow>}
                    </Marker>
                )

fabriceyopa avatar Jul 03 '18 09:07 fabriceyopa

Actually, resizing a marker is a lot easier than the post above.

const position = { lat: 52, lng: 6 };
const icon = { url: 'https://...', scaledSize: { width: 32, height: 32 } };
  
<Marker position={position} icon={icon} />

By using scaledSize and providing a object with a width and height (value in px), we don't need to call globals and/or constructors.

smeijer avatar Aug 30 '18 15:08 smeijer

Plus, if you want to specify a local image:

icon: {url: require("./pin.png"), scaledSize: {width: 32, height: 32}}

About icon prop, Google documentation says:

Icon for the foreground. If a string is provided, it is treated as though it were an Icon with the string as url.

See more on documentation.

velthune avatar Sep 17 '19 09:09 velthune

@velthune The repo of this project is unmaintained more than a year, and we had build new version https://www.npmjs.com/package/@react-google-maps/api

We had rewrite it to TypeScript, and updating it frequently: https://github.com/JustFly1984/react-google-maps-api/tree/master/packages/react-google-maps-api You can enjoy autocomplete.

You can see our docs: https://react-google-maps-api-docs.netlify.com/

Also a lot of examples: https://react-google-maps-api-gatsby-demo.netlify.com/ https://github.com/JustFly1984/react-google-maps-api/tree/master/packages/react-google-maps-api-gatsby-example/src/examples

The bundle size is much smaller: https://bundlephobia.com/result?p=@react-google-maps/[email protected]

Enjoy!

JustFly1984 avatar Sep 17 '19 10:09 JustFly1984

@JustFly1984 thank you for the tip, but I'm using a theme that comes with:

"react-google-maps": "^9.4.5"

Do you suggest to refactor to the newest version?

velthune avatar Sep 17 '19 15:09 velthune

@velthune Can you point me to the theme files? Maybe I could add that theme to my lib

JustFly1984 avatar Sep 17 '19 19:09 JustFly1984

I would suggest to refactor to my library, we are working to improve it constantly

JustFly1984 avatar Sep 17 '19 19:09 JustFly1984

Sorry for the late, I'm using ArchitectUI template. Well, I'll refactor as soon as possibile! 👌

velthune avatar Sep 19 '19 09:09 velthune

Plus, if you want to specify a local image:

icon: {url: require("./pin.png"), scaledSize: {width: 32, height: 32}}

About icon prop, Google documentation says:

Icon for the foreground. If a string is provided, it is treated as though it were an Icon with the string as url.

See more on documentation.

the require is most important for local png file. Thanks

ajay-bsoft avatar Feb 05 '20 19:02 ajay-bsoft