GeoXml3 parse fusion table layer of kml (points) into Google Maps - google-maps

I have a kml of points loaded into fusion table layer. I want to parse the data to une map.fitBounds on the layer extent using geoxml3, but thats not functionning. This exactly code below is working with KML polygons but not with KML points layer.
Code:
var queryText = encodeURIComponent("SELECT * FROM 1CNJWjLDYgBkJGZVslJ67Fak4DyqadEFuIabzQ60 ");
var query = new google.visualization.Query('http://www.google.com/fusiontables/gvizdata?tq=' + queryText);
query.send(zoomTo);
}
function zoomTo(response) {
if (!response) {
alert('no response');
return;
}
if (response.isError()) {
alert('Error in query: ' + response.getMessage() + ' ' + response.getDetailedMessage());
return;
}
FTresponse = response;
//for more information on the response object, see the documentation
//http://code.google.com/apis/visualization/documentation/reference.html#QueryResponse
numRows = response.getDataTable().getNumberOfRows();
numCols = response.getDataTable().getNumberOfColumns();
var geoXml = new geoXML3.parser();
var bounds = new google.maps.LatLngBounds();
for (var i = 0; i < numCols; i++){
if (FTresponse.getDataTable().getColumnLabel(i) == 'geometry') {
var ColIndex = i;
}
}
if (!ColIndex){
alert('Geometry column "geometry" not found.')
}
for (var i = 0; i < numRows; i++){
var kml = FTresponse.getDataTable().getValue(i,ColIndex);
geoXml.parseKmlString("<Placemark>"+kml+"</Placemark>");
bounds.union(geoXml.docs[i].bounds);
}
map.fitBounds(bounds);
}

