How to add MarkerClusterer to my google map - php

I'm trying to add the Google Cluster to my map, but i cant work out how to do it? i have created the map and its markers with info box using mysql query in php. know i'm adding more markers they are over lapping so i need to add the cluster command but i can't get it working. my code is below
<script>
$(document).ready(function() {
//------- Google Maps ---------//
// Creating a LatLng object containing the coordinate for the center of the map
var latlng = new google.maps.LatLng(53.0,0);
// Creating an object literal containing the properties we want to pass to the map
var options = {
zoom: 5, // This number can be set to define the initial zoom level of the map
center: latlng,
disableDefaultUI: true,
panControl: false,
zoomControl: true,
scaleControl: true,
mapTypeId: google.maps.MapTypeId.ROADMAP, // This value can be set to define the map type ROADMAP/SATELLITE/HYBRID/TERRAIN
styles: [
{
"featureType": "landscape.natural",
"stylers": [
{ "visibility": "simplified" },
{ "color": "#07c6ee" }
]
},{
"featureType": "landscape.man_made",
"stylers": [
{ "visibility": "on" },
{ "hue": "#6e00ff" },
{ "gamma": 1.96 },
{ "lightness": 18 },
{ "saturation": -78 },
{ "color": "#46fcb3" }
]
},{
"featureType": "water",
"stylers": [
{ "visibility": "simplified" },
{ "color": "#f9ec55" }
]
},{
},{
"featureType": "road.highway",
"stylers": [
{ "visibility": "on" },
{ "color": "#7b8080" }
]
},{
"featureType": "road.arterial",
"stylers": [
{ "visibility": "on" },
{ "color": "#808080" }
]
},{
"featureType": "road.local",
"stylers": [
{ "color": "#808080" },
{ "visibility": "on" }
]
},{
"featureType": "road",
"elementType": "labels.text.fill",
"stylers": [
{ "color": "#fefffe" },
{ "visibility": "on" }
]
},{
"featureType": "landscape.man_made",
"stylers": [
{ "visibility": "off" }
]
},{
"featureType": "road.highway",
"elementType": "labels.icon",
"stylers": [
{ "visibility": "off" }
]
},{
"featureType": "poi",
"stylers": [
{ "visibility": "off" }
]
},{
}
]
};
// Calling the constructor, thereby initializing the map
var map = new google.maps.Map(document.getElementById('map_div'), options);
// Define Marker properties
var image1 = new google.maps.MarkerImage('http://www.foodhawkers.co.uk/images/map-market.png',
// This marker is 129 pixels wide by 42 pixels tall.
new google.maps.Size(40, 40),
// The origin for this image is 0,0.
new google.maps.Point(0,0),
// The anchor for this image is the base of the flagpole at 18,42.
new google.maps.Point(40, 40)
);
//
// Add Marker
var marker1 = new google.maps.Marker({
position: new google.maps.LatLng(51.4636101,-0.1382557),
map: map,
icon: image1 // This path is the custom pin to be shown. Remove this line and the proceeding comma to use default pin
});
// Add listener for a click on the pin
google.maps.event.addListener(marker1, 'click', function() {
infowindow1.open(map, marker1);
});
// Add information window
var infowindow1 = new google.maps.InfoWindow({
content: createInfo('Venn Street Market', '<div style="width:200px">...</div><br /><br />View profile')
});
var marker2 = new google.maps.Marker({
position: new google.maps.LatLng(51.5367954,-0.0618898),
map: map,
icon: image1 // This path is the custom pin to be shown. Remove this line and the proceeding comma to use default pin
});
// Add listener for a click on the pin
google.maps.event.addListener(marker2, 'click', function() {
infowindow2.open(map, marker2);
});
// Add information window
var infowindow2 = new google.maps.InfoWindow({
content: createInfo('Broadway Market', '<div style="width:200px">...</div><br /><br />View profile')
});
var marker3 = new google.maps.Marker({
position: new google.maps.LatLng(51.4721386,-2.7580983),
map: map,
icon: image1 // This path is the custom pin to be shown. Remove this line and the proceeding comma to use default pin
});
// Add listener for a click on the pin
google.maps.event.addListener(marker3, 'click', function() {
infowindow3.open(map, marker3);
});
// Add information window
var infowindow3 = new google.maps.InfoWindow({
content: createInfo('StrEAT Food Market', '<div style="width:200px">We\'re really hoping to be doing some amazing street food nights in the CENTRE of...</div><br /><br />View profile')
});
var marker4 = new google.maps.Marker({
position: new google.maps.LatLng(51.5945845,-0.1302629),
map: map,
icon: image1 // This path is the custom pin to be shown. Remove this line and the proceeding comma to use default pin
});
// Add listener for a click on the pin
google.maps.event.addListener(marker4, 'click', function() {
infowindow4.open(map, marker4);
});
// Add information window
var infowindow4 = new google.maps.InfoWindow({
content: createInfo('Alexandra Palace Farmers Market', '<div style="width:200px">Sundays 10:00 - 15:00...</div><br /><br />View profile')
});
var marker5 = new google.maps.Marker({
position: new google.maps.LatLng(51.5050824,-0.0900808),
map: map,
icon: image1 // This path is the custom pin to be shown. Remove this line and the proceeding comma to use default pin
});
// Add listener for a click on the pin
google.maps.event.addListener(marker5, 'click', function() {
infowindow5.open(map, marker5);
});
// Add information window
var infowindow5 = new google.maps.InfoWindow({
content: createInfo('Borough Market', '<div style="width:200px">...</div><br /><br />View profile')
});
var marker6 = new google.maps.Marker({
position: new google.maps.LatLng(51.5057757,-0.1168251),
map: map,
icon: image1 // This path is the custom pin to be shown. Remove this line and the proceeding comma to use default pin
});
// Add listener for a click on the pin
google.maps.event.addListener(marker6, 'click', function() {
infowindow6.open(map, marker6);
});
// Add information window
var infowindow6 = new google.maps.InfoWindow({
content: createInfo('The Real Food Market', '<div style="width:200px">...</div><br /><br />View profile')
});
var marker7 = new google.maps.Marker({
position: new google.maps.LatLng(51.5005095,-0.1131232),
map: map,
icon: image1 // This path is the custom pin to be shown. Remove this line and the proceeding comma to use default pin
});
// Add listener for a click on the pin
google.maps.event.addListener(marker7, 'click', function() {
infowindow7.open(map, marker7);
});
// Add information window
var infowindow7 = new google.maps.InfoWindow({
content: createInfo('Lower Marsh Market', '<div style="width:200px">Lower Marsh is steeped in history as one of London’s oldest and best-loved market streets....</div><br /><br />View profile')
});
var marker8 = new google.maps.Marker({
position: new google.maps.LatLng(51.4682204,-0.0259369),
map: map,
icon: image1 // This path is the custom pin to be shown. Remove this line and the proceeding comma to use default pin
});
// Add listener for a click on the pin
google.maps.event.addListener(marker8, 'click', function() {
infowindow8.open(map, marker8);
});
// Add information window
var infowindow8 = new google.maps.InfoWindow({
content: createInfo('Brockley Market', '<div style="width:200px">...</div><br /><br />View profile')
});
var marker9 = new google.maps.Marker({
position: new google.maps.LatLng(51.4528397,-0.1019773),
map: map,
icon: image1 // This path is the custom pin to be shown. Remove this line and the proceeding comma to use default pin
});
// Add listener for a click on the pin
google.maps.event.addListener(marker9, 'click', function() {
infowindow9.open(map, marker9);
});
// Add information window
var infowindow9 = new google.maps.InfoWindow({
content: createInfo('Herne Hill Farmers Market', '<div style="width:200px">...</div><br /><br />View profile')
});
var markerCluster = new MarkerClusterer(options, markers);
// Create information window
function createInfo(title, content) {
return '<div class="infowindow"><strong>'+ title +'</strong><br />'+content+'</div>';
}
});
</script>

