这是我的代码:
import React, { Component } from 'react';
import logo from './logo.svg';
import './App.css';
import fetch from 'node-fetch';
import PropTypes from 'prop-types';
import { Map, TileLayer } from 'react-leaflet';
export default class PageBackground extends Component{
constructor(props) {
super(props);
this.getLocation=this.getLocation.bind(this);
this.state={
position:[2,2]
};
}
componentWillMount() {
this.getLocation();
}
getLocation () {
fetch('http://freegeoip.net/json/')
.then((res)=>res.json())
.then((objec)=>{this.setState({position:[objec.latitude,objec.longitude]});console.log(this.state.position)})
.catch((err)=>{console.log("didn't connect to App",err);this.setState({position:['10','8']});})
}
render(){
return (
<Map center={this.state.position} zoom={13} >
<TileLayer
attribution='© <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
url='http://{s}.tile.osm.org/{z}/{x}/{y}.png'/>
</Map>
);
}
}
我的目标是让地图的高度为 100 像素,宽度为 100 像素,所以我写了这个 CSS 样式:
.leaflet-container {
height:100px;
width:100px;
}