Google maps lat long array - php

I have two arrays $lat, $long,
1 "73.0785729"
2 "73.0785729"
3 "73.0785729"
and same for the $long.
google.maps.event.addDomListener(window, 'load', init);
function init() {
var mapOptions = {
zoom: 11,
center: new google.maps.LatLng(40.6700, -73.9400),
styles: []
};
var mapElement = document.getElementById('map');
var map = new google.maps.Map(mapElement, mapOptions);
var marker = new google.maps.Marker({
position: new google.maps.LatLng({{$lat}}, {{$long}}),
map: map,
title: 'Snazzy!'
});
}
It throws htmlspecialchars() expects parameter 1 to be string, array given.

You have to pass your PHP datas to Javascript with json_encode :
<?php
//Construct the array you want, assuming $lat and $long have the same length
$markers = array();
foreach($lat as $i => $currentLat) {
$markers[] = array("lat" => $currentLat, "long" => $long[$i]);
}
And modify you Javascript :
function init() {
var mapOptions = {
zoom: 11,
center: new google.maps.LatLng(40.6700, -73.9400),
styles: []
};
var mapElement = document.getElementById('map');
var map = new google.maps.Map(mapElement, mapOptions);
var markers = <?php echo json_encode($markers); ?>; //Passing the PHP values to JS
//Add marker to map
for(var i in markers) {
var marker = markers[i];
new google.maps.Marker({
position: new google.maps.LatLng(marker.lat, marker.long),
map: map,
title: 'Snazzy!'
});
}
}

<?php
$lat = ['73.0785729','73.0785729','73.0785729'];
$long = ['73.0785729', '73.0715769', '73.0785729'];
?>
var lat_array = <?=json_encode($lat)?>;
var long_array = <?=json_encode($long)?>;
function init() {
var mapOptions = {
zoom: 11,
center: new google.maps.LatLng(40.6700, -73.9400),
styles: []
};
var mapElement = document.getElementById('map');
var map = new google.maps.Map(mapElement, mapOptions);
// assuming that the length of lat_array and long_array are the same
for(var i = 0; i < lat_array.length; i++){
var marker = new google.maps.Marker({
position: new google.maps.LatLng(parseFloat(lat_array[i]), parseFloat(long_array[i])),
map: map,
title: 'Snazzy!'
});
}
}

Related

Multiple Markers on google maps using an array in php and html. Markers not adding when in array but ok when the value is static

Markers not adding when in array but ok when the value is set to a static value. Is there some way to loop the array so that multiple markers will be added?
function initMap() {
int i = 0;
for (i = 0; i< 5; i++){ //LOOP TO INCREMENT ARRAY
var LatLng = {lat: <?php echo $lat[i]?>, lng: <?php echo $long[i]?>};
//WORKING WHEN A STATIC NUMBER IS PLACED ON ARRAY
//var myLatLng2 = {lat: <?php echo $lat[1]?> , lng: <?php echo $long[1]? >};
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 4,
center: LatLng
});
var marker = new google.maps.Marker({
position: LatLng,//CREATE MARKER
map: map,
title: 'Hello World!'
});
}
}

Google Map contain location function with foreach and mysql

I am using google map api with contain location function .... i am retrieving latitude longitude from database i am using foreach loop to get result.If marker is within polygon it will stop foreach but problem it is not showing any result. Result come out is from last row query how can i fix it.It is showing me only last row from database .Below is my code
<?php
if(!empty($zone)){
foreach($zone as $findzone)
{
$exploded_data=explode('),',$findzone['zone_latlog']);
$count=count($exploded_data);
?>
<script>
var latitude = document.getElementById('latitude').value;
var longitude = document.getElementById('longitude').value;
var map;
var coord1 = new google.maps.LatLng(latitude, longitude);
function initialize() {
var map = new google.maps.Map(
document.getElementById("map_canvas"), {
center: new google.maps.LatLng(33.714760, 73.083160),
zoom: 8,
mapTypeId: google.maps.MapTypeId.ROADMAP
});
var bermudaTriangle = new google.maps.Polygon({
map: map,
paths: [
<?php
for($i=0;$i<$count;$i++){
echo "new google.maps.LatLng".$exploded_data[$i]."),";
}
?>
]
});
var bounds = new google.maps.LatLngBounds();
for (var i=0; i<bermudaTriangle.getPath().getLength(); i++) {
bounds.extend(bermudaTriangle.getPath().getAt(i));
}
bounds.extend(coord1);
var marker1 = new google.maps.Marker({
map: map,
position: coord1
});
map.setCenter(bounds.getCenter());
map.setZoom(11);
checkInPolygon(marker1, bermudaTriangle);
}
google.maps.event.addDomListener(window, "load", initialize);
function checkInPolygon(marker, polygon) {
var infowindow = new google.maps.InfoWindow();
var html = "";
if (google.maps.geometry.poly.containsLocation(marker.getPosition(), polygon)) {
html = "inside polygon";
} else {
html = "outside polygon";
}
infowindow.setContent(html);
infowindow.open(map, marker);
}
</script>
<?php
}}?>
How to fix that problem that show me only only that polygon that has my latitude longitude marker

Google maps markers , polyline and window info from array

I want to show a polyline with markers and window-info from array point (from a ajax page)
This is index Page codes:
var gmarkers = [];
var map = null;
function initialize() {
var myOptions = {
zoom: 15,
center: new google.maps.LatLng(27.332702, 53.177137),
// mapTypeControlOptions: {style: google.maps.MapTypeControlStyle.DROPDOWN_MENU},
mapTypeId: google.maps.MapTypeId.ROADMAP
}
map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
google.maps.event.addListener(map, 'click', function() {
infowindow.close();
});
}
var infowindow = new google.maps.InfoWindow(
{
size: new google.maps.Size(150,50)
});
function myclick(i) {
google.maps.event.trigger(gmarkers[i], "click");
}
function createMarker(latlng, name, html) {
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);
});
// save the info we need to use later for the side_bar
gmarkers.push(marker);
}
My Ajax Page:
var polylines = [];
var beaches = [
['Bondi Beach',10,15, 4],
['Coogee Beach',11,16, 5],
['Cronulla Beach',13,15, 3],
['Manly Beach',13,17, 2],
['Maroubra Beach',12,10, 1]
];
for (var i = 0; i < beaches.length; i++) {
var beach = beaches[i];
var myLatLng = new google.maps.LatLng(beach[1], beach[2]);
var polylines = new google.maps.LatLng(beach[1], beach[2]);
var marker = createMarker(myLatLng,"This place",beach[0])
}
var routes = new google.maps.Polyline({
path: polylines,
strokeColor: "#FF0000",
strokeOpacity: 0.6,
strokeWeight: 4
});
routes.setMap(map);
After I call the Ajax Page , markers add with window-info , but route polyline don't show , where is the problem ?
After I call the Ajax Page , markers add with window-info , but route polyline don't show , where is the problem ?
Yes. polylines is an array here:
var polylines = [];
Here you overwrite it with a single google.maps.LatLng:
var polylines = new google.maps.LatLng(beach[1], beach[2]);
You probably want to do this instead:
polylines.push(new google.maps.LatLng(beach[1], beach[2]));

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