Which version of MC you are using? Because version 2.0.x has following construct syntax:
var markerCluster = new MarkerClusterer(map, opt_markers, opt_options);
Try this:
var markerCluster = new MarkerClusterer(map);
markerCluster.addMarker(marker1);
...
markerCluster.addMarker(marker9);
Or, keep Your markers in array, add this to top of Your code:
var markers = [];
After declaring each marker add it to that array:
markers.push(markerN);
And add all of them at once to cluster:
markerCluster.addMarkers(markers);

Related

how to display data on google map info window from database using php mysqli

my sql query to fetch state count and state name that give me count and name of the state.
my problem is that i wanted to display multiple markers on google map from database. I need to display the data on click of marker on info window at google map im trying following code.
I'm not able to understand how to add loop in data.addRows.
or please let me know where I'm doing wrong
$sql = "SELECT `state`, COUNT(DISTINCT `sr_no`) AS `stateno`
FROM `Mystate` GROUP BY `state`";
<html>
<head>
<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
<script type="text/javascript">
google.charts.load('current', {
'packages': ['map'],
// Note: you will need to get a mapsApiKey for your project.
// See: https://developers.google.com/chart/interactive/docs/basic_load_libs#load-settings
'mapsApiKey': 'API_KEY'
});
google.charts.setOnLoadCallback(drawMap);
function drawMap () {
var data = new google.visualization.DataTable();
data.addColumn('string', 'Address');
data.addColumn('string', 'Location');
data.addColumn('string', 'Marker')
data.addRows([
['Mumbai, Maharashtra', 'India', 'blue' ],
['Pune, Maharashtra', 'India', 'green']
]);
var url = 'https://icons.iconarchive.com/icons/icons-land/vista-map-markers/48/';
var options = {
center: { lat: 20.5937, lng: 78.9629 },
zoomLevel: 5,
showTooltip: true,
showInfoWindow: true,
useMapTypeControl: true,
icons: {
blue: {
normal: url + 'Map-Marker-Ball-Azure-icon.png',
selected: url + 'Map-Marker-Ball-Right-Azure-icon.png'
},
green: {
normal: url + 'Map-Marker-Push-Pin-1-Chartreuse-icon.png',
selected: url + 'Map-Marker-Push-Pin-1-Right-Chartreuse-icon.png'
},
pink: {
normal: url + 'Map-Marker-Ball-Pink-icon.png',
selected: url + 'Map-Marker-Ball-Right-Pink-icon.png'
}
}
};
var map = new google.visualization.Map(document.getElementById('map_div'));
map.draw(data, options);
}
</script>
</head>
<body>
<div id="map_div" style="height: 600px; width: 900px"></div>
</body>
</html>
It is not clear how you hope to select the colour of the marker, nor what you actually do with the stateno from the SQL query. That said, the JSON deata in the following is from one of my old databases and was used to emulate what your query might return. The actual task of processing this recordset and adding the data to the dataTable is done with an Object.keys( json ).forEach loop.
ie:
Object.keys( json ).forEach( key=>{
let obj=json[ key ];
data.addRow([
obj.state, 'India', 'green'
]);
})
The full, working example with a hardcoded valud of green for the marker. I would suggest that this colour could be determined by the stateno data as that is not used but nothing in the above supports that. If that were the case a simple lookup of the stateno number against some sort of matrix of value/colour could be done.
I should possibly warn that running and testing this multiple times could lead to charges being raised against your apikey because of the need for each marker location needing to be geocoded in order to find the location.
<html>
<head>
<script src='https://www.gstatic.com/charts/loader.js'></script>
<script>
const json=[
{
"state":"Andaman and Nicobar Islands",
"stateno":"1"
},
{
"state":"Andhra Pradesh",
"stateno":"104"
},
{
"state":"Arunachal Pradesh",
"stateno":"1"
},
{
"state":"Assam",
"stateno":"15"
},
{
"state":"Bihar",
"stateno":"46"
},
{
"state":"Chandigarh",
"stateno":"1"
},
{
"state":"Chhattisgarh",
"stateno":"12"
},
{
"state":"Delhi",
"stateno":"29"
},
{
"state":"Goa",
"stateno":"3"
},
{
"state":"Gujarat",
"stateno":"63"
},
{
"state":"Haryana",
"stateno":"26"
},
{
"state":"Himachal Pradesh",
"stateno":"1"
},
{
"state":"Jammu and Kashmir",
"stateno":"8"
},
{
"state":"Jharkhand",
"stateno":"23"
},
{
"state":"Karnataka",
"stateno":"65"
},
{
"state":"Kerala",
"stateno":"32"
},
{
"state":"Madhya Pradesh",
"stateno":"53"
},
{
"state":"Maharashtra",
"stateno":"90"
},
{
"state":"Manipur",
"stateno":"1"
},
{
"state":"Meghalaya",
"stateno":"2"
},
{
"state":"Mizoram",
"stateno":"2"
},
{
"state":"Nagaland",
"stateno":"3"
},
{
"state":"Orissa",
"stateno":"22"
},
{
"state":"Pondicherry",
"stateno":"3"
},
{
"state":"Punjab",
"stateno":"34"
},
{
"state":"Rajasthan",
"stateno":"50"
},
{
"state":"Tamil Nadu",
"stateno":"85"
},
{
"state":"Tripura",
"stateno":"3"
},
{
"state":"Uttar Pradesh",
"stateno":"119"
},
{
"state":"West Bengal",
"stateno":"93"
}
];
google.charts.load('current', {
'packages': ['map'],
'mapsApiKey': 'AIzaSy..................04......tA'
});
google.charts.setOnLoadCallback(drawMap);
function drawMap () {
var data = new google.visualization.DataTable();
data.addColumn('string', 'Address');
data.addColumn('string', 'Location');
data.addColumn('string', 'Marker');
/*
data.addRows([
['Mumbai, Maharashtra', 'India', 'blue' ],
['Pune, Maharashtra', 'India', 'green']
]);
*/
Object.keys( json ).forEach( key=>{
let obj=json[ key ];
data.addRow([
obj.state, 'India', 'green'
]);
})
var url = 'https://icons.iconarchive.com/icons/icons-land/vista-map-markers/48/';
var options = {
center: { lat: 20.5937, lng: 78.9629 },
zoomLevel: 5,
showTooltip: true,
showInfoWindow: true,
useMapTypeControl: true,
icons: {
blue: {
normal: url + 'Map-Marker-Ball-Azure-icon.png',
selected: url + 'Map-Marker-Ball-Right-Azure-icon.png'
},
green: {
normal: url + 'Map-Marker-Push-Pin-1-Chartreuse-icon.png',
selected: url + 'Map-Marker-Push-Pin-1-Right-Chartreuse-icon.png'
},
pink: {
normal: url + 'Map-Marker-Ball-Pink-icon.png',
selected: url + 'Map-Marker-Ball-Right-Pink-icon.png'
}
}
};
var map = new google.visualization.Map(document.getElementById('map_div'));
map.draw(data, options);
}
</script>
</head>
<body>
<div id='map_div' style='height: 600px; width: 900px'></div>
</body>
</html>
update
Following on from comments about infoWindows etc I modified the working code I had to perform the lookup and return a different colour icon based upon arbitrary figures ( edit as appropriate if applicable ) and modified the data being added to the dataTable so that the 2nd argument contains the name & count - when the marker is clicked that will be displayed in the tooltip/infoWindow
const matrix={
0:'blue',
30:'green',
75:'pink'
};
const lookup=(i)=>{
let icon=false;
Object.keys( matrix ).forEach( key=>{
if( i > key ) icon=matrix[ key ];
});
return icon
};
Object.keys( json ).forEach( key=>{
let obj=json[ key ];
let icon=lookup( obj.stateno );
data.addRow([
obj.state, `${obj.state} - Count: ${obj.stateno}`, icon
])
});

