android componentname 百度地图,react native android百度地图定位

使用百度地图进行定位,我选择的插件是:react-native-baidu-map

github地址:https://github.com/lovebing/react-native-baidu-map

样例:

7ca4d7acb6d2

图片.png

此处我只写了配置Android的教程,需要ios配置的可以移步至:http://www.jianshu.com/p/eceb7e66fa5e ,作者写的很详细。

配置步骤:

1、导入

npm install react-native-baidu-map --save

2、link

react-native link react-native-baidu-map

3、配置

有时候link可能没起作用,或者link只自动配置了一半而没有配全,所以我们需要手动去检查一些是否都配置全了,去过没有配置全,还需手动去配置。

setting.gradle

include ':react-native-baidu-map' project(':react-native-baidu-map').projectDir = new File(settingsDir, '../node_modules/react-native-baidu-map/android’)

7ca4d7acb6d2

setting.gradle.png

App/build.gradle

compile project(':react-native-baidu-map')

7ca4d7acb6d2

build.gradle .png

MainApplication

new BaiduMapPackage(getApplicationContext())

7ca4d7acb6d2

图片.png

除此之外,别忘了将包导进来:不过一般都会自动导进来。

MainApplication

import org.lovebing.reactnative.baidumap.BaiduMapPackage;

7ca4d7acb6d2

图片.png

AndroidMainifest.xml

开启权限:

android:value=“你所申请的百度地图AK”/>

7ca4d7acb6d2

图片.png

申请百度地图AK可参考:http://www.jianshu.com/p/2e2c562a2275

现在所有的配置都已经完成了,我们就可以进行百度地图定位了。

BaiduMap.js

import React, {

Component,

PropTypes

} from 'react';

import {

MapView,

MapTypes,

Geolocation

} from 'react-native-baidu-map';

import {

AppRegistry,

StyleSheet,

Text,

View,

TouchableHighlight

} from 'react-native';

import styles from './styles';

export default class BaiduMap extends Component {

constructor() {

super();

this.state = {

mayType: MapTypes.NORMAL,

zoom: 15,

trafficEnabled: false,

baiduHeatMapEnabled: false,

};

}

componentDidMount() { // 获取位置

Geolocation.getCurrentPosition().then(

(data) => {

this.setState({

zoom:18,

markers:[{

latitude:data.latitude,

longitude:data.longitude,

title:'我的位置'

}],

center:{

latitude:data.latitude,

longitude:data.longitude,

}

})

}

).catch(error => {

console.warn(error,'error')

})

}

render() {

return (

trafficEnabled={this.state.trafficEnabled}

baiduHeatMapEnabled={this.state.baiduHeatMapEnabled}

zoom={this.state.zoom}

mapType={this.state.mapType}

center={this.state.center}

marker={this.state.marker}

markers={this.state.markers}

style={styles.map}

onMapClick={(e) => {

}}

>

);

}

}

styles.js

'use strict';

import { StyleSheet } from "react-native";

module.exports = StyleSheet.create({

container: {

flex: 1,

height:400,

flexDirection:'row',

justifyContent: 'flex-start',

alignItems: 'center',

backgroundColor: '#F5FCFF',

},

map: {

flex:1,

height:400,

}

});

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值