Problems using LatLngBounds and the center of a map - php

Am having a problem showing a map.
I have a collection of markers (from a db via php and json) and i want the center and the zoom for the map applied relative to all of the markers; Thats why am not setting the center and the zoom on the map and using the LatLngBounds object. Thing is, its not working.
This is the javascript code:
var jsonURL="http://localhost/gpsdev/db2json.php";
var map;
function init(){
var options = {
mapTypeId: google.maps.MapTypeId.ROADMAP,
mapTypeControl:false,
scaleControl:true,
streetViewControl:false,
zoom:12,
center: new google.maps.LatLng(-25.973728,32.582745)
};
var mapBounds= new google.maps.LatLngBounds();
map = new google.maps.Map(document.getElementById('map'), options);
$.getJSON(jsonURL,{cli:"1"}, function(data){
for (var i = 0; i < data.length; i++) {
var point = new google.maps.LatLng(data[i].Latitude,data[i].Longitude);
mapBounds.extend(point);
new google.maps.Marker({
position: point,
map: map,
title:data[i].PlateNR
});
}
});
map.fitBounds(mapBounds);
}
window.onload=init;
if i remove the zoom and center it doesnt work. this is the json am inputting on the javascript:
[
{\"VehicleID\":\"1\",\"ClientID\":\"1\",\"PlateNR\":\"MMA-01-01\",\"Type\":\"Ligeiro\",\"Latitude\":\"-25.973728\",\"Longitude\":\"32.582745\",\"Velocity\":\"0.000\",\"Ignition\":\"0\",\"ClientName\":\"Sergio\"},
{\"VehicleID\":\"1\",\"ClientID\":\"1\",\"PlateNR\":\"MMA-01-01\",\"Type\":\"Ligeiro\",\"Latitude\":\"-25.972456\",\"Longitude\":\"32.578968\",\"Velocity\":\"10.000\",\"Ignition\":\"1\",\"ClientName\":\"Sergio\"},
{\"VehicleID\":\"1\",\"ClientID\":\"1\",\"PlateNR\":\"MMA-01-01\",\"Type\":\"Ligeiro\",\"Latitude\":\"-25.970083\",\"Longitude\":\"32.580879\",\"Velocity\":\"80.000\",\"Ignition\":\"1\",\"ClientName\":\"Sergio\"},
{\"VehicleID\":\"1\",\"ClientID\":\"1\",\"PlateNR\":\"MMA-01-01\",\"Type\":\"Ligeiro\",\"Latitude\":\"-25.968191\",\"Longitude\":\"32.577724\",\"Velocity\":\"90.000\",\"Ignition\":\"1\",\"ClientName\":\"Sergio\"}
]
What am doing wrong here?

$.getJSON(jsonURL,{cli:"1"}, function(data){
for (var i = 0; i < data.length; i++) {
var point = new google.maps.LatLng(data[i].Latitude,data[i].Longitude);
mapBounds.extend(point);
new google.maps.Marker({
position: point,
map: map,
title:data[i].PlateNR
});
}
// this is where you should call map.fitBounds()
map.fitBounds(mapBounds);
});
// move the following line from its existing location to inside $.getJSON call
// map.fitBounds(mapBounds);

Check this tutorial: http://www.svennerberg.com/2008/11/bounding-box-in-google-maps/, but be careful its not about the latest API (v3).

Related

Change the color of one of the pin on google map in PHP

My code is as below `
<script type="text/javascript">
var locations = [
['add1', lat1, long1],
['add2', lat2, long2],
['add3', lat3, long3],
];
var map = new google.maps.Map(document.getElementById('map'), {
zoom:10,
center: new google.maps.LatLng(18.528771, 73.906314),
mapTypeId: google.maps.MapTypeId.ROADMAP
});
var infowindow = new google.maps.InfoWindow();
var marker, i;
for (i = 0; i < locations.length; i++) {
marker = new google.maps.Marker({
position: new google.maps.LatLng(locations[i][1], locations[i][2]),
map: map
});
google.maps.event.addListener(marker, 'click', (function (marker, i) {
return function () {
infowindow.setContent(locations[i][0]);
infowindow.open(map, marker);
}
})(marker, i));
}
</script>`
More than one pins are there on map, I would like to display one of the pin in diff color or display an image instead of bubble.
I am implementing the code in PHP.
You need add an icon attribute to the marker options and set the image to the correct google pin:
var image = 'images/beachflag.png';
var myLatLng = new google.maps.LatLng(-33.890542, 151.274856);
var beachMarker = new google.maps.Marker({
position: myLatLng,
map: map,
icon: image
});
see google doc google example

Adding places to google maps

I have a php application that stores Lng-Lat Coordinates to my database.
I would like to show all these places to a google map.
Edit: I have 6000 coordinates, I would like to do this in some way automatically.
Thank you
There are articles for that, which you can find nice and easily on Google
https://developers.google.com/maps/articles/phpsqlajax_v3
If you have several locations, you can use the following code:
var coords = [
[40.980542, 55.111786],
[42.329036, 55.222452],
[44.280249, 54.333]
];
function initialize() {
var myLatlng = new google.maps.LatLng(coords[0][0], coords[0][1]);
var myOptions = {
zoom: 4,
center: myLatlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
var map = new google.maps.Map(document.getElementById("map"), myOptions);
for (var i = 0; i < coords.length; i++) {
var latlng = new google.maps.LatLng(coords[i][0], coords[i][1]);
var marker = new google.maps.Marker({
position: latlng,
map: map,
title: "marker : " + (i + 1)
});
}
}
google.maps.event.addDomListener(window, 'load', initialize);
Check out a working example in JSfiddle.
Also, take a look at Google Places API.
Its a service that returns information about Places using HTTP requests

Retrieve remote JSON content with jQuery

I have the problem of retrive a Json into my Jquery script.
A php script, located at http://myserver.com/script.php return by echo a JSON like:
{"locations":[{"name":18492554,"lat":"12345","long":"234"},{"name":18492553,"lat":"4567","long":"234},{"name":18492555,"lat":"2234","long":"234}]}
I want to plot that point into my Jqueru script like:
(function() {
window.onload = function() {
// Creating a new map
var map = new google.maps.Map(document.getElementById("map"), {
center: new google.maps.LatLng(41.6, -0.88),
zoom: 12,
mapTypeId: google.maps.MapTypeId.ROADMAP
});
///////////////////// GET the JSON data ///////////////////
var json = // ???????
// Creating a global infoWindow object that will be reused by all markers
var infoWindow = new google.maps.InfoWindow();
// Looping through the JSON data
for (var i = 0, length = json.length; i < length; i++) {
var data = json[i],
latLng = new google.maps.LatLng(data.lat, data.long);
// Creating a marker and putting it on the map
//var iconBase = 'https://dea-srl.net/domenico/traking/js/';
var iconBase = 'https://maps.google.com/mapfiles/kml/shapes/';
var marker = new google.maps.Marker({
position: latLng,
map: map,
title: data.nombre,
icon: iconBase + 'schools_maps.png'
});
// Creating a closure to retain the correct data, notice how I pass the current data in the loop into the closure (marker, data)
(function(marker, data) {
// Attaching a click event to the current marker
google.maps.event.addListener(marker, "click", function(e) {
infoWindow.setContent(data.nombre);
infoWindow.open(map, marker);
});
})(marker, data);
}
}
})();
My problem is get that Json into de jquery. It is possible?
I tryed using get metho like:
$.get(" http://myserver.com/script.php");
but it doesnt works.
Any idea about that? Thanks in advance.
What you mean by doesn't work? What errors you have?
Are you using the done function?
$.get(" http://myserver.com/script.php").done(function(data) {
console.log(data);
});
or you can use directly the $.getJSON function.
$.getJSON('http://myserver.com/script.php', function(data) {
var json = data;
});
To load the json data.
If there is any other issue, you have be a bit more precise.

Getting error when trying to pass values from array to Google Maps Object

Im setting some json using wordpress post data on a page and then passing that json to some JS which loops through and adds markers to a map. I'm so close to getting it working, just need to figure out this last part.
My PHP code to create the json from an array:
<script type="text/javascript">
var markers = <?php echo json_encode($pageposts);?>
</script>
Here is my JS code:
var infowindow = null;
$(document).ready(function(){
initialize();
});
function initialize() {
var centerMap = new google.maps.LatLng(41.141208, -73.263726);
var options = {
zoom: 12,
center: centerMap,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
var map = new google.maps.Map(document.getElementById('map'), options);
setMarkers(map, markers);
infowindow = new google.maps.InfoWindow({
content: "loading..."
});
}
function setMarkers(map, markers) {
for (var i = 0; i < markers.length; i++) {
var marker = new google.maps.Marker({
position: new google.maps.LatLng(markers[i].meta_value),
map: map
});
var contentString = "Some content";
google.maps.event.addListener(marker, "click", function () {
//infowindow.setContent(this.html);
//infowindow.open(map, this);
});
}
}
If you want to see the page, with the json embedded - check out this link:
http://www.fairfieldctguide.com/test-map
view-source:http://www.fairfieldctguide.com/test-map
Any help would be greatly appreciated!
Jake
google.maps.LatLng expects two numbers as an argument. Currently you are passing in a string which will result in an error. So you need to convert your markers[i].metavalue to two numbers like so:
function setMarkers(map, markers) {
for (var i = 0; i < markers.length; i++) {
latlng = markers[i].meta_value.split(",")
lat = parseFloat(latlng[0])
lng= parseFloat(latlng[1])
var marker = new google.maps.Marker({
position: new google.maps.LatLng(lat, lng),
map: map
});
var contentString = "Some content";
google.maps.event.addListener(marker, "click", function () {
//infowindow.setContent(this.html);
//infowindow.open(map, this);
});
}
}
If you don't want to do a converson you could just store lat and lng values as numbers in separate properties. So your json would look like this:
var markers = [{
"ID":"883",
"post_title":"Tucker's Cafe",
"meta_key":"meta_geo",
"lat":41.1674377,
"lng": -73.2236554
}
and you would add a marker like so:
var marker = new google.maps.Marker({
position: new google.maps.LatLng(markers[i].lat, markers[i].lng),
map: map
});

Google Maps V3 Zoom to Show all markers not working

I'm using Google Maps V3 API and after creating several markers on the map, I am having difficulty getting the map to zoom out such that it shows all markers. Right now the code below just shows the markers without adjusting the zoom. Can you find the mistake in there? Thanks!
<script type="text/javascript">
function initialize() {
var latlng = new google.maps.LatLng(1.289566,103.847267);
var options = {
zoom: 15,
center: latlng,
mapTypeId: google.maps.MapTypeId.ROADMAP,
scrollwheel: false
};
var map = new google.maps.Map(document.getElementById('map_canvas'), options);
var marker = new google.maps.Marker({
position: new google.maps.LatLng(1.289566,103.847267),
map: map,
icon: "http://chart.apis.google.com/chart?chst=d_map_pin_letter&chld=1|ff776b"
});
var marker = new google.maps.Marker({
position: new google.maps.LatLng(1.301224,103.912949),
map: map,
icon: "http://chart.apis.google.com/chart?chst=d_map_pin_letter&chld=2|ff776b"
});
var marker = new google.maps.Marker({
position: new google.maps.LatLng(1.293150,103.827164),
map: map,
icon: "http://chart.apis.google.com/chart?chst=d_map_pin_letter&chld=3|ff776b"
});
var LatLngList = array (
new google.maps.LatLng(1.289566,103.847267),
new google.maps.LatLng(1.301224,103.912949),
new google.maps.LatLng(1.293150,103.827164)
);
var bounds = new google.maps.LatLngBounds();
for (var i = 0, LtLgLen = LatLngList.length; i < LtLgLen; i++) {
bounds.extend(LatLngList[i]);
}
map.fitBounds(bounds);
}
</script>
This:
var LatLngList = array (
new google.maps.LatLng(1.289566,103.847267),
new google.maps.LatLng(1.301224,103.912949),
new google.maps.LatLng(1.293150,103.827164)
);
is not how you create an array in JavaScript. Instead, try:
var LatLngList = [
new google.maps.LatLng(1.289566,103.847267),
new google.maps.LatLng(1.301224,103.912949),
new google.maps.LatLng(1.293150,103.827164)
];
Where you are defining LatLngList you have an error. The proper code should be:
var LatLngList = new Array();
Not:
var LatLngList = array();

Categories