React原生谷歌地图



在我的react原生应用程序中。一旦用户在我的应用程序中按下按钮,我想打开标准的谷歌地图应用程序并显示特定的位置。我该怎么做

解决方案

openGps = (lat, lng) => {
var scheme = Platform.OS === 'ios' ? 'maps:' : 'geo:';
var url = scheme + `${lat},${lng}`;
Linking.openURL(url);
}

相应地通过纬度和经度

或者,如果你想用自定义标签试试这个:

const scheme = Platform.select({ ios: 'maps:0,0?q=', android: 'geo:0,0?q=' });
const latLng = `${lat},${lng}`;
const label = 'Custom Label';
const url = Platform.select({
ios: `${scheme}${label}@${latLng}`,
android: `${scheme}${latLng}(${label})`
});

Linking.openURL(url);

安装"react native navigation directions"并使用下面的给定代码。点击按钮后调用_callShowDirections,它会将您重定向到标准的谷歌地图应用程序。

import { OpenMapDirections } from 'react-native-navigation-directions';
//Use this function 
_callShowDirections = () => {
const startPoint = {
longitude: 'START_POINT_LONGITUDE',
latitude: 'START_POINT_LATITUDE'
} 
const endPoint = {
longitude: 'END_POINT_LONGITUDE',
latitude:  'END_POINT_LATITUDE'
}
const transportPlan = 'd';
OpenMapDirections(startPoint, endPoint, transportPlan).then(res => {
console.log(res)
});
}

最新更新