The parse method should not be used for parsing KML strings from FusionTables (the parseKmlString method is for doing that).
var kml = FTresponse.getDataTable().getValue(i,ColIndex);
geoXml.parseKmlString("<Placemark>"+kml+"</Placemark>");
Note: the KML fragments stored in FusionTables do not include the <Placemark> tags which geoxml3 looks for, that is why they are added to the string passed to geoxml3.
The GViz query response has a 500 row limit (that doesn't seem to be documented anywhere I could find, the best I could find was this reference to it, but the documentation has moved since then).
Looks like you are going to run into that limitation with your table, to overcome that use the FusionTables API v1.0, that returns GeoJSON, not KML (so you will no longer need geoxml3).
example that decodes KML "Points" from FusionTables using GViz and geoxml3 (table contains less than 500 points)
example of parsing markers from Fusion Tables using the Fusion Tables API v1.0

Related

How to get places on google map route

How to get all places like Entertainment, food, gas station etc on google map plotted route from source to destination only on route not nearby.
To build on the link that #geocodezip gave, to work around query limits it might be a better practice to make a bounding box based on steps rather than using the Google Maps utility, and then filter locations by actual distance to a node in the step. This could be accomplished using the following code:
In initialize(), put:
var placeserv = null;
function initialize(){
//setup map
placeserv = new google.maps.PlacesService(map); //use your map variable
}
In the callback function for the Directions Service (using parameters results and status), you can include something like this:
var route = results[0];
var steps = route.legs[0].steps;
for(var i=0; i<steps.length; i++){
var lats = steps[i].path.map(function(a){return a.lat()});
var lngs = steps[i].path.map(function(a){return a.lng()});
var box = new google.maps.LatLngBounds(
new google.maps.LatLng(Math.max.apply(null, lats), Math.max.apply(null, lngs)),
new google.maps.LatLng(Math.min.apply(null, lats), Math.min.apply(null, lngs))
);
placeserv.radarSearch(yourRequest_seeDocumentation_or_geocodezipsCode,
function(r,s){callback(r,s,steps[i].path)}
);
//depending on the number of queries you need to make, you may to add in
//some setTimeouts
}
And then add a callback function for your call that checks if the route is within a specified degree. If so, it will do something with the location. (By the way, this requires the Geometry Library for Google Maps. Please look it up.)
var minimumDist = 300 //within 300 meters of a point on the route
function callback(results, status, stepPts){
if(results == 'OK'){
for(var j=0; j<results.length; j++){
for(var k=0; k<stepPts.length; k++){
var dist = google.maps.geometry.spherical.computeDistanceBetween(stepPts[k], results[j].geometry.location);
if(dist < minimumDist){
//do something with the location
}
}
}
}
}
Again, geocodezip has a very complete and excellent post in the link he gave you. This is just the implementation I would use to cut down on Places Service calls.

Drawing polygons with WKT - XML file using OpenLayers

I have this XML file created with PHP and Mysql:
How can I use this to draw all the polygons using OpenLayers? I did some research and found examples with WKT, but in those examples they used just one polygon:
var feature = format.readFeature(
'POLYGON((10.689697265625 -25.0927734375, 34.595947265625 ' +
'-20.1708984375, 38.814697265625 -35.6396484375, 13.502197265625 ' +
'-39.1552734375, 10.689697265625 -25.0927734375))');
I want to draw all the polygons to create a thematic map based on the "Area" data.
Just parse your XML and iterate over the marker tags. Something like this:
var xml = new OpenLayers.Format.XML(),
wkt = new OpenLayers.Format.WKT(),
vectorLayer = new OpenLayers.Layer.Vector('features'),
doc, markers, i, feature;
OpenLayers.Request.GET({
url: "features.xml",
success: function(request) {
doc = xml.read(request.responseText);
markers = doc.documentElement.getElementsByTagName('marker');
for (i = 0; i < markers.length; i++) {
feature = wkt.read(markers[i].attributes.geometry.nodeValue);
vectorLayer.addFeatures([feature]);
}
}
});
Edit: If you are using OpenLayers 3, try the following:
var wkt = new ol.format.WKT(),
vectorLayer,
source,
features = [],
feature,
markers;
//make sure that jQuery is included
$.ajax('features.xml').then(function(response) {
var markers = response.getElementsByTagName('marker');
for (var i = 0; i < markers.length; i++) {
feature = wkt.readFeature(markers[i].attributes.geometry.nodeValue);
features.push(feature);
}
source = new ol.source.Vector({
features: features
});
vectorLayer = new ol.layer.Vector({
source: source
});
});

How to create a direct link to access a specific Google Maps API V3 marker?

I'm creating a ASP.NET MVC3 website using a Google Maps API V3 with a lot of markers. Each marker has an InfoWindow with some information about this place.
I wish each marker have a direct link to access my website directly on this marker, like http://www.mywebsite/1589. So the user could access the website with the map centered on marker 1589 and its InfoWindows would be open.
The markers are already on the map and their InfoWindow are already displaying informations, but I have no idea how to create a direct link to the marker... Could anybody help me ?
Thanks in advance
The key pieces are:
parse the query string
// skip the first character, we are not interested in the "?"
var query = location.search.substring(1);
// split the rest at each "&" character to give a list of "argname=value" pairs
var pairs = query.split("&");
for (var i=0; i<pairs.length; i++) {
// break each pair at the first "=" to obtain the argname and value
var pos = pairs[i].indexOf("=");
var argname = pairs[i].substring(0,pos).toLowerCase();
var value = pairs[i].substring(pos+1).toLowerCase();
// process each possible argname - use unescape() if theres any chance of spaces
if (argname == "id") {id = unescape(value);}
if (argname == "marker") {index = parseFloat(value);}
}
open the infowindow after loading the markers, if a parameter is passed
// ========= If a parameter was passed, open the info window ==========
if (id) {
if (idmarkers[id]) {
google.maps.event.trigger(idmarkers[id],"click");
} else {
alert("id "+id+" does not match any marker");
}
}
if (index > -1) {
if (index < gmarkers.length) {
google.maps.event.trigger(gmarkers[index],"click");
} else {
alert("marker "+index+" does not exist");
}
}
you might also want the "link to" functionality in this example, which sets up the query string

Google maps doesn't link kml file

I have a PHP function that creates a kml file. (I validated the output and it's a valid KML file).
Then, I use Google Maps with these file, but I don't know why, no data appears on the map...
In PHP, I have this:
//some stuff here
return 'iniMap("", "", "http://my.web.com/Class/API/GMaps/Rep/'.$g->make($l, $a, $user).'.kml")';
This function is called via AJAX, so the return string will be evaluated with JS "eval()". My Google Maps functions are:
/*GOOGLE MAPS FUNCTIONS*/
function iniMap(x,y,url){
n=document.createElement('DIV');
n.id='map_canvas';
ge('con').appendChild(n);
var latlng=new google.maps.LatLng(x,y);
var map=new google.maps.Map(ge("map_canvas"),{zoom:6,center:latlng,mapTypeId:google.maps.MapTypeId.ROADMAP});
var div1=document.createElement('DIV');
var homeControl1=new makeControl(div1,'t1');
var div2=document.createElement('DIV');
var homeControl2=new makeControl(div2,'t2');
var div3=document.createElement('DIV');
var homeControl3=new makeControl(div3,'t3');
var div4=document.createElement('DIV');
var homeControl4=new makeControl(div4,'t4');
var ctaLayer=new google.maps.KmlLayer(url);
div1.index = 1;
map.controls[google.maps.ControlPosition.TOP_RIGHT].push(div1);
div2.index = 2;
map.controls[google.maps.ControlPosition.TOP_RIGHT].push(div2);
div3.index = 3;
map.controls[google.maps.ControlPosition.TOP_RIGHT].push(div3);
div4.index = 4;
map.controls[google.maps.ControlPosition.TOP_RIGHT].push(div4);
ctaLayer.setMap(map);
}
function makeControl(d,t){
d.style.padding='5px';
var controlUI=document.createElement('DIV');
controlUI.style.backgroundColor='#FEFEFE';
controlUI.style.borderStyle='solid';
controlUI.style.borderWidth='1px';
controlUI.style.cursor='pointer';
controlUI.style.textAlign='center';
controlUI.style.width='60px';
controlUI.style.height='17px';
d.appendChild(controlUI);
var controlText=document.createElement('DIV');
controlText.style.fontFamily='Arial,sans-serif';
controlText.style.fontSize='12px';
controlText.style.paddingLeft='4px';
controlText.style.paddingRight='4px';
controlText.innerHTML=t;
controlUI.appendChild(controlText);
google.maps.event.addDomListener(controlUI,'click',function(){doAction(t);});
}
function doAction(t){
if(t=='t1'){document.location.href='http://my.web.com?t=sy0'}
else if(t=='t2'){document.location.href='http://my.web.com?t=sm0'}
else if(t=='t3'){document.location.href='http://my.web.com?t=sw0'}
else if(t=='t4'){document.location.href='http://my.web.comt=sd0'}
}
What I'm doing wrong?
Thanks!
I fixed the problem...
It was a cache issue. Maybe some a file with bad data was cached, and therefore my function was always getting that bad file...
Today I changed the way to generate the file name, and the map start working...
Thanks to #geocodezip for all the answers and sorry for the time wasting...

Google maps: Set different zoom levels for two different maps

I have two maps on a page, one is a map of the world, and the other is a closeup of the current place they picked on the map of the world. I would like to set different zoom min/max levels for each map but:
G_NORMAL_MAP.getMinimumResolution = function(){return 11};
Seems to set the same min/max for both maps, I can't set them to different levels.
I think the problem is probably elsewhere in your code - I'm not sure exactly how you're using that function.
Here is a method that will work. You can re-write it to have less duplication.
map1 = new GMap2(document.getElementById("map1"));
map1.addControl(new GLargeMapControl3D());
map1.addControl(new GMenuMapTypeControl());
var mt = map1.getMapTypes();
// Overwrite the getMinimumResolution() and getMaximumResolution() methods
for (var i=0; i<mt.length; i++) {
mt[i].getMinimumResolution = function() {return 7;}
mt[i].getMaximumResolution = function() {return 11;}
}
map1.setCenter(new GLatLng(40,-100), 8);
map2 = new GMap2(document.getElementById("map2"));
map2.addControl(new GLargeMapControl3D());
map2.addControl(new GMenuMapTypeControl());
var mt = map2.getMapTypes();
// Overwrite the getMinimumResolution() and getMaximumResolution() methods
for (var i=0; i<mt.length; i++) {
mt[i].getMinimumResolution = function() {return 2;}
mt[i].getMaximumResolution = function() {return 6;}
}
map2.setCenter(new GLatLng(40,-100), 4);
Do you need 2 different maps? You can use the Map2.showMapBlowup() function to show a subarea which will be a zoomed in section on the current map.
I'm sorry I don't know if you can actually do it with 2 different maps.
http://code.google.com/apis/maps/documentation/reference.html#GMap2.showMapBlowup
You could use a custom map type and copy the G_NORMAL_MAP members of using a library like Prototype.
var G_MY_MAP = Class.create(G_NORMAL_MAP, {
getMinimumResolution: function()
{
return 11;
}
});
Then on your second map:
secondMap.addMapType(G_MY_MAP);
secondMap.setMapType(G_MY_MAP);
No idea if this will work, just a brain storm.....