Displaying a infowindow based on the geometry in the fusion table - javascript

Problem : have to place a marker and infowindow without knowing the latitude and longitude
Explanation:
I have a fusion table with some data and geometry(that defines the region).
Below is the snippet that let me hightlight the neccessary region with the query.
But now i have to set a marker on that neccessay region.but i don't have the latitude and longitude of that region
but i do have is the geometry of the region in the fusion table.
Please suggest me a way.
Snippet:
layer = new google.maps.FusionTablesLayer({
query: {
select: 'geometry',
from: '1j2RT5lmo9*********v-WRXMKSM-2jNsV1pcEc',
where: "CODE = 'S2801'"
},suppressInfoWindows:true,styles: [{
polygonOptions: {
fillColor: "#FC0",
fillOpacity: '1.0',
strokeWeight:"int"
}
}]});

Related

style multiple GeoJson files with the Google Maps Javascript API v3 data layer

I have a site that uses google api v3 for showing polygons from json files.
the site has multiple json polygons, I need to style each polygon with a different color and create a handle to the shape.
The only examples that I can find refer to pure polygons and not json files, there is one example that changes the json file (i cant do this as the json files are static.
sample code:
var map;
function initMap() {
map = new google.maps.Map(document.getElementById('map'), {
zoom: 4,
center: { lat: 45, lng: -90 }
});
//1st Json file
map.data.loadGeoJson(
'https://storage.googleapis.com/mapsdevsite/json/google.json');
//2nd json file (same as #1 for illustration purpose)
map.data.loadGeoJson(
'https://storage.googleapis.com/mapsdevsite/json/google.json');
// I want to style each Json file independently
map.data.setStyle({
fillColor: 'green',
strokeWeight: 1
});
// map1.setMap(map);
}
I managed to get the layer added to the map using,
data_layer.loadGeoJson('https://storage.googleapis.com/mapsdevsite/json/google.json');
// Construct the polygon.
var nLayer = new google.maps.JSON({
paths: data_layer,
strokeColor: 'green',
strokeOpacity: 0.5,
strokeWeight: 1,
fillColor: 'green',
fillOpacity: 0.8
});
nLayer.setMap(map);
I cannot get the style to apply to the map. any ideas ?
I have created a demo on github where I load polygons (boundaries) using Data Layer and I also show how to keep reference to respective polygons and update their specific styles. Check out this SO answer for a snippet (I don't want to copy it here, because it's redundant).
Notice mainly: new_boundary.feature = data_layer.getFeatureById(boundary_id); where I store reference to specific feature, which styles I can update anytime using e.g.:
data_layer.overrideStyle(new_boundary.feature, {
fillColor: '#0000FF',
fillOpacity: 0.9
});
And it would just update that one polygon, not all of them. So if your polygons in geoJSON files have some unique ids, or you can assign ids to all of them, you can then reference and change their styles based on that.
Another option, not shown in the example is, to have multiple data layers. It's possible to have multiple data layers in your application, e.g. create them like this:
var data_layer = new google.maps.Data({map: map});
var data_layer_2 = new google.maps.Data({map: map});
and then load data to them and change styles:
data_layer.loadGeoJson(
'https://storage.googleapis.com/mapsdevsite/json/google.json');
data_layer_2.loadGeoJson(
'https://storage.googleapis.com/mapsdevsite/json/google.json');
data_layer.setStyle({
fillColor: 'green',
strokeWeight: 1
});
data_layer_2.setStyle({
fillColor: 'blue',
strokeWeight: 2
});
I think the best way to do this is to add a property to your feature, inside the json file you load, like so:
{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"properties": {
"county": "hernando"
},
"geometry": {
"type": "MultiPolygon",
"coordinates": [
[
[
[
-82.7784371,
28.694206
],
[
-82.778214,
28.6939659
],
]
]
]
}
}
]
}
Note the important part:
"properties": {
"county": "hernando"
},
Each one of your json files can have as many properties as you want
Then in your javascript file, you can do something like this:
var map = new google.maps.Map($el[0], args);
map.data.loadGeoJson(
'/wp-content/themes/hello-theme-child-master/county-json/pinellas.json'
);
map.data.loadGeoJson(
'/wp-content/themes/hello-theme-child-master/county-json/pasco.json'
);
map.data.loadGeoJson(
'/wp-content/themes/hello-theme-child-master/county-json/hillsborough.json'
);
map.data.setStyle( function ( feature ) {
var county = feature.getProperty('county');
var color = '';
if ( county === 'pasco ) {
color = 'orange'
}
else {
color = 'green'
}
return {
fillColor: color,
strokeWeight: 1
};
});
The important part to note there is that you must pass a function to setStyle, so that it automatically iterates through every feature
I think you'll want to add the polygons and their style individually. From the example ( https://developers.google.com/maps/documentation/javascript/examples/polygon-arrays )
// Define the LatLng coordinates for the polygon.
var triangleCoords = [
{lat: 25.774, lng: -80.190},
{lat: 18.466, lng: -66.118},
{lat: 32.321, lng: -64.757}
];
// Construct the polygon.
var bermudaTriangle = new google.maps.Polygon({
paths: triangleCoords,
strokeColor: '#FF0000',
strokeOpacity: 0.8,
strokeWeight: 3,
fillColor: '#FF0000',
fillOpacity: 0.35
});
bermudaTriangle.setMap(map);

