0

I'm currently building a website, in which there is a function that allows users to mark their homeaddress in google map and the coordinates are stored in database using php+mysql, then on another page the coordinates are grabbed from db and used to disaplay. how do I do that ?

I'm not very familiar with google map api so it will be great help if any of you can provide an similar example code:)

3
  • Google provides loads of examples. Why don't you try their basic ones and then modify/adapt the more complex ones to your needs? Commented Jul 11, 2012 at 14:30
  • What have you tried? What information are you going to store. It seems like should not only store the address but the latitude and longitude values once you retrieve them. Commented Jul 11, 2012 at 14:36
  • Well, it's rather simple: You attach an event handler to the onclick event on the map in which you place the marker and also you store it's coordinates in a variable. When the user clicks save, you send the variable to the server, via AJAX preferably. This example should get you on the right track: google-developers.appspot.com/maps/documentation/javascript/… You'll have to figure the AJAX part yourself, though. Try looking into jQuery for that. Commented Jul 11, 2012 at 17:19

1 Answer 1

2

As I understand you got stuck in the part where you want to grab the data from the database and present a map with the location based on the latitude and longitude.. If this is right, you can try this..

Base on the example found on this link https://developers.google.com/maps/articles/phpsqlsearch_v3?hl=es#createtable

This is the code that worked for me (I do not know if anything is redundant).

<?php
    //select statement that grabs latitude and longitude for the current user and stores them to   variables eg $lat and $lng. Then you just echo them below into the javascript.
?>
<html>
<head>
<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=false"></script>
<script language="javascript" type="text/javascript">

    var map;
    var geocoder;
    function InitializeMap() {

    var latlng = new google.maps.LatLng(<?php echo $lat; ?>, <?php $lng; ?>);
    var myOptions =
    {
        zoom: 8,
        center: latlng,
        mapTypeId: google.maps.MapTypeId.ROADMAP,
        disableDefaultUI: true
    };
    map = new google.maps.Map(document.getElementById("map"), myOptions);
}

window.onload = InitializeMap;

</script>
</head>
<body>

<table>

<tr>
<td colspan ="2">
<div id ="map" style="height: 253px;width: 447px;" >
</div>
</td>
</tr>
</table>
</body>

</html>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.