Enable/Disable placemarks from kml files in the Maps API? - javascript

function initMap() {
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 12,
center: {lat: 37.06, lng: -95.68}
});
var kmlLayer = new google.maps.KmlLayer({
url: 'http://googlemaps.github.io/kml-samples/kml/Placemark/placemark.kml',
suppressInfoWindows: true,
map: map
});
}
A simple example of loading a KML file, which will display the placemarks on the map, taken from: https://developers.google.com/maps/documentation/javascript/examples/layer-kml-features
My question is, how can I toggle the placemarks? I want to have all of them disabled at first, but as I run tests, searches, or whatever, I can have certain ones displayed. How can I do this?

You can't modify the displayed placemarks in a KmlLayer dynamically.
Options:
use a third party KML parser like geoxml3 or geoxml-v3, they display the KML as native Google Maps Javascript API v3 objects, which can be hidden/shown dynamically.
example
import your KML into FusionTables and use a FusionTablesLayer, you can toggle the displayed objects by changing the layer query.
create dynamic KML on your server and display that using KmlLayer

Related

How to add a GeoJSONLayer to a map from json file ArcGIS API for JavaScript?

I created a point layer in ArcMap, then, using the Features to JSON tool, converted these points into a JSON file and ticked GeoJSON, trying to add it to the map:
let layer = new GeoJSONLayer({
url: "data/points.json"
});
let map = new Map({
basemap: "dark-gray"
});
map.add(layer);
let mapview = new MapView({
container: "map",
map: map
});
But nothing appears on the map, I tried to change the format to .geojson, the same way, if I change the url to a link from the documentation, then all the rules, dots appear, what am I doing wrong?
Maybe the problem is in the json file?
https://drive.google.com/open?id=1YxKfhiQMD82W2EI1llsmQaVD3kWgnWP-

Empty Space Around Google Map Marker Icons In Chrome Device Mode

I'm trying to use an svg file as my marker icons. Everything looks great, except for that fact that there is this giant empty space around the marker, rendering them incredibly hard to interact with when grouped up. I've tried using icon.size and icon.scaledSize to address my issue but I still can't remove the empty space. Perhaps it's my svg file?
function initMap() {
var myLatLng = {lat: -25.363, lng: 131.044};
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 4,
center: myLatLng
});
var marker = new google.maps.Marker({
position: myLatLng,
map: map,
icon: {
url: 'assets/icons/art.svg'
}
});
}
this is the svg (converted to png)
The actual svg in question
Here's the issue
EDIT:
It turned out that using Chrome's Device Mode was interfering with the way Maps rendered the icons.
I ended up seeing the expected result after following #bonnie suggestion (cropped any excess empty space from my files), adjusting the icon.scale and icon.scaledSize properties, and exiting Device Mode.
The final icon object was:
icon = {
url: 'assets/icons/art.svg',
size: new google.maps.Size(35, 35),
scaledSize: new google.maps.Size(40, 40)
};
Google maps rendered
icon.size as image
& icon.scaledSize as image
I still have no idea why Chrome's Device Mode can't render marker icons properly.
The viewbox of the SVG is too large. If you open in a graphics program, such as Illustrator, you can adjust the size of the artboard, so that it has no blank space around the edge of the graphic.

Google Maps API not loading KML files

