Why doesn't the google search API show images? - javascript

When I use the google search api example from here
It doesn't show me any results for Images.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8"/>
<title>Hello World - Google Web Search API Sample</title>
<script src="https://www.google.com/jsapi"
type="text/javascript"></script>
<script language="Javascript" type="text/javascript">
//<!
google.load('search', '1');
function OnLoad() {
// Create a search control
var searchControl = new google.search.SearchControl();
// Add in a full set of searchers
var localSearch = new google.search.LocalSearch();
searchControl.addSearcher(localSearch);
searchControl.addSearcher(new google.search.WebSearch());
searchControl.addSearcher(new google.search.VideoSearch());
searchControl.addSearcher(new google.search.BlogSearch());
searchControl.addSearcher(new google.search.NewsSearch());
searchControl.addSearcher(new google.search.ImageSearch());
searchControl.addSearcher(new google.search.BookSearch());
searchControl.addSearcher(new google.search.PatentSearch());
// Set the Local Search center point
localSearch.setCenterPoint("New York, NY");
// tell the searcher to draw itself and tell it where to attach
searchControl.draw(document.getElementById("searchcontrol"));
// execute an inital search
searchControl.execute("VW GTI");
}
google.setOnLoadCallback(OnLoad);
//]]>
</script>
</head>
<body>
<div id="searchcontrol">Loading</div>
</body>
</html>
The option is there through this line
searchControl.addSearcher(new google.search.ImageSearch());
However the results say 0.
Shouldn't there be at least a couple of images? I think a couple of weeks ago when I tried it, it still worked?
Any idea?

Related

Load Javascript on page inside Div using jQuery load()

I have this page that uses Google Maps Javascript API (extPage.php):
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no">
<meta charset="utf-8">
<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false"></script>
<script>
function initialize() {
var cord = new google.maps.LatLng(28.545078,-81.377196);
var mapOptions = {
zoom: 15,
center: cord,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById('map-canvas'),
mapOptions);
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>
</head>
<body>
<div id="map-canvas"style="width:600px; height:500px"></div>
</body>
</html>
That page loads fine as is but I am trying to load into a div on another page. I am doing it like this (test.php):
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$("#LoadBut").click(function() {
$("#extDiv").load('extPage.php');
});
});
</script>
</head>
<body>
<input type="button" id="LoadBut" value="Load">
<div id="extDiv"></div>
</body>
</html>
When I run it just like this I get an error ReferenceError: google is not defined. I've tried moving the <script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false"></script> to the test.php page. The error message goes away but the map does not load.
I've never been to sure about the best way to handle Javascript on a page inside of a div like this.
Any help with this would be great.
Thanks!
From what I understand of your requirements, you can use the
Google Static Maps API, whcih does not require any JavaScript.
You just place an img tag with a reference to a static map url into your page with the appropriate parameters. For your example:
<div id="extDiv">
<img src="https://maps.googleapis.com/maps/api/staticmap?center=28.545078,-81.377196&zoom=15&size=600x500&maptype=roadmap&sensor=false" />
</div>
See the note at the top of the documentation to see if the inclusion of API keys are applicable to your project.

log4javascript - no trace messages appear

I'm using log4javascript version 1.4.3.
In my app it works fine for all log levels except trace. To simplify things and make sure that the problem is not within my app I made an example using the authors sample code as an example and added a setThreshold(log4javascript.TRACE), then added a line to generate a trace entry (I also deleted the lines from his example that are relevant only to an ajaxAppender leaving just the popUpAppender).
When I run the code the error and debug messages appear in a log4javascript popUp window but no trace message.
Evidently I don't correctly understand the use of the trace level or the configuration for trace messages. If someone could point out the error of my ways then I can fix my app logging.
Sample code that fails below.
<?xml version="1.0"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<title>log4javascript example from manual</title>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
<meta name="author" content="Tim Down - tim#log4javascript.org" />
<meta name="description" content="log4javascript, a logging framework for JavaScript based on log4j" />
<meta name="robots" content="all" />
<script type="text/javascript" src="/tracker/libraries/log4javascript.js"></script>
<script type="text/javascript">
//<![CDATA[
var log = log4javascript.getLogger();
var popUpAppender = new log4javascript.PopUpAppender();
var popUpLayout = new log4javascript.PatternLayout("%d{HH:mm:ss} %-5p - %m%n");
popUpAppender.setLayout(popUpLayout);
// new line below
popUpAppender.setThreshold(log4javascript.Level.TRACE);
log.addAppender(popUpAppender);
log.debug("Debugging message (appears in pop-up)");
log.error("Error message (appears in pop-up and in server log)");
// new line below
log.trace("Trace message");
//]]>
</script>
</head>
<body>
<h1>log4javascript example from manual</h1>
<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
var pageTracker = _gat._getTracker("UA-448786-3");
pageTracker._initData();
pageTracker._trackPageview();
</script>
</body>
</html>
The problem is that the logger has a threshold level as well, which is set to DEBUG by default. Add the following line before calling log.trace():
log.setLevel(log4javascript.Level.TRACE);