How to send map coordinates to javascript file - with Laravel?

I have a contact page. This page contains map information. This map is generated by the script file. How do I change the coordinates dynamically?
Js file content;
var MY_MAPTYPE_ID = 'style_KINESB';
function initialize() {
var featureOpts = [
{
"featureType": "administrative",
"elementType": "labels.text.fill",
"stylers": [
{
"color": "#666666"
}
]
},
{
"featureType": 'all',
"elementType": 'labels',
"stylers": [
{ visibility: 'simplified' }
]
},
{
"featureType": "landscape",
"elementType": "all",
"stylers": [
{
"color": "#e2e2e2"
}
]
},
{
"featureType": "poi",
"elementType": "all",
"stylers": [
{
"visibility": "off"
}
]
},
{
"featureType": "road",
"elementType": "all",
"stylers": [
{
"saturation": -100
},
{
"lightness": 45
},
{
"visibility": "off"
}
]
},
{
"featureType": "road.highway",
"elementType": "all",
"stylers": [
{
"visibility": "off"
}
]
},
{
"featureType": "road.arterial",
"elementType": "labels.icon",
"stylers": [
{
"visibility": "off"
}
]
},
{
"featureType": "transit",
"elementType": "all",
"stylers": [
{
"visibility": "off"
}
]
},
{
"featureType": "water",
"elementType": "all",
"stylers": [
{
"color": "#aadaff"
},
{
"visibility": "on"
}
]
}
];
var myGent = new google.maps.LatLng(40.6946703,-73.9280182);
var Kine = new google.maps.LatLng(40.6946703,-73.9280182);
var mapOptions = {
zoom: 11,
mapTypeControl: true,
zoomControl: true,
zoomControlOptions: {
style: google.maps.ZoomControlStyle.SMALL,
position: google.maps.ControlPosition.LEFT_TOP,
mapTypeIds: [google.maps.MapTypeId.ROADMAP, MY_MAPTYPE_ID]
},
mapTypeId: MY_MAPTYPE_ID,
scaleControl: false,
streetViewControl: false,
center: myGent
}
var map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
var styledMapOptions = {
name: 'style_KINESB'
};
var image = 'images/resource/mapmarker.png';
var marker = new google.maps.Marker({
position: Kine,
map: map,
animation: google.maps.Animation.DROP,
title: 'B4318, Gumfreston SA70 8RA, United Kingdom',
icon: image
});
var customMapType = new google.maps.StyledMapType(featureOpts, styledMapOptions);
map.mapTypes.set(MY_MAPTYPE_ID, customMapType);
}
google.maps.event.addDomListener(window, 'load', initialize);
The area I want to access through the Laravel blade. I want to make the coordinates dynamic.
var myGent = new google.maps.LatLng(40.6946703,-73.9280182);
var Kine = new google.maps.LatLng(40.6946703,-73.9280182);
contact.blade.php
#extends('layouts.frontend.master')
#section('title', 'Empathy - Become Volunteer')
#section('content')
#endsection
Please help me. Thank you very much for your help.
My solution here. I also worked. I'm waiting if you have better solutions.
Firstly. I am add #yield("javascript") directive in master.blade.php file. I add on to the js file.
#yield('javascript')
<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyAAz77U5XQuEME6TpftaMdX0bBelQxXRlM&callback=initMap" type="text/javascript"></script>
<script src="{{ asset('empathy/frontend/assets/js/googlemaps1.js') }}"></script>
<!-- Custom script for all page -->
<script src="{{ asset('empathy/frontend/assets/js/script.js') }}"></script>
Later edit contact.blade.php
#extends('layouts.frontend.master')
#section('title', 'Empathy - Become Volunteer')
#section('content')
#endsection
#section('javascript')
<script>
var x = JSON.parse('{!! json_encode($coordinate_array[0]) !!}');
var y = JSON.parse('{!! json_encode($coordinate_array[1]) !!}');
</script>
#endsection
ContactPageController.php
$contact = Contact::first();
$coordinate_array = explode(',', $contact->coordinates); // 40.6946703,-73.9280182
This way my solution worked.

