Searchbox in Google map API - integrated with infobox database link - php

I am a newby at this and this question may be very simple to alot of you, but please help.
I am trying to intergrate a searchbox within a very simple google map script.
1- I successfully copied the file from here: From Info Windows to a Database: Saving User-Added Form Data
configured and it works very well!!
Now I am trying to add the searchbox functionality into the existing code.
Basically, I would like to integrate this: Place Search Box
into the previous code.
This would allow me to be able to search for places(Cities) before adding specific location to my DB.
Thanks Guys!

You can set things like this:
implement search place.
get important data (name, address, type) auto populate the info window with this data.
edit/save data in DB.
Codes:
// Create a marker for each place.
markers.push(new google.maps.Marker({
map: map,
icon: icon,
title: place.name,
position: place.geometry.location
}));
Then add the custom info window from your link / tweak some values in the form, getting the result of the search place.
var html = "<table>" +
"<tr><td>Name:</td> <td><input type='text' id='name'/> </td> </tr>" +
"<tr><td>Address:</td> <td><input type='text' id='address'/></td> </tr>" +
"<tr><td>Type:</td> <td><select id='type'>" +
"<option value='bar' SELECTED>bar</option>" +
"<option value='restaurant'>restaurant</option>" +
"</select> </td></tr>" +
"<tr><td></td><td><input type='button' value='Save & Close' onclick='saveData()'/></td></tr>";
infowindow = new google.maps.InfoWindow({
content: html
});
google.maps.event.addListener(map, "click", function(event) {
marker = new google.maps.Marker({
position: event.latLng,
map: map
});
google.maps.event.addListener(marker, "click", function() {
infowindow.open(map, marker);
});
});
}
Here is the list of item response from the places details.

Related

Show location details in Google maps javascript api v3

I am displaying the google map using javascript api v3. I am now able to display the title on mouse over of the pin.But I want to display the details in a div on mouse over of the pin.Can anyone suggest a method for doing so?
Assuming details holds the information to display, marker is the pin to click and map is your map:
var contentDetails = "<div>" + details + "</div>"
var infowindow = new google.maps.InfoWindow( {content: contentDetails});
google.maps.event.addListener(marker, 'mouseover', function() {
infowindow.open(map,marker);
});

Google maps - load polygon on marker click

