Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to customize react-google-map 's marker with local png?

I recently try to implement tomchentw/react-google-maps, but i can't seem to figure out how to cutomize the marker icon, at default, it show a red icon, it worked fine, but when i try to pass in the icon prop, no marker were shown.

here is my code:

/* global google */
import React, { Component } from 'react';
import { MarkerWithLabel } from 'react-google- 
maps/lib/components/addons/MarkerWithLabel';
import {
  withScriptjs,
  withGoogleMap,
  GoogleMap,
  Marker,
} from 'react-google-maps';
import { compose, withProps, withHandlers } from 'recompose';


const MapWithAMarkerClusterer = compose(
  withProps({
    googleMapURL: "https://maps.googleapis.com/maps/api/js?key=AIzaSyC3naz5xCZtPlOeMo38InY3GFr4k8A2LO0&v=3.exp&libraries=geometry,drawing,places",
    loadingElement: <div style={{ height: `100%` }} />,
    containerElement: <div style={{ height: `100%` }} />,
    mapElement: <div style={{ height: `100%` }} />,
  }),
  withScriptjs,
  withGoogleMap
)(props =>
  <GoogleMap
    defaultZoom={11}
    defaultCenter={{ lat: 25.0391667, lng: 121.525 }}
  >
          <Marker
          position={{ lat: 25.0391667, lng: 121.525 }}
          icon={{
            url: 'assets/image2vector.svg',
            anchor: new google.maps.Point(5, 58),
          }}
        />   
  </GoogleMap>
);

class googleMap extends Component {

  render() {
    return (
      <MapWithAMarkerClusterer />
    )
  }
}

export default googleMap;

if i remove the icon prop, the red marker will return. But i really want to use my own local icon. How can i do that?

like image 851
ThomasH.Allen Avatar asked Dec 11 '22 05:12

ThomasH.Allen


1 Answers

You need to either import or use require directly in render to render image

Below are two ways rendering images in React.

    import vector from 'assets/image2vector.svg';


    icon={{
        url: vector,
        anchor: new google.maps.Point(5, 58),
      }}

Or use require directly

   icon={{
        url: require('assets/image2vector.svg'),
        anchor: new google.maps.Point(5, 58),
      }}
like image 84
Hemadri Dasari Avatar answered Dec 28 '22 07:12

Hemadri Dasari