Custom Google Makers with DataLayer/GeoJson

I'm currently having an Issue getting google markers to display custom images depending on the type. I have had it working in the past that only had one image for all the markers.
map.php
var map,
infoWindow = new google.maps.InfoWindow({pixelOffset: new google.maps.Size(0,-25)});
function initialize() {
map = new google.maps.Map(document.getElementById('map-canvas'), {
zoom: 12,
center: {lat: 53.927895, lng: -1.386487}
});
map.data.loadGeoJson('http://customers.auroracomputers.co.uk/customers-json.php');
map.data.addListener('click', function(event) {
infoWindow.setContent(
'Surname: ' + event.feature.getProperty('surname') + '<br>' +
'Postcode: ' + event.feature.getProperty('postcode')
);
var anchor = new google.maps.MVCObject();
anchor.set("position",event.latLng);
infoWindow.open(map,anchor);
});
var iconBase = 'http://customers.auroracomputers.co.uk/icons/'
var icons = {
business: {
icon: iconBase + 'business.png'
},
home: {
icon: iconBase + 'home.png'
},
competitor: {
icon: iconBase + 'devil.png'
}
};
function addMarker(feature) {
var marker = new google.maps.Marker({
position: feature.position,
icon: icons[feature.type].icon,
map: map
});
}
map.data.setStyle({
icon: 'http://customers.auroracomputers.co.uk/icons/home.png',
icon: icons[feature.type].icon,
icon: icon.competitor.icon
});
}
google.maps.event.addDomListener(window, 'load', initialize);
Customer-Json.php
for ($i=0;$i<$nrows;$i++){
$row = mysqli_fetch_assoc($result);
?>
<script>
{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [<?echo $row['lng'];?>,<?echo $row['lat'];?>]
},
"properties": {
"surname": "<?echo $row['surname'];?>",
"postcode": "<?echo $row['postcode'];?>",
"type": "<?echo $row['type'];?>"
}
}<?php if( $i != $nrows-1 ){ ?>,<?php } ?>
</script>
GeoJson markers are styled differently than native Google Maps Javascript API v3 markers. See Style GeoJSON Data in the documentation, in particular the section on Change Appearance Dynamically.
map.data.setStyle(function(feature) {
var type = feature.getProperty('type')
return /** #type {google.maps.Data.StyleOptions} */ ({
icon: icons[type].icon,
title: type
});
});
from the reference:
icon Type: string|Icon|Symbol
Icon for the foreground. If a string is provided, it is treated as though it were an Icon with the string as url. Only applies to point geometries.
proof of concept fiddle
code snippet:
var map,
infoWindow = new google.maps.InfoWindow({
pixelOffset: new google.maps.Size(0, -25)
});
function initialize() {
map = new google.maps.Map(document.getElementById('map-canvas'), {
zoom: 10,
center: {
lat: 40.7127837,
lng: -74.0059413
}
});
// map.data.loadGeoJson('http://customers.auroracomputers.co.uk/customers-json.php');
map.data.addGeoJson(testGeoJson)
map.data.addListener('click', function(event) {
infoWindow.setContent(
'Surname: ' + event.feature.getProperty('surname') + '<br>' +
'Postcode: ' + event.feature.getProperty('postcode')
);
var anchor = new google.maps.MVCObject();
anchor.set("position", event.latLng);
infoWindow.open(map, anchor);
});
var iconBase = 'http://customers.auroracomputers.co.uk/icons/'
var icons = {
business: {
icon: iconBase + 'business.png'
},
home: {
icon: iconBase + 'home.png'
},
competitor: {
icon: iconBase + 'devil.png'
}
};
map.data.setStyle(function(feature) {
var type = feature.getProperty('type')
return /** #type {google.maps.Data.StyleOptions} */ ({
icon: icons[type].icon,
title: type
});
});
}
google.maps.event.addDomListener(window, 'load', initialize);
// New York, NY, USA (40.7127837, -74.00594130000002)
// Newark, NJ, USA (40.735657, -74.1723667)
var testGeoJson = {
"type": "FeatureCollection",
"features": [{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [-74.0059413, 40.7127837]
},
"properties": {
"surname": "York",
"postcode": " 10007",
"type": "home"
}
}, {
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [-74.1723667, 40.735657]
},
"properties": {
"surname": "Newark",
"postcode": "07102",
"type": "business"
}
}]
};
html,
body,
#map-canvas {
height: 100%;
width: 100%;
margin: 0px;
padding: 0px
}
<script src="https://maps.googleapis.com/maps/api/js"></script>
<div id="map-canvas"></div>
One thing I note is the source json is corrupted, hopefully the following might help get that sorted.
<?php
/*
Customer-Json.php
-----------------
The original code had thousands of `<script>` & `</script>` tags
which meant it was not valid json.
I have assumed that the previous loop can be replaced with the
more usual recordset iteration loop as shown below.
Each row has items added to the json array which gets echoed at the
end - there is not need for the script tags at all - just include the
correct headers.
*/
/* store data to be sent */
$json=array();
/* using object notation for ease */
while( $row=mysqli_fetch_object( $result ) ){
$surname=$row->surname;
$postcode=$row->postcode;
$lat=$row->lat;
$lng=$row->lng;
$type=$row->type;
$json[]=array(
'type' => 'Feature',
'geometry' => array(
'type' => 'Point',
'coordinates' => array( $lng, $lat )
),
'properties' => array(
'surname' => $surname,
'postcode' => $postcode,
'type' => $type
)
);
}
$json=json_encode( $json );
header( 'Content-Type: application/json' );
echo $json;
?>