How do I make this JavaScript code portable?

The following code works only in Chrome. I need it to run on all major browsers. Firefox and IE load the image once. Opera doesn't load it at all.
The screen.jpg file is sent with Cache-Control: no-cache, no-store. I cannot use a library like jQuery because they're too big.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
<script type="text/javascript">
function refresh()
{
document.getElementById("screen").src = "screen.jpg";
setTimeout("refresh()", 500);
}
</script>
</head>
<body style="margin:0" onload="refresh()">
<img id="screen" style="width:100%"/>
</body>
</html>
you can append random number to the end of source so that the image is re-requested every time:
document.getElementById("screen").src = "screen.jpg?" + Math.random()*999999;

Why is Bing Maps 7 AJAX control not lazy-loading?

I'm trying to use Bing Maps 7.0 API with Lazy Loading, as this seem to be supported with the onScriptLoad parameter. I used this minimal test case:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Bing Maps Test</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
<div id='mapDiv' style="position:absolute; width:600px; height:400px;"></div>
<script type="text/javascript">
function initMap() {
console.log('Callback called');
var map = new Microsoft.Maps.Map(document.getElementById("mapDiv"), {credentials:"<API KEY>"});
}
function loadMapControl()
{
var script = document.createElement("script");
script.setAttribute("src", "http://dev.virtualearth.net/mapcontrol/mapcontrol.ashx?v=7.0&onScriptLoad=initMap");
script.setAttribute("type", "text/javascript");
document.documentElement.firstChild.appendChild(script);
console.log('Tag appened');
}
setTimeout(loadMapControl, 1000);
</script>
</body>
</html>
The callback is called as expected, but Firebug throws me an Microsoft.Maps.Map is not a constructor error. This even if I delay the map creation by 10 seconds after the callback is called via setTimeout.
The simple example in Bing docs, however, is working perfectly:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Bing Maps Test</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<script charset="UTF-8" type="text/javascript" src="http://ecn.dev.virtualearth.net/mapcontrol/mapcontrol.ashx?v=7.0"></script>
</head>
<body>
<div id='mapDiv' style="position:absolute; width:600px; height:400px;"></div>
<script type="text/javascript">
var map = new Microsoft.Maps.Map(document.getElementById("mapDiv"), {credentials:",API KEY>"});
</script>
</body>
</html>
Has anybody succeeded in getting the API 7.0 to work with lazy loading?
Thanks.
FYI, this issue has finally been fixed in the newest release of the Maps API.
Everything is working as expected with this new version.

Adding points to google map API

I am putting a google map on my web site and I wanted to add points to the map but I am not sure how to do it. I have tried a few different things but they did not work. Here is what I have so far:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:v="urn:schemas-microsoft-com:vml">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8"/>
<title>Google Maps JavaScript API Example: Simple Map</title>
<script src="http://maps.google.com/maps?file=api&v=2&sensor=false&key=myKeyHere"
type="text/javascript"></script>
<script type="text/javascript">
function initialize() {
if (GBrowserIsCompatible()) {
var map = new GMap2(document.getElementById("map_canvas"));
map.setCenter(new GLatLng(39.8163, -98.55762), 4);
map.setUIToDefault();
}
}
</script>
</head>
<body onload="initialize()" onunload="GUnload()">
<div id="map_canvas" style="width: 750px; height: 500px"></div>
</body>
</html>
This page should get you going
The Google Maps API shows information on how to do this. Basically do:
var point = new GLatLng(latitude,longitude);
map.addOverlay(new GMarker(point));
I used this plugin (it uses Google Map V3 as V2 is deprecated)
http://blog.bobcravens.com/2010/06/06/AGoogleMapsVersion3JQueryPlugin.aspx
it also requires jquery(which makes things easier). It has methods such as
map.addMarkerByLatLng
or
map.addMarkerByAddress
Hope that helps!

Categories

Resources