I found this file mappanslow.html last week on
http://code.google.com/p/gmaps-samples-v3/source/browse/trunk/talks/io2011/map-usability/mappanslow.html?spec=svn265&r=265
and it was very useful for me to know how to get data from a json
file. I'm re-coding in V3 this map at present
http://www.ville.rimouski.qc.ca/styles/carte/carte.jsp and my problem
is that the user must be able to choose which data (from different
json file) will be displayed by selecting what he wants in the select
box under "Imprimer".
mappaslow.html works fine with one json file but I tried to use the
code for more than one json file and, if I list, per example, all my
files like this:
<script type="text/javascript" src="file1.json"></script>
<script type="text/javascript" src="file2.json"></script>
<script type="text/javascript" src="file3.json"></script>
the data displayed are always from the last one (file3.json).
Does anyone please have an idea how to get around this issue?
There is the code from mappaslow.html:
<script type="text/javascript" src="markers.json"></script>
<script type="text/javascript">
var map;
var timeout;
var m = [];
function init() {
map = new google.maps.Map(document.getElementById('map'), {
center: new google.maps.LatLng(44.85471, -93.24185),
zoom: 17,
mapTypeId: google.maps.MapTypeId.ROADMAP,
mapTypeControl: false
});
google.maps.event.addListener(map, 'bounds_changed', function() {
clearMarkers();
if (timeout) {
window.clearTimeout(timeout);
}
timeout = window.setTimeout(function() {
addMarkers();
}, 500);
});
}
function addMarkers() {
var bounds = map.getBounds();
for (var i = 0; i < markers.length; i++) {
var pos = new google.maps.LatLng(markers[i].lat, markers[i].lng);
if (bounds.contains(pos)) {
var marker = new google.maps.Marker({
map: map,
position: pos
});
m.push(marker);
}
}
}
function clearMarkers() {
for (var i = 0, marker; marker = m[i]; i++) {
marker.setMap(null);
}
m = [];
}
google.maps.event.addDomListener(window, 'load', init);
</script>
The json files used in this example and for my map look like this:
var markers = [
{"lat":44.86065444748757, "lng":-93.26223975870084},
{"lat":44.84990797511595, "lng":-93.22287578639873}];
Thanks for your suggestion #harmlessdragon, but my hope was to be able to specify the right variable just by passing the name of the selected .json in parameters, because their structure are identical. Something like:
function addMarkers(jsonName) {
var rightJson = jsonName;
var bounds = map.getBounds();
for (var i = 0; i < rightJson.markers.length; i++) {
var pos = new google.maps.LatLng(rightJson.markers[i].lat, rightJson.markers[i].lng);
...
Is it possible to do something like this? I tried many ways but always resulting in syntax error or something undefined.
Don't forget the target is the map of the second link above..
#harmlessdragon, I finally found the correct syntax for the json file:
var markers = [{
"file1": [
{"lat": "number", "lng": "number"}, {"lat": "number", "lng": "number"},
]},{"file2": [
{"lat": "number", "lng": "number"}, {"lat": "number", "lng": "number"},
]},{"file3": [
{"lat": "number", "lng": "number"}, {"lat": number, lng: number"}
]
}];
I see with an alert that the right number of "files" is recognized by the first loop. The problem now is that anything passes into second loop:
for (var t = 0; t < markers[i].length; t++) {
MOST RECENT EDIT:
I'm going off this example here:
http://www.json.org/js.html
var myJSONObject = {"bindings": [
{"ircEvent": "PRIVMSG", "method": "newURI", "regex": "^http://.*"},
{"ircEvent": "PRIVMSG", "method": "deleteURI", "regex": "^delete.*"},
{"ircEvent": "PRIVMSG", "method": "randomURI", "regex": "^random.*"}
]
};
myJSONObject.bindings[0].method // "newURI"
Your loop should look similar to this:
function addMarkers(param) {
if (param == "1") {
// if it is for file one, loop through all in file one
for (t = 0; t < markers.file1.length; t++ ) {
var pos = new google.maps.LatLng(markers.file1[t].lat, markers.file1[t].lng)
// then create marker, push map, etc.
}
} else if (param == "2") {
// if it is for file two, loop through all in file two instead
for (t = 0; t < markers.file2.length; t++ ) {
var pos = new google.maps.LatLng(markers.file2[t].lat, markers.file2[t].lng)
// then create marker, push map, etc.
}
} else if ( param == "3") {
for (t = 0; t < markers.file3.length; t++ ) {
var pos = new google.maps.LatLng(markers.file3[t].lat, markers.file3[t].lng)
// then create marker, push map, etc.
}
}
}
/////////////////////////////////////////////////////////////////////////////////////////
You can't store different data under the same name and retrieve the data without some indicator that differentiates the data. If you want to use the same variable name, then you will need to add that additional indicator into your json and merge your json files.
You can do something like this:
var markers = [
file1: {
{lat: number, lng: number}, {lat: number, lng: number}, etc.
}
file2: {
{lat: number, lng: number}, {lat: number, lng: number}, etc.
}
file3: {
{lat: number, lng: number}, {lat: number, lng: number}, etc.
}
];
Then, to retrieve the data, you'll need to add in another loop. This json structure is an array..within an array...within an array, which I don't think is the most efficient.
function addMarkers(param) {
...
for (var i = 0; i < markers.length; i++) {
for (var t = 0; t < markers[i].length; t++) {
if (markers[i] == param) {
var pos = new google.maps.LatLng(markers[i].lat, markers[i].lng);
if (bounds.contains(pos)) {
var marker = new google.maps.Marker({
map: map,
position: pos
});
m.push(marker);
}
}
}
You may want to consider looking into a hash map structure. Here's one that I just looked up:
http://freecode-freecode.blogspot.com/2007/06/hashmap-object-in-javascript-like.html
Some syntax may be wrong in the above code, as I don't regularly code in Javascript, but I should convey the general idea of what needs to be done.
//////////////////////////////////////////////////////////////////////////////////////////
Perhaps what we needed was information on file1.json, file2.json, and file3.json.
I'm going on a few assumptions, but here's my guess of what's happening:
If you have files1.json, file2.json, file3.json, etc., and you're following the example of having var markers = [] then you have created 3 variables with the same name, which are overwriting each other, which is why files3.json, the last one you have created, is the one that remains.
Here's what you need to pay attention to if you want this example to work for you:
// This method adds a marker by looping through the variable
// with the name 'markers', which you created
// in the JSON file with var markers = [].
function addMarkers() {
var bounds = map.getBounds();
for (var i = 0; i < markers.length; i++) {
var pos = new google.maps.LatLng(markers[i].lat, markers[i].lng);
if (bounds.contains(pos)) {
var marker = new google.maps.Marker({
map: map,
position: pos
});
m.push(marker);
}
}
}
If you want this example to work, you will need to make sure the json files have a variable of a different name, and make sure that the above method is changed for the new names. That might mean you will have 3 different function: addJSONFile1Markers, addJSONFile2Markers, addJSONFile3Markers, where each function handles a different variable. Alternatively, you can create a function that checks based on a parameter, which file to add markers for.
function addMarkers(FILE2) {
if (FILE2 == "file2") {
// do this for file2
}
else if (FILE3 == "file3") {
// do this for file 3
}
}
This is just an example in pseudo code.
If this isn't the problem, then please provide more information regarding the json files.
Related
Currently i am able to display 3 route of 3 drivers respectively(one for each) on a single Gmap.
Now, I wish to show route of 'n' number of drivers (single route for each driver)on Gmap. Its a case of dynamic drivers. I can get data from db for 'n' number of drivers that i need to display on a single map.
My Code is below for single driver please help me for dynamic craetion of routes:
var lat_lng1 = [];
var latlngbounds1 = "";
lat_lng1 = new Array();
var value1 = markers1.length;
//Intialize the Path Array
var path1 = new google.maps.MVCArray();
//Intialize the Direction Service
var service1 = new google.maps.DirectionsService();
var lineSymbol1 = { path1: google.maps.SymbolPath.CIRCLE };
//Set the Path Stroke Color
for (i = 0; i < markers1.length; i++) {
var data1 = markers1[i]
var myLatlng1 = new google.maps.LatLng(data1.lat, data1.lng);
lat_lng1.push(myLatlng1);
var marker1 = new google.maps.Marker({position: myLatlng1,map: map,icon: icon1});
(function (marker1, data1) {
// Attaching a click event to the current marker
google.maps.event.addListener(marker1, "click", function (e) {
if (i == 0){
infoWindow.setContent(data1.Person);infoWindow.open(map, marker1);}
if(i=(markers2.length -1)){
infoWindow.setContent(data1.Person);infoWindow.open(map, marker1);}
else{
infoWindow.setContent(data1.Title);
infoWindow.open(map, marker1);}
});
})(marker1, data1);
}
poly = new google.maps.Polyline({
// path: lineCoordinates,
strokeColor: '#BC456F',
icons: [{
icon: {
path: google.maps.SymbolPath.FORWARD_CLOSED_ARROW,strokeColor: '#009900',fillColor: '#009900',fillOpacity: 1},
repeat: '100px',
path1: []
}],
map: map
});
well I solved this issue the very next day by calling ajax loops by driver id
I have a google Map with markers on a webpage.
Each marker has a unique feature position and type
This is the scenario I would like to put in place:
On another webpage I have static links to different markers of the map.
If you click on one of those links, you are directed to the map in which, one of these markers is centered (and its info window open).
But the markers latitude and longitude might change while the links will never change.
This means, I need the links not to use latitude and longitude info but markers feature type instead (which are remain the same).
How can I do that?
Here is my sample google Map script so far:
<script>
function initMap() {
var mapDiv = document.getElementById('map');
var map = new google.maps.Map(mapDiv, {
center: {lat: 48.85639, lng: 2.33625}, // default centering
zoom: 18,
styles:
[
{featureType: 'poi',stylers: [{ visibility: 'off' }]},
{featureType: 'transit.station',stylers: [{ visibility: "off" }]}
]
});
var features = [
{position: new google.maps.LatLng(48.85659, 2.33555),type: 'markerone'},
{position: new google.maps.LatLng(48.85619, 2.33695),type: 'markertwo'}
];
var icons = {
'markerone': {icon: 'icon_one.png'},
'markertwo': {icon: 'icon_two.png'}
};
var contents= {
'markerone': {text: 'Content 1'},
'markertwo': {text: 'Content 2'}
};
for (var i = 0, feature; feature = features[i]; i++)
{
var marker = new google.maps.Marker({
position: feature.position,
icon: icons[feature.type].icon,
map: map
});
var content = contents[feature.type].text;
var infowindow = new google.maps.InfoWindow()
google.maps.event.addListener(marker,'mouseover', (function(marker,content,infowindow){
return function() {
infowindow.setContent(content);
infowindow.open(map,marker);
};
})(marker,content,infowindow));
google.maps.event.addListener(marker,'mouseout', (function(marker,content,infowindow){
return function() {
infowindow.close(map,marker);
};
})(marker,content,infowindow));
}
}
</script>
In this sample, I have to markers.
One has a feature type of "markerone" and the second is "markertwo".
How can I set my links to redirect and center the map around a specific marker in this kind of fashion:
http://www.mywebsite.com/mymap.php?myvariable=markertwo
Thank you.
First you would have to get the parameters. The example below gets all parameters and put them into an array. There you can search for your certain paramater like "markerType" and check if it's given or not. If not you have to perform a default action, otherwise you can handle the certain markerType like finding the correct marker, setting the map center to it and open the corrosponding infoWindow.
You just have to call the focusMarkerType-method onload of your page.
function getSearchParameters() {
var prmstr = window.location.search.substr(1);
return prmstr != null && prmstr != "" ? transformToAssocArray(prmstr) : {};
}
function transformToAssocArray( prmstr ) {
var params = {};
var prmarr = prmstr.split("&");
for ( var i = 0; i < prmarr.length; i++) {
var tmparr = prmarr[i].split("=");
params[tmparr[0]] = tmparr[1];
}
return params;
}
function focusMarkerType(){
var params = getSearchParameters();
if(params.markerType!=null){
//Handling the certain marker type
var found = false;
for (var i = 0, feature; feature = features[i]; i++) {
if (feature.type == params.markerType) {
found = true;
map.setCenter(feature.position);
//more...
break;
}
}
if (!found) {
console.log("unknown type")
}
}else{
//Handling default behaviour if no marker type is given
}
}
I've created the following testcase in JSFiddle: http://jsfiddle.net/4Wtb3/15/. When I check a checkbox for route A or route B, the routes are displayed. So far, so good ...
if (routes.length > 0) {
for (var i = 0; i < routes.length; i++) {
var route = routes[i];
var request = {
origin: route.origin,
destination: route.destination,
travelMode: google.maps.TravelMode.WALKING
};
var directionsDisplay = new google.maps.DirectionsRenderer({
suppressMarkers: true,
preserveViewport: true,
polylineOptions: {
strokeColor: '#C6D300'
}
});
directionsDisplay.setMap(map);
directionsDisplays.push(directionsDisplay);
directionsService.route(request, function (result, status) {
if (status == google.maps.DirectionsStatus.OK) {
directionsDisplay.setDirections(result);
}
});
}
}
But when I uncheck them, I want to clear the selected route. I keep track of all directionDisplay's using an array (to put the map value to 'null'). First I clear everything, then I redraw every route which is still in the list.
The problem: routes are still in the map, even if I uncheck them ...
Any idea's what exactly is going wrong?
There is no length of array in for loop so setMap(null) is never called. Code should be changed from
if (directionsDisplays.length > 0) {
for(var i = 0; i < directionsDisplays; i++) {
directionsDisplays[i].setMap(null);
}
}
to
if (directionsDisplays.length > 0) {
for(var i = 0; i < directionsDisplays.length; i++) {
directionsDisplays[i].setMap(null);
}
}
...
And additional fix for closure problem:
// Draw all routes
if (routes.length > 0) {
for (var i = 0; i < routes.length; i++) {
(function(i){
var route = routes[i];
var request = {
origin: route.origin,
destination: route.destination,
travelMode: google.maps.TravelMode.WALKING
};
....
})(i);
}
}
Updated example at jsfiddle
I have got a map that I want to have a feed of lat/long data getting pushed to the map data array. I have the function to get the data, but am having trouble getting that to be usable in the map data array.
The idea is to have a new marker drop in when a new coordinate is added to the array. Any ideas? Thanks in advance!
var ID='0';
var DATA=[];
function getData(){
var url = 'http://us7.fieldagent.net/api/newResponses/';
//url = 'http://us7.fieldagent.net/api/newResponses/;
$.post(url,{'id':ID},function(data){
if(data.status_id == 0){
ID = data.id;
console.log('Last Id: '+data.id);
var new_data = data.responses;
var count = 0
$.each(new_data,function(i,v){
count += 1;
var coord = 'new google.maps.LatLng('+v.lat+','+v.lon+'),';
DATA.push(coord);
})
console.log('Added '+count+' responses..')
}
});
}
$(document).ready(function(){
getData();
setInterval(getData,20*1000);
});
function drop() {
for (var i = 0; i < DATA.length; i++) {
setTimeout(function() {
addMarker();
}, i * 500);
}
}
function addMarker(){
markers.push(new google.maps.Marker({
position: DATA[iterator],
map: map,
draggable: false,
icon: 'fatie.svg',
animation: google.maps.Animation.DROP
}));
iterator++;
}
You need to actually add the item to the map. Right now, you're only adding an item to your DATA array. You need to call addMarker with the new data as well.
You seem to want to add these markers to the map at an interval so they drop onto the map over time, while also being able to query for new markers from your server.
Try code like this:
var ID='0';
var DATA=[];
function getData(){
var url = 'http://us7.fieldagent.net/api/newResponses/';
$.post(url,{'id':ID},function(data){
if(data.status_id == 0){
ID = data.id;
console.log('Last Id: '+data.id);
var new_data = data.responses;
var count = 0
$.each(new_data,function(i,v){
count += 1;
var coord = 'new google.maps.LatLng('+v.lat+','+v.lon+'),';
DATA.push(coord);
});
console.log('Added '+count+' responses..');
if (count > 0) addMarker(); //call addMarker if there are new markers
}
});
}
$(document).ready(function(){
getData();
setInterval(getData,20*1000);
});
function addMarker(){
if (DATA.length == 0) return; //exit if DATA is empty
markers.push(new google.maps.Marker({
position: DATA.shift(), //take the first item in DATA
map: map,
draggable: false,
icon: 'fatie.svg',
animation: google.maps.Animation.DROP
}));
if (DATA.length > 0) setTimeout(addMarker, 500); //call again if needed
}
Create a Method which does two things.
Add to the Array
Add the Item to the map
I am running multiple ajax calls to download a large number of google maps icons. When I try to increment the Marker Clusterer, however, the map clears all markers. I believe this is because I am calling var markerCluster = new MarkerCluster(map); in each AJAX call.
Can anyone tell me how to correctly implement this?
var populateMapByIncident = function(incident, page) {
var run_again = false;
$.getJSON(
"/public_map_ajax_handler",
{"shortname" : incident, "page": page},
function(sites_list) {
if (sites_list.length > 2) {
run_again = true;
}
var mapOptions = {
zoom: 8,
center: new google.maps.LatLng(40.6501038, -73.8495823),
mapTypeId: google.maps.MapTypeId.ROADMAP
}
var map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions);
var markers = [];
var i = 0;
for (var i = 0; i < sites_list.length; i++) {
var latLng = new google.maps.LatLng(sites_list[i].latitude, sites_list[i].longitude);
var marker = new google.maps.Marker({'position': latLng,
'icon': getMarkerIcon(sites_list[i]),
'site_id': sites_list[i].id,
'case_number': sites_list[i].case_number,
'work_type': sites_list[i].work_type,
'floors_affected': sites_list[i].floors_affected,
'status': sites_list[i].status});
markers.push(marker);
var site_id = sites_list[i].id;
google.maps.event.addListener(marker, "click", function() {
new Messi('<p>Name, Address, Phone Number are removed from the public map</p><p>Details: work type: '
+ this.work_type+ ', floors affected: ' + this.floors_affected + '</p>' + '<p>Status: ' + this.status + '</p>',
{title: 'Case Number: ' + this.case_number, titleClass: 'info',
buttons: [
{id: 0, label: 'Printer Friendly', val: "On the live version, this would send all of this site's data to a printer friendly page." },
{id: 1, label: 'Change Status', val: "On the live version, you would be able to change the site's status here."},
{id: 2, label: 'Edit', val: "On the live version, you would be able to edit the site's info, as new details come in."},
{id: 3, label: 'Claim', val: "On the live version, clicking this button would 'Claim' the site for your organization, letting other organizations know that you intend to work on that site"},
{id: 4, label: 'Close', val: 'None'}], callback: function(val) { if (val != "None") {Messi.alert(val);} }});
});
}
var markerCluster = new MarkerClusterer(map);
markerCluster.addMarkers(markers);
if (run_again == true) {
populateMapByIncident(incident, page + 1, markers);
} else {
markerCluster.addMarkers(markers);
}
}
);
}
I am running multiple ajax calls to download a large number of google maps icons. When I try to increment the Marker Clusterer, however, the map clears all markers. I believe this is because I am calling var markerCluster = new MarkerCluster(map); in each AJAX call.
Can anyone tell me how to correctly implement this?
Don't do that. Create the MarkerClusterer one time in the global scope (outside of any function), and add markers to it when you receive them from the server (assuming you aren't sending any duplicates).
See the documentation
Looks like you are already adding arrays of markers to the MarkerClusterer:
addMarkers(markers:Array., opt_nodraw:boolean) | None | Add an array of markers to the clusterer.
All you really need to do is move where you create the MarkerClusterer to the global scope. One suggestion below.
var markerCluster = new MarkerClusterer(map); // <------------- add this
var populateMapByIncident = function(incident, page) {
var run_again = false;
$.getJSON(
// ----- existing code ------- //
// leave as is
// ----- modification -------- //
// var markerCluster = new MarkerClusterer(map); <----------- remove this
markerCluster.addMarkers(markers);
if (run_again == true) {
populateMapByIncident(incident, page + 1, markers);
} else {
markerCluster.addMarkers(markers);
}