I'm using a google map api from this website , thanks to the author.
Here is part of the code:
function initMap() {
map = new google.maps.Map(document.getElementById("map"), {
center: new google.maps.LatLng(0, 0),
zoom: 14,
mapTypeId: google.maps.MapTypeId.ROADMAP,
mapTypeControl: false,
mapTypeControlOptions: {
style: google.maps.MapTypeControlStyle.HORIZONTAL_BAR
},
navigationControl: true,
navigationControlOptions: {
style: google.maps.NavigationControlStyle.SMALL
}
});
<?
$query = mysql_query("SELECT * FROM markers WHERE ID='1'");
while ($row = mysql_fetch_array($query)){
$name=$row['name'];
$lat=$row['lat'];
$lon=$row['lng'];
$desc=$row['address'];
echo ("addMarker($lat, $lon,'<b>$name</b><br/>$desc');\n");
}
?>
center = bounds.getCenter();
map.fitBounds(bounds);
However I'm facing a problem to adjust the zoom level , it set as 14, but no matter what value I changed , it remains the same.
I read from the comments below , someone suggest to remove this line :
map.fitBounds(bounds);
I removed it , but the center of map is move to other location , which I think is (0,0).
Is there any way to fix the zoom problem of this code? I tried to edit all the size though..but I'm not familiar with google map coding.
Thank you.
Replace this:
center = bounds.getCenter();
map.fitBounds(bounds);
with:
map.setCenter(bounds.getCenter());
bounds.getCenter() will return the calculated center of the bounds extended by addMarker()
It looks like you're setting a zoom level when you create the map, but then map.fitBounds() changes the zoom level to contain the bounds.
Try setting the zoom level after setting the viewport with fitBounds.
...
map.fitBounds(bounds);
map.setZoom(14);
Here you can pick where you want center the map:
center: new google.maps.LatLng(0, 0),
Ofc, put some other numbers in place of 0, 0 - these are Latitude and Longitude.
Then you wont need the fit bounds - delete it.
And zoom: 14 will work (and any other number). Its not working now, cuz its taking the bounds, and trying to fit all the area inside these bound, and use MAX zoom possible, to show whole area.
After map.fitBounds(bounds) statement add the following statement
map.setZoom(zoom:number)
This will set the zoomlevel to your desired zoom level.
Analyzing your code:
You are initializing the map with zoom level 14 and center (0,0).
Significance of map.fitBounds(bounds); command is it adjusts the bounds of the map to show all the added markers which in-turn will change the zoom level. If you remove this statement your center will remain as (0,0) and zoom level as 14. If you want to set the center of the map use the following statement
map.setCenter(latlng:LatLng)
Thanks
B.J.
Related
I need to extract the coordinates from the url and use it to center the map. The following is the code I use now. But when I use this code, the map doesn't load at all.
map = new google.maps.Map(document.getElementById("map"), {
center: new google.maps.LatLng(<?php $_GET['curlat']?> , <?php $_GET['curlng']?>),
zoom: 12,
mapTypeId: 'roadmap'
});
While if I pass the coordinates like the following, it's working perfect
center: new google.maps.LatLng(13.964711, 76.325325),
Evidently, the error is in the above line of code.
What am I doing wrong in extracting the url and passing the value?
Try to echo the output :
ap = new google.maps.Map(document.getElementById("map"), {
center: new google.maps.LatLng(<?php echo $_GET['curlat'] .",". $_GET['curlng'];?>),
zoom: 12,
mapTypeId: 'roadmap'
});
Maybe it'll work if you actually print the variable !
<?php echo $_GET['curlat']?>
I have list of coordinates in my database, I want to show that list of property on Google map using API, like this:
red mark are my coordinates/places
how is it possible any idea.
It's very easy, just read data from database, send it to client in json format and plot it in mail
refer following link,
https://developers.google.com/maps/documentation/javascript/markers
sample data
[{'latitude':'59.327383','longitude':'18.06747','title':'test'}....]
following is sample code
var center = new google.maps.LatLng('center latitude','center longitude'); // set center location
var mapOptions = {
zoom: 4,
center: center
}
var map = new google.maps.Map(document.getElementById("map-canvas"), mapOptions);
for(var m in points) {
var myLatlng = new google.maps.LatLng(points[m].latitude,points[m].longitude);
// To add the marker to the map, use the 'map' property
var marker = new google.maps.Marker({
position: myLatlng,
map: map,
title:points[m].title
});
}
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.
I am trying to figure out how to pass the latitude and longitude to a google map when my page loads. I am using the following code in the header of my page which works to generate the maps based on the hard-coded latitude and longitude:
var map1;
var map2;
$(document).ready(function(){
google.maps.event.addDomListener(window, 'load', initialize);
function initialize() {
var myOptions = {
zoom: 14,
center: new google.maps.LatLng(43.7620537, -79.3516683),
mapTypeId: google.maps.MapTypeId.ROADMAP
}
map1 = new google.maps.Map(document.getElementById("map-canvas-1"),myOptions);
var myOptions2 = {
zoom: 14,
center: new google.maps.LatLng(43.6918950,-79.5310706),
mapTypeId: google.maps.MapTypeId.ROADMAP
}
map2 = new google.maps.Map(document.getElementById("map-canvas-2"),myOptions2);
}
$("#maptabs").tabs({initialIndex:0,collapsible:true,select: function(){}});
In the body of the page I use the following PHP script to retrieve the correct latitude and longitude that I would like to map:
<?php
$google_maps_key='ABQIAAAAO-NJVsUOX31LQYJY1LUG8BSOYIDhzYrztjureJjDlAP7PmMi1xQsm9hQ6Z-LRdLFkCtIPr0FQBZxeQ';
$adr = urlencode(DLookup("Property Address", "tblproperties", "`Roll#`=".$id["pntysc"]["pn"]).", ".DLookup("municipalityname", "tblmunicipalities", "municipalitiescounty=".$county));
$url = "http://maps.google.com/maps/geo?q=".$adr."&output=xml&key=$google_maps_key";
$xml = simplexml_load_file($url);
$status = $xml->Response->Status->code;
if ($status='200') { //address geocoded correct, show results
foreach ($xml->Response->Placemark as $node) { // loop through the responses
$address = $node->address;
$quality = $node->AddressDetails['Accuracy'];
$coordinates = $node->Point->coordinates;
echo ("Quality: $quality. $address. $coordinates<br/>");
}
} else { // address couldn't be geocoded show error message
echo ("The address $adr could not be geocoded<br/>");
}
?>
Here is my question: How can I populate the maps using the $coordinates variable that is generated in the body of the page, when I have code in the header of the page that needs that particular value? What is the best practice in terms of populating the maps using variable latitude and longitude instead of having those values simply hard-coded?
Thanks.
You need to consider defining your Coordinates to display prior to displaying your header. That is, define everything your going to use in your google maps prior to anything being displayed. Place all data into arrays; $coordinates, $groups, etc. You don't have to build out an XML file. You can just use a mix of PHP to build out the Javascript used in your maps views. You can visit a WordPress example at:
http://www.dogfriendlyorlando.com/
There's an IFRAME in the page that displays the header map based on the information provided thru WordPress Posts.
I can't seem to paste in the entire code here.
Email me at jjwdesign - thru gmail and I'll send you the PHP code so you can look at it.
There's another more complex CRM example (SugarCRM - Google Maps project) at:
http://www.sugarforge.org/projects/jjwgooglemaps/
Cheers, Jeff Walters
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
});
}
}