I am deploying a rails site that includes an embedded google map. I have a couple images I wanted to use as custom google map icons, but I cannot get them to display when deployed to heroku (404 error).
I know that for background images to show I use a scss file with image-url, but I can't figure out how to format the JS file for heroku to find the image.
var map = new google.maps.Map(mapCanvas, mapOptions);
var wedding = 'assets/glyph-ring.png';
var weddingPosition = new google.maps.LatLng(44.192309, -82.243499);
var marker = new google.maps.Marker({
position: weddingPosition,
map: map,
icon: wedding
});
Related
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>
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
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
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
As i red from the readme file, openlayers.js has multiple choices for including files and themes.
What i would like is to use the lightest solution of openlayers.js files.
I included the openlayers.light.js in my app, and it creates maps but do not show them, check this:
do i forgot to include some other file?
my structure structure is this:
/vendor
/js
openlayers.light.js
/img
/theme
how to show maps layers?
Also does the openlayers.light.js will work on mobile devices (once fixed this problem :P )? or i'll need to include openlayers.mobile.js too?
This is the code not working with openlayers.light.js but working with openlayers.js (740kb) :
var _element = "#map";
var map = new OpenLayers.Map (_element, {
controls: [
new OpenLayers.Control.Navigation({
dragPanOptions: {
enableKinetic: true
}
}),
new OpenLayers.Control.Zoom()
],
projection: new OpenLayers.Projection("EPSG:900913"),
displayProjection: new OpenLayers.Projection("EPSG:4326")
});
var lonLat = new OpenLayers.LonLat(_lon, _lat).transform (
new OpenLayers.Projection("EPSG:4326"), // transform from WGS 1984
new OpenLayers.Projection("EPSG:900913") // to Spherical Mercator Projection
// map.getProjectionObject() doesn't work for unknown reason
);
var markers = new OpenLayers.Layer.Markers( "Markers" );
map.addLayer(markers);
var size = new OpenLayers.Size(21,25);
var offset = new OpenLayers.Pixel(-(size.w/2), -size.h);
var icon = new OpenLayers.Icon(_img_map_marker, size, offset);
markers.addMarker(new OpenLayers.Marker(lonLat, icon.clone()));
var mapnik = new OpenLayers.Layer.OSM("Test");
map.addLayer(mapnik);
map.setCenter (lonLat,3);
PS: my openlayers map js init method is ok, it works using the huge openlayers.js (740KB), but not working with openlayers.light.js as i showed above
html
<div id="map"></div>
css
img{max-width:none;}
#map{
width:300px;
height:300px;
}
if you want to use mobile properties with openlayers as panning or zooming with hand you have to use openlayers.mobile.js.
you can use openlayers.light.js with mobile devices but not mobile functions.
i think your structure should be :
myProject
/js
openlayers.light.js
/img
/theme
and i have tried openlayers.light.js in http://jsfiddle.net/aragon/ZecJj/ and there is no problem with it.
My code:
var map = new OpenLayers.Map({
div: "map",
minResolution: "auto",
maxResolution: "auto",
});
var osm = new OpenLayers.Layer.OSM();
var toMercator = OpenLayers.Projection.transforms['EPSG:4326']['EPSG:3857'];
var center = toMercator({x:-0.05,y:51.5});
map.addLayers([osm]);
map.setCenter(new OpenLayers.LonLat(center.x,center.y), 13);
and try to read Deploying (Shipping OpenLayers in your Application).
OpenLayers comes with pre-configured examples out of the box: simply
download a release of OpenLayers, and you get a full set of easy to
use examples. However, these examples are designed to be used for
development. When you’re ready to deploy your application, you want a
highly optimized OpenLayers distribution, to limit bandwidth and
loading time.
you can change src file with this link and can see it still works.
<script type="text/javascript" src="http://openlayers.org/dev/OpenLayers.light.debug.js"></script>
to
<script type="text/javascript" src="https://view.softwareborsen.dk/Softwareborsen/Vis%20Stedet/trunk/lib/OpenLayers/2.12/OpenLayers.light.js?content-type=text%2Fplain"></script>
i hope it helps you...