The background Information
I am trying to build a google map using the JS API.
My map can be found at http://matthewjohnwilson.com/Maps/Maps.html
I wanted to add paths to my map so to get started I used the KML example found at https://developers.google.com/maps/documentation/javascript/examples/layer-kml
And I have made a map using MyMaps that can be seen here.
https://www.google.com/maps/d/viewer?mid=1D3USEeIbdVN3zV4B0S8jgODVIS0NHGvY&hl=en&usp=sharing
so that I could try and download the kml file from the MyMaps then upload it to my site to use in my js api map.
Now for the issue.
My paths from my KML file doesn't want to load in my map. When I would go to the KML file directly I was getting a 404 error so I looked into that and it turns out that KML files hosted on godaddy were not supported by default. So I edited my website's Web.config and added
<staticContent>
<mimeMap fileExtension=".kmz" mimeType="application/vnd.google-earth.kmz" />
<mimeMap fileExtension=".kml" mimeType="application/vnd.google-earth.kml+xml" />
</staticContent>
Now when I go to the KML file directly it trys to download the file. I think that is a good thing, at least I hope I am in the right direction. But the paths still dont load in my JS Api map.
Any one have any thing else I can try? I am stuck at the moment. First time using KML files.
YURIKA! I got it to work. So in godaddy I had to go into my MORE/IIS Management and make a KML folder. While running IIS 7. Then in my file manager I now had a KML folder at the webroot of my site. I placed the KML file in there and updated the new KML location in http://matthewjohnwilson.com/Maps/Maps.html and it worked. So now I just need to house all of my KML files in the KML folder on the root of my site.
The simplest way to use output from MyMaps is to link to it directly:
var kmlLayer = new google.maps.KmlLayer({
url: "http://www.google.com/maps/d/kml?forcekml=1&mid=1D3USEeIbdVN3zV4B0S8jgODVIS0NHGvY",
map: map
});
Then the map updates when you change the MyMap and you don't need to configure your server to serve the KML.
function initMap() {
var bounds = new google.maps.LatLngBounds();
var infowindow = new google.maps.InfoWindow();
var mapOptions = {
center: {
lat: 45,
lng: -100
},
zoom: 2,
mapTypeId: 'roadmap'
};
// Display a map on the page
var map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions);
var kmlLayer = new google.maps.KmlLayer({
url: "https://www.google.com/maps/d/kml?forcekml=1&mid=1D3USEeIbdVN3zV4B0S8jgODVIS0NHGvY",
map: map
});
}
google.maps.event.addDomListener(window, 'load', initMap);
html,
body,
#map_canvas {
height: 100%;
width: 100%;
padding: 0px;
margin: 0px;
}
<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCkUOdZ5y7hMm0yrcCQoCvLwzdM6M8s5qk"></script>
<div id='map_canvas'></div>

Save google maps markers locally

building a map (using html and JS) with several markers, different labels and colors, it takes time to use google function
http://maps.google.com/mapfiles/ms/icons/color.png
Is that possible to store and use local png images, or, better to save image code (eg base64) into vars ?
Thanks a lot
Sure it is, in the main loop where you are defining markers, use something like this and that will work:
marker[ i ] = new google.maps.Marker({
map: map,
position: new google.maps.LatLng( lat, lng ),
title: krexmap[ i ].title,
icon: "data:image/png;base64,iVBORw0KGgoAAAANSUh...",
});
Icon, can be a url path but for sure you can also use datauri.
Note: I took that code from my project

Google new KMLLayer not working with data extracted from custom map

I created my google map. Imported markers from google spreadsheet. Shared this map publicly. Export like KMZ. With choosing option "Keep data updated". Then In my JS Application trying to load KML Layer. I am using link that was placed inside downloaded kmz/kml file.
http://www.google.com/maps/d/u/0/kml?forcekml=1&mid=zmJT0iVx3pVs.kmPWwDLpv90w&lid=zmJT0iVx3pVs.kenD66KR2z4o
screenshot http://screencloud.net/v/lRjb
My JS code.
var myOptions = {
center: new google.maps.LatLng(49.988997, 36.225374),
zoom: 11,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById("map"), myOptions);
var kmzLayer = new google.maps.KmlLayer('http://www.google.com/maps/d/u/0/kml?forcekml=1&mid=zmJT0iVx3pVs.kmPWwDLpv90w&lid=zmJT0iVx3pVs.kenD66KR2z4o');
kmzLayer.setMap(map);
I have no seen any diagnostic in console.
So My Question is:
1) How to load this data? I need user of "My Map" able to add new places using this spreadsheet/map.
2) I see in these exported kml a lot options(columns) that exist in spreadsheet, but not chosen for description. It is not secure to use this columns. In the reason i want to see just address and title on the map

Categories