How can we replace A,B,C .. to 1 ,2,3.. in waypoints Google map API?

We need some help. We have below Google Map code which is working fine. Only thing is we need to customise the text in waypoints to integer.Will it be possible?
This is how we want : http://screencast.com/t/TKFOAB7UQ
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
<title>Google Maps API</title>
<script type="text/javascript" src="http://maps.google.com/maps/api/js? sensor=false&libraries=places"></script>
</head>
<body style="font-family: Arial; font-size: 12px;">
<div style="width: 100%">
<div id="map-canvas" style="width: 950px; height: 400px; float: left;"></div>
<div id="panel" style="width: 300px; float: left;"></div>
</div>
<script type="text/javascript">
function initialize() {
var directionsService = new google.maps.DirectionsService();
var directionsDisplay = new google.maps.DirectionsRenderer();
var map = new google.maps.Map(document.getElementById('map-canvas'), {
zoom:7,
mapTypeId: google.maps.MapTypeId.ROADMAP,
styles: [ { featureType:'water', stylers:[{color:'#46bcec'},{visibility:'on'}] },{ featureType:'landscape', stylers:[{color:'#f2f2f2'}] },{ featureType:'road', stylers: [{saturation:-100},{lightness:45}] },{ featureType:'road.highway', stylers: [{visibility:'simplified'}] },{ featureType:'road.arterial', elementType:'labels.icon', stylers: [{visibility:'off'}] },{ featureType:'administrative', elementType:'labels.text.fill', stylers: [{color:'#444444'}] },{ featureType:'transit', stylers:[{visibility:'off'}] },{ featureType:'poi', stylers:[{visibility:'off'}] }],
});
directionsDisplay.setMap(map);
// directionsDisplay.setPanel(document.getElementById('panel'));
var waypts = [{location:new google.maps.LatLng(1.33202, 103.67673), stopover:true}, { location:new google.maps.LatLng(1.29418, 103.84943), stopover:true}];
var request = {
origin: new google.maps.LatLng(1.25675, 103.82033),
destination: new google.maps.LatLng(1.28627, 103.85927),
waypoints: waypts,
travelMode: google.maps.DirectionsTravelMode.DRIVING
};
directionsService.route(request, function(response, status) {
if (status == google.maps.DirectionsStatus.OK) {
directionsDisplay.setDirections(response);
}
});
}
initialize();
</script>
</body>
</html>
What you can do, is not display the markers with letters and make your own.
Here is an example.
Look at my
- line 4: suppressMarkers : true
- line 40: here you can also change the color of the icon. Look at the link for more info.
function initialize() {
var directionsService = new google.maps.DirectionsService();
var directionsDisplay = new google.maps.DirectionsRenderer({
suppressMarkers : true
});
var map = new google.maps.Map(document.getElementById('map-canvas'), {
zoom:7,
mapTypeId: google.maps.MapTypeId.ROADMAP,
styles: [ { featureType:'water', stylers:[{color:'#46bcec'},{visibility:'on'}] },{ featureType:'landscape', stylers:[{color:'#f2f2f2'}] },{ featureType:'road', stylers: [{saturation:-100},{lightness:45}] },{ featureType:'road.highway', stylers: [{visibility:'simplified'}] },{ featureType:'road.arterial', elementType:'labels.icon', stylers: [{visibility:'off'}] },{ featureType:'administrative', elementType:'labels.text.fill', stylers: [{color:'#444444'}] },{ featureType:'transit', stylers:[{visibility:'off'}] },{ featureType:'poi', stylers:[{visibility:'off'}] }],
});
directionsDisplay.setMap(map);
// directionsDisplay.setPanel(document.getElementById('panel'));
var waypts = [
{location:new google.maps.LatLng(1.33202, 103.67673), stopover:true},
{location:new google.maps.LatLng(1.29418, 103.84943), stopover:true}
];
var request = {
origin: new google.maps.LatLng(1.25675, 103.82033),
destination: new google.maps.LatLng(1.28627, 103.85927),
waypoints: waypts,
travelMode: google.maps.DirectionsTravelMode.DRIVING
};
directionsService.route(request, function(response, status) {
if (status == google.maps.DirectionsStatus.OK) {
var markerCounter = 1;
directionsDisplay.setDirections(response);
// add custom markers
var route = response.routes[0];
// start marker
addMarker(route.legs[0].start_location, markerCounter++);
// the rest
for (var i = 0; i < route.legs.length; i++) {
addMarker(route.legs[i].end_location, markerCounter++);
}
}
});
function addMarker(position, i) {
return new google.maps.Marker({
// #see http://stackoverflow.com/questions/2436484/how-can-i-create-numbered-map-markers-in-google-maps-v3 for numbered icons
icon: 'http://chart.apis.google.com/chart?chst=d_map_pin_letter&chld=' + i + '|FF0000|000000',
position: position,
map: map
})
}
}
google.maps.event.addDomListener(window, 'load', initialize);