I have a kml file with polygon data that is too complex to load onto one googlemap as it contains thousands of latlng coordinates that just wont load all at once.
My question is, is it possible to load just one polygon when a map marker is clicked? I have a mysql database table that holds the latlngs for each marker and the table also has a column for polyCoords. I am looping through the data using php which displays all the markers correctly. Can i then add a listener to the markers which will load the data from the polyCoords column and just show the polygon for that clicked map marker?
<script type="text/javascript">
function initialize()
{
var centre = new google.maps.LatLng(34.233753,-83.828712);
var myOptions =
{
zoom: 4,
center: centre,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
<?php while ($row = #mysql_fetch_assoc($result))
{?>
var myLatlng = new google.maps.LatLng(<?php echo $row['lat'] . ',' . $row['lng']?>);
var marker = new google.maps.Marker
(
{
position: myLatlng,
map: map
}
);
<?php }?>
}
</script>
Thanks
Yes. Investigate AJAX.
There are some articles in the Google docs which may help: https://developers.google.com/maps/documentation/javascript/articles
Have a look at Using PHP/MySQL with Google Maps and Creating a store locator with PHP & MySQL. The latter covers getting data out of a database and updating what's shown on the map (it's markers rather a polygon boundary, but the technique is very similar).
If you run into a specific coding difficulty, ask a question about that.

Load Markers in Google Maps according to change in checkbox value in php

hi,
Above is the screenshot of my page. Now currently upon my query below i am displaying the markers in my map.
<?
$query=mysql_query("select * from events join event_time
on(events.event_id=event_time.event_id) where events.isapproved='Y'");
while ($row = mysql_fetch_array($query)){
$title=$row['title'];
$location=$row['location'];
$sdate=$row['start_date'];
$edate=$row['end_date'];
$stime=$row['start_time'];
$duration=$row['duration'];
$lat=$row['latitude'];
$lon=$row['longitude'];
$address=$row['address'];
$de=$row['description'];
$link=$row['link'];
echo ("addMarker($lat, $lon,'<b>$title</b><br/><br/>$sdate ".to."$edate<br/>
<br/>$address<br/><br/><a>$link</a>');\n");
}
?>
below is the add marker fn that currently i am using
function addMarker(lat, lng, info) {
var pt = new google.maps.LatLng(lat, lng);
bounds.extend(pt);
var marker = new google.maps.Marker({
position: pt,
icon: icon,
map: map
});
Now i want the functionality like this, As you can see there is a list of check boxes by the side. I want the functionality like this:
I want to change my marker's display according to my selection of the check box. These check boxes are my category in my database. Can anyone please help me out!!!
I would use a function like this:
var bounds = new google.maps.LatLngBounds();
$('#map_canvas').gmap('findMarker', customAttribute, variable, separator, function(marker, found) {
if (found){
marker.setVisible(true);
bounds.extend(marker.position);
marker.map.fitBounds(bounds);
}
else{
marker.setVisible(false);
}
});
You have to add a CustomAttribute to your marker, which is the one you will ad to the function, like 'class' for example.
Then, you need to specify (with the checkbox value) which element of this class need to be searched. Finally, you should provide a separator if you have it added. This allow to have multiple 'classes', for example you could have 'foodanddrinks-sports' and that marker would be show in both cases.

Trying to put a posted php variable into a javascript variable so can plot on googlemap

Hi I have posted two values for latitute and longitute to a php page using a form, then on the php page I am displaying a googlemap and want to use the two values i posted as the latitude and longitute to plot on the map. I think iv done everything right but it doesnt seem to pick up the values.any ideas? cheers!
<script type="text/javascript">
function initialize() {
var lat= "<?php echo $_POST['coordinates']; ?>";
var long= "<?php echo $_POST['coordinatestwo']; ?>";
var latlng = new google.maps.LatLng(lat,long);
var myOptions = {
zoom: 8,
center: latlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById("map_canvas"),
myOptions);
var marker = new google.maps.Marker({
position: latlng,
map: map,
title: "Your current location!"
});
}
</script>
</head>
<body onLoad="initialize();">
long is a reserved word in JavaScript. Try lng (no 'o') as your variable name instead.
var lng= "<?php echo $_POST['coordinatestwo']; ?>";
If you view source in your browser, do you see the php values there? If not then there is something wrong with your form. We would need to see more code.
But chances are it's the long being a reserved word issue I mentioned above.
Don't use long as a variable name (it's a reserved word). Also, you don't need quotes around numeric values. Try this:
var lat = <?php echo floatval($_POST['coordinates']); ?>;
var lng = <?php echo floatval($_POST['coordinatestwo']); ?>;
EDIT: Tip - if you use a (good) editor with color highlighting and syntax checking for JavaScript, this is an error that would be highlighted (mine reports: "strict warning: long is a reserver identifier").
I have a working jsFiddle here:
http://jsfiddle.net/rcravens/RFHKd/
Mostly your code. I added the following:
A 'div' with an ID of map_canvas
A style to this div giving it width and height.
Included the script to load google maps.
Hope this helps.
Bob
#paul elliot: That doesn't look like all your code..? See this example to fill in the bits your code is missing: http://code.google.com/apis/maps/documentation/javascript/examples/map-simple.html

How can I load markers from MySQL database dynamically?

I have a MySQL database where I stored some marker position (along with some other information) which I loaded into google map. I followed the example -
http://code.google.com/apis/maps/articles/phpsqlajax.html
and it worked fine for me.
Now what I want to do is, suppose a user will be able to choose an id of a specific marker and clicking on a button it will be loaded onto the map. Not sure how to load the specific marker on the map dynamically.
Any sort of help will be appreciated.
I have set up an example here. I am using the Google Geocoding API to receive co-ordinates of the address entered by the user and creating the marker using Google API V3 as shown below
Ajax Request to get the co-ordinates
$.getJSON("getjson.php?address="+address,
function(data){
lat=data.results[0].geometry.location.lat;
lng=data.results[0].geometry.location.lng;
point= new google.maps.LatLng(lat,lng);
map.setCenter(point);
zoom = 14;
marker = createMarker(point,"<h3>Marker"+(markersArray.length+1)+" "+point+"</h3>",zoom);
});
PHP Code to return the co-ordinates
<?php
$address = $_GET['address'];
$address=str_replace(" ","+",$address);
if ($address) {
$json = file_get_contents('http://maps.googleapis.com/maps/api/geocode/json?address='.$address.
'&sensor=true');
echo $json;
}
?>
Creating the Marker
function createMarker(latlng, html,zoom) {
var contentString = html;
var marker = new google.maps.Marker({
position: latlng,
map: map,
zIndex: Math.round(latlng.lat()*-100000)<<5
});
google.maps.event.addListener(marker, 'click', function() {
infowindow.setContent(contentString);
infowindow.open(map,marker);
});
marker.MyZoom = zoom;
return marker;
}
First of all i see in your link that you use v2.Try to use v3 at least now that you are at the beginning since the v2 is deprecated.
As of your question i can only tell you the procedure and then you can make it happen.
1. User interaction
User interacts with UI and selects i.e. a range of prices(0-100).
2. Ajax request
The client sends an ajax request to server to get json with jquery getJson() or any other way.
3. Server Respond
A php page responds to your ajax call and make a query in mysql getting a resultset of position-markers,converts it in json and send it back.
4. Parse respond
Parse the json back to the client and create markers.
Hi I found this one great help
http://www.svennerberg.com/2012/03/adding-multiple-markers-to-google-maps-from-json/
Create a json object as given the link and pass it to javascript .
function createMarker(json){
for (var i = 0, length = json.length; i < length; i++) {
var data = json[i],
latLng = new google.maps.LatLng(data.lat, data.lng);
// Creating a marker and putting it on the map
var marker = new google.maps.Marker({
position: latLng,
map: map,
title: data.title
});
}
}

Categories