I have to load KML file in browser.And I have load simple KML file it works perfectly but when I upload the KML file with multiple folder for images it is not loading properly.
Code of line used:-
<div id="map_canvas"></div>
<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false>"></script>
<script>
var mylocation = {
'latitude': -45.2427303,
'longitude': 115.0639984
};
var map;
function initialize()
{
var myLatlng = new google.maps.LatLng( mylocation.latitude, mylocation.longitude );
var mapOptions = {
zoom: 5,
center: myLatlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
var map = new google.maps.Map(document.getElementById('map_canvas'), mapOptions);
var kmlPath = '<?php echo urldecode($file_name)?>';
var urlSuffix = (new Date).getTime().toString();
var layer = new google.maps.KmlLayer(kmlPath + '?' + urlSuffix );
layer.setMap(map);
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>
KMLLayer generates map tiles in Google's server side. For the reason, multiple KMLLayer (and fusion tables layer also) do not work as you expected sometimes.
This is not described in the official document, but this comes from my experience.
I recommend you use GeoJSON with DataLayer.
DataLayer generates overlays (marker, polyline , etc...) in client side.
And you can modify the generated overlays easily.
Check it out.
https://developers.google.com/maps/documentation/javascript/datalayer#overview
Related
So Im working on my project and the user is to enter an address after its been created in the database, i want it to show in a google map. I've found out the coordinates for the map through this link :
http://www.codeproject.com/Tips/650139/Finding-Co-ordinates-of-an-Address-in-ASP-NET-Csha
However this link shows the coordinates as a label where as i have a java script method as follows :
<script type="text/javascript">
var myCenter = new google.maps.LatLng(1.303895, 103.831941);
function initialize() {
var mapProp = {
center: myCenter,
zoom: 18,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById("googleMap"), mapProp);
var marker = new google.maps.Marker({
position: myCenter,
});
marker.setMap(map);
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>
The script works fine if i enter the coordinates myself. I've tried parsing the latitude and longitude through a label and using document.getElementById() method however the map does not show.
Any clue how i can solve this ?
I am editing my question after spending 3 days unable to figure this out:
I have had a website of all stairs in Seattle that showed a description and image when a marker was clicked. It has been working well for a couple of years.
(http://seattlestairs.home.comcast.net/~seattlestairs)
Then google changed the format of their kml files and if I substitute the NEW kml for the OLD one, I no longer can see the image. You can see this at:
(http://seattlestairs.home.comcast.net/~seattlestairs/index2.html)
However, if I just embed my map into a webpage and click on the marker, the info window pops up and shows the image and description.
However, I don't like that because the images are too small to see details.
I want my sidebar images back and was hoping somebody could tell me how to change the code to make that happen. (I am just an amateur programmer). The comments suggested using a parser and I am able to get the gist of that but I can not figure out exactly how to do it.
Here is the structure of a placemark in the OLD kml file:
<Placemark>
<name>#176 John St</name>
<description><![CDATA[<div ><img style="width:400px" src="http://lh6.ggpht.com/_izunqcjpHto/TIx-yqN7PdI/AAAAAAAADXw/lt7xzoZ1ivI/s720/%23176%20side.jpg"><br>67 steps</div>]]></description>
<styleUrl>#ordinary</styleUrl>
<Point>
<coordinates>-122.282104,47.618984,0.000000</coordinates>
</Point>
Here it is in the NEW kml file:
<Placemark>
<styleUrl>#icon-ci-1</styleUrl>
<name>#176 John St</name>
<ExtendedData>
<Data name='gx_media_links'>
<value>http://lh6.ggpht.com/_izunqcjpHto/TIx-yqN7PdI/AAAAAAAADXw/lt7xzoZ1ivI/s720/%23176%20side.jpg</value>
</Data>
</ExtendedData>
<description><![CDATA[67 steps]]></description>
<Point>
<coordinates>-122.282104,47.618984,0.0</coordinates>
</Point>
</Placemark>
I previously used a sample java script to do this and just put the "description" into the infowindow. Now with the new kml files the source of the image is in a "extended data" and I don't know the right way to access that image information. I can get the name and description but not the image.
My old script looked like this:
<script type="text/javascript">
function initialize() {
var latlng = new google.maps.LatLng(47.605, -122.333);
var myOptions = {
zoom: 12,
center: latlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById("map_canvas"),
myOptions);
var stairLayer = new google.maps.KmlLayer('http://seattlestairs.home.comcast.net/~seattlestairs/SeattleStairs97.kml',
{preserveViewport: true, suppressInfoWindows:true});
stairLayer.setMap(map);
google.maps.event.addListener(stairLayer, 'click', function(kmlEvent) {
var text = kmlEvent.featureData.description;
text = kmlEvent.featureData.name + text;
showInContentWindow(text);
});
function showInContentWindow(text) {
var sidediv = document.getElementById('content_window');
sidediv.innerHTML = text;
}
}
</script>
So where is said "text = kmlEvent.featureData.description" there needs to be some code that will allow me to get the src of the image, and then to put that into html into the sidediv, but I don't know how.
KmlLayer (API Reference) does not let you access any of the data from the KML except on a click. If you use a 3rd party KML parser (like geoxml3 or geoxml-v3) you can use whatever information you want from the KML
One option is to use a third party parser (like the KMZ branch of geoxml3) which allows you to access the <ExtendedData> in the KML.
var map;
var geoXmlDoc;
function initialize() {
var latlng = new google.maps.LatLng(47.605, -122.333);
var myOptions = {
zoom: 12,
center: latlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById("map_canvas"),
myOptions);
var myParser = new geoXML3.parser({
map:map,
afterParse: useTheData,
suppressInfoWindows: true,
suppressDirections: true
});
google.maps.event.addListenerOnce(map, 'idle', function() {
for (var i = 0; i < geoXmlDoc.placemarks.length; i++) {
var placemark = geoXmlDoc.placemarks[i];
if (placemark.marker) {
createClickableMarker(placemark);
}
}
});
myParser.parse("http://www.geocodezip.com/geoxml3_test/kml/newSeattleStairs.kml");
}
function showInContentWindow(text) {
var sidediv = document.getElementById('content_window');
sidediv.innerHTML = text;
}
function createClickableMarker(placemark) {
google.maps.event.addListener(placemark.marker,'click',function(e){
var htmlString =placemark.name+"<br>"+placemark.description;
var media_imgs = placemark.vars.val.gx_media_links.split(" ");
for (var i=0; i < media_imgs.length; i++) {
htmlString += "<br><img src='"+placemark.vars.val.gx_media_links+"' width='400'/>";
}
showInContentWindow(htmlString)
});
}
function useTheData(doc){
geoXmlDoc = doc[0];
};
google.maps.event.addDomListener(window, 'load', initialize);
proof of concept (there seem to be issues with the click targets for the icons, but it can be made to work). Note that third party parsers render the KML as native Google Maps Javascript API v3 objects, so will not have the performance of the tile based rendering of KmlLayer.
This way works on Chrome and Firefox and Safari, can't tell about IE:
I altered the kml file from Google Maps into an xml file by deleting the "kml" tags and icon info, and I read about xml path from the W3 online teaching, and then modified their example.
So, first I used the Google-maps event feature to get the name of the stairway.
Then I loaded the xml file and used the "evaluate" function to get the source for my images (called value" in the kml file):
<script>
var smoname = " ";
var smodev = " ";
// this will set up the google map in the right location
function initialize() {
var myLatlng = new google.maps.LatLng(47.605, -122.333);
var mapOptions = {
zoom: 13,
center: myLatlng
};
var map = new google.maps.Map(document.getElementById('map-canvas'),
mapOptions);
//Here is the raw kml file as exported from google maps
var kmlLayer = new google.maps.KmlLayer({
url: 'http://seattlestairs.home.comcast.net/~seattlestairs/newSeattleStairs.kml',
suppressInfoWindows: true,
map: map
});
google.maps.event.addListener(kmlLayer, 'click', function(kmlEvent) {
//gets the name of the stairway when the icon is clicked
smoname = kmlEvent.featureData.name;
smodef = kmlEvent.featureData.description;
smoin = smoname +"<br>"+ smodef;
showInContentWindow(smoin);
//looks up the url of the image from the name
function loadXMLDoc(dname)
{
if (window.XMLHttpRequest)
{
xhttp=new XMLHttpRequest();
}
else
{
xhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xhttp.open("GET",dname,false);
try {xhttp.responseType="msxml-document"} catch(err) {} // Helping IE
xhttp.send("");
return xhttp;
}
var x=loadXMLDoc("AllSeattleStairs.xml");
var xml=x.responseXML;
path="/Document/Placemark[name= '" + smoname + "']/ExtendedData/Data/value";
// code for IE
if (window.ActiveXObject || xhttp.responseType=="msxml-document")
{
xml.setProperty("SelectionLanguage","XPath");
nodes=xml.selectNodes(path);
document.getElementById("showme").innerHTML = "<img width = 400px src =' " + nodes.childNodes[0].nodeValue + " ' ></src>";}
// code for Chrome, Firefox, Opera, etc.
else if (document.implementation && document.implementation.createDocument)
{
var nodes=xml.evaluate(path, xml, null, XPathResult.ANY_TYPE, null);
var result=nodes.iterateNext();
document.getElementById("showme").innerHTML = "<img width = 400px src =' " + result.childNodes[0].nodeValue + " ' ></src>";
}
});
function showInContentWindow(smoin) {
var sidediv = document.getElementById('content-window');
sidediv.innerHTML = smoin; }
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>
I am trying to add geo-location to a custom public google map, I can't manage to get it to work. For example here is a custom public google map.
Lets say I wanted to add geo-targeting to that map. I have the following on the site which is directly off the google maps API website:
<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=true"></script>
as well as the following which I just changed '.getElementsById' to '.getElementsByClassName':
<script type="text/javascript">
var map;
function initialize() {
var mapOptions = {
zoom: 3,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementsByClassName('map-canvas'),
mapOptions);
// Try HTML5 geolocation
if(navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(position) {
var pos = new google.maps.LatLng(position.coords.latitude,
position.coords.longitude);
var infowindow = new google.maps.InfoWindow({
map: map,
position: pos,
content: 'Location found using HTML5.'
});
map.setCenter(pos);
}, function() {
handleNoGeolocation(true);
});
} else {
// Browser doesn't support Geolocation
handleNoGeolocation(false);
}
}
function handleNoGeolocation(errorFlag) {
if (errorFlag) {
var content = 'Error: The Geolocation service failed.';
} else {
var content = 'Error: Your browser doesn\'t support geolocation.';
}
var options = {
map: map,
position: new google.maps.LatLng(60, 105),
content: content
};
var infowindow = new google.maps.InfoWindow(options);
map.setCenter(options.position);
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>
Then I call for the map which is in a lightbox:
<h2 class="dis">Where can I<br />get Rumble?</h2>
It displays the map fine, and asks to geo-target but I assume the reason its not working on this map is because its not included in the API.
I was hoping someone had a solution for this.
This is not an API based map. You can display the KML from that map on an API based map
Then use your geolocation code to center the map (depending on the ordering, you might need to use the preserveViewport:true option on the KmlLayer). Relevant code below, see the documenation for more examples and information.
This is in your existing page (leave your version)
var myOptions = {
zoom: 5,
center: new google.maps.LatLng(0,0),
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById("map_canvas"),myOptions);
Add this to display the data from your "custom map":
var kmlLayer = new google.maps.KmlLayer("https://maps.google.ca/maps/ms?ie=UTF8&msa=0&output=kml&msid=202458571791405992786.0004b9061a3fcd9461d42");
kmlLayer.setMap(map);
I'm trying to emulate this functionality
https://maps.google.com/maps?q=http://en.wikipedia.org/w/index.php%3Ftitle%3DTemplate:Attached_KML/N_postcode_area%26action%3Draw
I want to be able to add and remove a postcode region from an external link / checkbox. I have the KML file loading correctly using this code but I have no idea how to attach the external event listener / handler.
function initialize() {
var london = new google.maps.LatLng(51.522416,-0.11673);
var mapOptions = {
zoom: 11,
center: london,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
var map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
var ctaLayer = new google.maps.KmlLayer({
url: 'path/to/kml',
suppressInfoWindows: true
});
ctaLayer.setMap(map);
}
google.maps.event.addDomListener(window, 'load', initialize);
I've tried looking in the ctaLayer object to get the individual nodes from the KML file but they dont seem to be there.
I am making a map where I have the KML load and the markers load on the same canvas in which I want the boundary to be however big the markers even though the kml is on the canvas. I'm using this in conjunction with php so there's a bit of php in the code but nothing to special besides entering information for variables.
<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=false"></script>
<script type="text/javascript">
jQuery(document).ready(function($) {
if ($('#map_canvas').length != 0) {
var myLatlng = new google.maps.LatLng(45.266438, -93.233225);
var myOptions = {
zoom: 4,
center: myLatlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
var bounds = new google.maps.LatLngBounds();
var kmlLayer = new google.maps.KmlLayer('<?php bloginfo('template_url'); ?>/includes/delivery-maps/mansettisdel.kml', { map: map });
var jsongeo = <?php echo(json_encode($geo_json)); ?>;
$.each(jsongeo, function(index, value) {
var latlng = new google.maps.LatLng(value.lat,value.lng);
var marker = new google.maps.Marker({
position: latlng,
map: map,
title:"Hello World!"
});
bounds.extend(latlng);
map.fitBounds(bounds);
});
}
});
</script>
here is the var_dump of the json that I am looping through the lat and longitude.
string(617) "[{"type":"delivery","lat":"45.341969","lng":"-93.277657","date":"2011-07-11"},{"type":"delivery","lat":"45.005360","lng":"-93.323738","date":"2011-07-12"},{"type":"delivery","lat":"45.319408","lng":"-93.202446","date":"2011-07-12"},{"type":"delivery","lat":"45.131786","lng":"-93.216576","date":"2011-07-13"},{"type":"delivery","lat":"44.804131","lng":"-93.166885","date":"2011-07-13"},{"type":"delivery","lat":"45.119965","lng":"-93.287727","date":"2011-07-13"},{"type":"delivery","lat":"42.358433","lng":"-71.059776","date":"2011-07-13"},{"type":"delivery","lat":"34.195915","lng":"-84.507317","date":"2011-07-13"}]"
Now I'm not having issues with the markers going through but just the bounds. I don't know if there is something other I would have to do with the lat,long but I read up on this a ton and can't find a thing.
Here is what is showing up.
What shows up
Here is what I would like to show up.
What I want to show up
Even though the KML is on the map I would like the markers to be the bounds. Is there something that google maps does that sets it to the KML or do I have the bounds done incorrectly? I notice it goes to the bounds view and then RIGHT to the KML view.
Try adding the option preserveViewport: true to the KML options so it reads:
var kmlLayer = new google.maps.KmlLayer('<?php bloginfo('template_url'); ?>/includes/delivery-maps/mansettisdel.kml', {
map: map,
preserveViewport: true
});
That should stop it zooming it into the KML bounds.
http://code.google.com/apis/maps/documentation/javascript/reference.html#KmlLayerOptions