Gmap3: How to add title to marker?

I have working php script that stores the name and address of the company in variables. I then echo it out like this:
$(document).ready(function() {
$("#map").gmap3({
marker:{
address: "<?echo $coord;?>"
},
map:{
options:{
zoom:15,
mapTypeControl: true,
navigationControl: true,
scrollwheel: true,
streetViewControl: true
}
}
});
});
How would I add the name of the company to show below the marker? For example: this?
I tried to add:
marker:{
address: "<?echo $coord;?>"
title: "<?echo $com_name;?>"
}
But this doesn't work.
Thank you.
The title property must be inside option property.
var marker = {
latLng: [53.23564, 24.32654],
data: 'some data',
options: {
title: 'Title',
icon: '/Path/to/custom/icon/if/you/need.png'
}
}
Other information you can show with infowindow.
Example:
$(".gmap-widget .gmap3").gmap3({
action: "addMarkers",
markers: markers,
marker: {
events: {
click: function(marker, event, data){
var map = $(this).gmap3('get');
infowindow = $(this).gmap3({action:'get', name:'infowindow'});
if (infowindow){
infowindow.open(map, marker);
infowindow.setContent(data);
} else {
$(this).gmap3({action:'addinfowindow', anchor:marker, options:{content: data, maxWidth: 300}});
}
}
}
}
});

Categories