I have been having problems getting react-native-maps to work properly on android. The issue is that when I load my array data it works only on IOS. It generates like this :
const locationData = [
{latitude: "52.4194975", longitude: "-1.5101260"},
{latitude: "52.403002", longitude: "-1.491740"}
];
It is generated from a MYSQL database using PHP.
I display the markers like so:
{locationData.map((data, index) => (
<Marker
key={index}
coordinate={{
latitude: data.latitude,
longitude: data.longitude
}}
/>
))}
It will work on both IO and Android when I use this :
const locationData = [
{latitude: 52.4194975, longitude: -1.5101260},
{latitude: 52.403002, longitude: -1.491740}
];
But if I use this it does not work on Android but its OK on IOS :
const locationData = [
{latitude: "52.4194975", longitude: "-1.5101260"},
{latitude: "52.403002", longitude: "-1.491740"}
];
Its obvious it is because of the "" I have tried adding these but they do not work?
latitude: data.latitude,replace('"', '')
latitude: data.latitude,str.replace('"', "")
Is there a workaround or am I missing something? Thanks
Convert a string to a floating point number using parseFloat, something like this:
const locationData = [
{ latitude: "52.4194975", longitude: "-1.5101260" },
{ latitude: "52.403002", longitude: "-1.491740" },
];
const coordinates = locationData.map(c => {
return {
latitude: parseFloat(c.latitude),
longitude: parseFloat(c.longitude),
};
});
Naturally the best solution is to return numbers already from a backend to avoid any conversion in a client.