Fusion Tables Layer without markers & infowindows

I'm using Fusion Tables Layer to color in all the countries of the world. Unfortunately there are also markers. And when I click on a country a info window is shown.
I only want the polygons to color the countries, not the markers & infowindows. How can I prevent those from showing.
I have used the layeroption: 'suppressInfoWindow: true', but this does not seem to work.
I have created a codepen example
My fusion table can be found here
var layer = new google.maps.FusionTablesLayer({
map: map,
query: {
select: '\'geometry\'',
from: '1czcJKrnxMfRHNlHcMd1C0OjhprGb2pxNS8YjNUvr'
},
styles: [{
polygonOptions: {
fillColor: '#FF0000',
fillOpacity: 1,
strokeOpacity: '0',
strokeWeight: '0'
}
}],
suppressInfoWindow: true
});
You have a typo. suppressInfoWindow: true should be suppressInfoWindows: true

Drawing border to specific country in google map incorrect

Here is my fiddle , I want to draw border for county India,
For displaying border to specific country i am modifying layer object to ,
layer = new google.maps.FusionTablesLayer({
query: {select: "kml_4326",
from: tableid,
where: "sovereignt = 'India'"},
styles: [{
polygonOptions: {
strokeWeight: "10",
strokeColor: "#FF0000",
strokeOpacity: "0.11",
fillOpacity: "0.0",
fillColor: "#000000"
}
}]});
When i give "sovereignt = 'India'" it is not applying border to India . But if i used any country name other than India it is working correct. ( I have tested for china, pakistan,Australia,Iraq,Iran)
Why it is not applying border to India ? Whats wrong i am doing here ?
There is something wrong with the data for the countries you are having issues with in the table you are using (419167)
This table works fine: 420419 (also from the Natural Earth Data set)
Example (working fiddle)
var layer; // Fusion Tables layer
var tableid = 420419;
layer = new google.maps.FusionTablesLayer({
query: {select: "kml_4326",
from: tableid,
where: "name_0 = 'India'"},
styles: [{
polygonOptions: {
strokeWeight: "2",
strokeColor: "#FF0000",
strokeOpacity: "0.4",
fillOpacity: "0.0",
fillColor: "#000000"
}
}]});
layer.setMap(map);
Analyzing the KML in the kml_4236 column for India, it looks "OK", one of the Polygons is strange (looks like it only contains two unique coordinates), hard to say what is wrong.

Google map api v3 - only show capital city name - London - not boroughs

Does anyone know how to display only the capital city on a customised map e.g London ?
If you are using a KML Layer (https://developers.google.com/maps/documentation/javascript/layers#KMLLayers) and do not want Infowindows you need to use the
suppressInfoWindows
option.
It looks like this:
var ctaLayer = new google.maps.KmlLayer('http://www.insideoutdigital.com/map/Clients3.kml',
{ suppressInfoWindows: true });
ctaLayer.setMap(map);
There is no generic way. You would need to set the label to off for all localities using a styled map with:
style = [
{
"featureType": "administrative.locality",
"elementType": "labels",
"stylers": [
{ "visibility": "off" }
]
}
]
var mapOptions = {
zoom: 12,
...
styles: styles
};
var map = new google.maps.Map(document.getElementById('map_canvas'), mapOptions);
}
and then geocode and display a label for capital cities yourself by geocoding the location or providing it yourself.
You can use the MapLabel library to draw a nice looking label:
http://google-maps-utility-library-v3.googlecode.com/svn/trunk/maplabel/docs/reference.html
Check out this demo on how to use the maplabel library:
http://devfestmtm.appspot.com/demos/directions/directions2.html
HTH

How to know the Geometry coordinates for google Maps

I found a sample script on Google Code about : Google Maps Javascript API V3 Overlays - http://code.google.com/apis/maps/documentation/javascript/overlays.html#OverlaysOverview
And I want to apply this code to other countries (France, spain...) but I don't know where/how to find the Geometry code like in this script (see commented line)
Here is the code:
var australia = new google.maps.LatLng(-25, 133);
map = new google.maps.Map(document.getElementById('map_canvas'), {
center: australia,
zoom: 4,
mapTypeId: google.maps.MapTypeId.ROADMAP
});
layer = new google.maps.FusionTablesLayer({
query: {
select: 'geometry',
from: '815230' // This one
},
styles: [{
polygonOptions: {
fillColor: "#00FF00",
fillOpacity: 0.3
}
}, {
where: "birds > 300",
polygonOptions: {
fillColor: "#0000FF"
}
}, {
where: "population > 5",
polygonOptions: {
fillOpacity: 1.0
}
}]
});
layer.setMap(map);
P.S. I tried to change the google.maps.LatLng(-25, 133) to France Lat&Long but this is used only to center the map on that position.
Thank you for your help
The example you've posted makes use of a FusionTables layer. FusionTables are basically something like a small database or spreadsheet which containt the actual data. The ID that you've commented in the code (815230) is not a coordinate, but the ID of the FusionTable. In fact, you can see the data behind this ID in this link.
You can read more on how to use FusionTables in your maps application in the link that you provided yourself, specifically here. I would recommend reading the article about how to work with FusionTables in general, if you decide to fetch your data from them. You can find a longer article here.

Categories

Resources