SupaFlybaba
SupaFlybaba

Reputation: 57

Can I retrieve google map coordinates from mySQL with lat & lng together in a column?

Normally people created 2 column in mySQL called latitude and longitude to store and retrieve the coordinates of the points. However, my app retrieves the whole latitude and longitude coordinates and insert them into only 1 column.

Example shown: (1.372442, 103.94954699999994)

Is it possible to insert my database column into the position like below ? or any way I could do it.

var marker = new google.maps.Marker({
position: new google.maps.LatLng(<?php echo $location['locationOne']),
map: map,
}); 

Can the google map acknowledge this and retrieve the points and plot out according to my example ? Thanks

Upvotes: 0

Views: 1740

Answers (4)

Coded Container
Coded Container

Reputation: 863

You can keep your values as they are in the database with or without commas. If Google maps doesn't like it, then you can either add it or remove it using PHP and JavaScript functions. JavaScript = replace(); and PHP = str_replace();

If you are filtering your maps by data it would be behove of you to "cache" the data in a JSON for retrieval, this makes the maps load much quicker. Good luck!

Upvotes: 0

barryhunter
barryhunter

Reputation: 21091

You don't need to split - as long as your server side code produces valid javascript - the users browser will parse it. It so happens javascript uses commas to seperate variables in a function call.

From your example, it appears it already has brackets on the provided values, so you dont need the hardcoded ones.

position: new google.maps.LatLng<?php echo $location['locationOne']; ?>,

But you may want to do some validation, on the value inside locationOne - otherwise you liable to produce invalid javascript, which will at best give a cryptic error message to the user. Not good.

Upvotes: 1

mike
mike

Reputation: 7177

You can either split the column up on the server side with PHP or on the client side in JavaScript. Here's sample JavaScript code:

var latlng = "<?php echo $location['locationOne'] ?>".split(",");
var marker = new google.maps.Marker({
  position: new google.maps.LatLng(latlng[0], latlng[1]),
  map: map,
}); 

Upvotes: 0

Alex
Alex

Reputation: 2420

You will need to split the column into two separate values to create a google maps LatLng object.

Upvotes: 1

Related Questions