Add custom div as control to Google Maps API V2 - google-maps

I can seem to do this for V3 but cannot find any examples for V2
I want to embed the following DIV contents below, ideally keep within a function so that is is easily changed..
function embedLogo() {
return "<a href='http://website.net/?ref=19299'><img src='./images/seestuff.png' /></a>";
}
I would like this in the bottom right and an example of it on v3 is at http://www.planefinder.net

This sorted it for me
http://googlemapsapi.martinpearman.co.uk/articles.php?cat_id=2

Related

How can i load an API call for this weather API to display inside a div in my html page

I have this weather API plugin and my api key and i would like to show the data it displays inside a div. RIght now if i enter this link into my browser it shows me the correct weather info. My question is how do i get this data to display in a div in my web page.
API call (link): http://api.openweathermap.org/data/2.5/weather?q=Montreal&mode=html&appid=4a5344b7f84ba5c5c620525315413734
If you enter this link above in any browser it will work.
I haven't tried anything because its just a link, so obviously adding it anywhere on my page doesn't make sense. I think this has something to do with a GET request perhaps...?
Here is a basic example from your post.
$(function() {
$(".weatherbug > div:eq(1)").load("https://api.openweathermap.org/data/2.5/weather?q=Montreal&mode=html&appid=4a5344b7f84ba5c5c620525315413734");
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="weatherbug">
<div>My Weather</div>
<div></div>
</div>

How to correctly use google maps static tiles with leaflet

I am trying to use tiles that I have generated with the google maps static API with leaflet (I need to generate some tiles in advance because they will be used offline with no internet connection).
For example, to create a 640x640 tile centred on the Isle of Man at lat/lng (54.217, -4.5373) with a zoom level 8, I am using the following request URL: https://maps.googleapis.com/maps/api/staticmap?center=54.217,-4.5373&zoom=8&size=640x640&maptype=roadmap
The resulting tile is shown below:
Next I want to use this tile with leaflet and add a marker at the center of the tile with the following code:
<html>
<header>
<link rel="stylesheet" href="https://unpkg.com/leaflet#1.0.2/dist/leaflet.css" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script src="http://localhost/leaflet.js"></script>
<style>
#map {height: 640px; width:640px;}
</style>
</header>
<body>
<div id="map"></div>
<script type="text/javascript">
$( document ).ready(function() {
// Handler for .ready() called.
console.log("document ready!");
var map = L.map('map').setView([54.217, -4.5373], 8);
L.tileLayer('http://localhost/staticmap.png', {
attribution: 'Copyright Google Maps',
tileSize: 640,
minZoom:8,
maxZoom:8,
continuousWorld: false,
nonoWrap: false,
}).addTo(map);
var marker = L.marker([54.217, -4.5373]).addTo(map);
});
</script>
</body>
</html>
It seems however that the resulting map is completely "off-centred" as illustrated below (the marker should also be on the isle of man). Not sure what I am doing wrong here:
Any help of this would be greatly appreciated! Thanks!
First, take into account that images from the "static maps API" are not tiles: you can request arbitrary sizes, and there is no trivial scheme to stitch such images back together. Tiles work nice because when one tile ends, another tile begins. Tiles need a grid, much like the one explained in the TMS specification.
Second, as they are not tiles, using a L.TileLayer is not the best way. For a north-oriented image fitting the map's projection scheme, the way to go is using a L.ImageOverlay.
To use an image overlay, you'll need to calculate the bounding box of the image. In your case, we can do that manually as so:
console.log('lat-lng of center:', map.containerPointToLatLng( map.getSize().divideBy(2) ));
console.log('lat-lng of center -320px:', map.containerPointToLatLng( map.getSize().divideBy(2).subtract([320,320]) ));
console.log('lat-lng of center +320px:', map.containerPointToLatLng( map.getSize().divideBy(2).add([320,320]) ));
Once you have the URL for the image overlay and the bounds, add it to the map and it should look like this.
Last, be aware of the terms and conditions for stuff coming from the google maps API, in particular:
10.5.d No caching or storage. You will not pre-fetch, cache, index, or store any Content to be used outside the Service, [...]
Also be aware that other web map platforms don't have those restrictions.

How to add marker to google logo destination?

I am creating a map object with a long/lat position as center. When clicking the google logo in the bottom left corner you are routed to google maps with the same position as center. But in the embedded map I have also added a marker at the center position.
Is it possible to have that marker set when you route to google maps as well?
I couldn't find anything in the documentation..
The logo's link looks like this:
https://maps.google.com/maps?ll=40.758893,-73.985132&z=10&t=m&hl=en-US&gl=US&mapclient=apiv3
In your case all you have to do is change the parameters of the link.
Using the ?q=40.758893,-73.985132 parameter instead of ?ll=40.758893,-73.985132 is probably enough for you. The q=latitude,longitude puts a marker to the position. Although it is possible to use both parameters. The q is the marker's position and the ll is the center of the map. (use the & separator between them).
I think it is against the Google Maps API ToS to modify the Google branding or the link. But anyway it is possible with javascript.
You can see the original answer here: How to change Google logo's href
After the map is loaded you can manipulate the link:
...
map.addListener('tilesloaded', function(){
modifyLogoLink();
});
...
And here is how to change the link:
function modifyLogoLink(){
var anchors = document.getElementsByTagName('a'),
l = anchors.length,
i,
a;
for (i = 0; i < l; i++) {
a = anchors[i];
if (a.href.indexOf('maps.google.com/maps?') !== -1) {
// here you can manipulate the anchor
a.href = a.href.replace('ll=', 'q=')
}
}
}
Please also note that the Google's link is changed every time when the map is dragged or zoomed. So you must handle this as well.
Hope this helps.

Google static map image with link to Google iframe

I am trying to display a google static map, which when clicked, will open up a larger iframe, where the user can pan, zoom, etc.
JSFiddle here
Code below:
<div>
<a class="various fancybox.iframe" title="Whitehouse - USA" href="https://maps.google.com/maps?f=d&source=s_d&saddr=&daddr=1600+Pennsylvania+Ave+NW,+White+House,+Washington,+DC+20500&hl=en&geocode=Ca3jx5Eq6BcjFQ6IUQIdG4Ro-ynPaZnjvLe3iTGGOSyaFzTP2g&sll=38.897678,-77.036517&sspn=0.009644,0.01443&g=1600+Pennsylvania+Avenue+Northwest,+Washington,+DC&mra=ls&ie=UTF8&t=m&ll=38.89768,-77.036519&spn=0.008016,0.013733&z=16&output=embed">
<img src="http://maps.googleapis.com/maps/api/staticmap?center=1600+Pennsylvania+Ave+NW,+White+House,+Washington,+DC+20500&markers=1600+Pennsylvania+Ave+NW,+White+House,+Washington,+DC+20500&size=300x300&sensor=false">
</a>
</div>
I have tried to look for the non-javascript documentation relating to the iframe,but haven't come across anything. I would like to add the following to the iframe:
Center on the marker - The JSFiddle appears centered, but the exact same code run on the production site renders an iframe with the marker appearing in the top left.
Remove the marker label "B"
Input my own coordinates from my database - for example... do the same for New York City, Chicago, etc.. However, I have tried changing the daddr (destination address), but am unsure what the other variable stand for (i.e. sll, sspn, g, mra, ll, etc.)
Get directions - insert starting point, and get directions to pre-determined destination
At first a explanation of the parameters you need:
f
has to be d for directions
saddr
the start-address, may be a string(would be geolocated) or a latLng
daddr
the destination-address, may be a string(would be geolocated) or a latLng
ll
where to center the map(latlng) .when ommited, the map will be centered based on the markers
z
the zoom of the map. When ommitted the map will be zoomed based on the direction
output
has to be embed for iframe
A detailed list and explanation of the parameters you'll find at http://www.seomoz.org/ugc/everything-you-never-wanted-to-know-about-google-maps-parameters
However: you should note that none of the parameters is a part of any official API, it may change every day
The issues:
Center on the marker:
the marker could not be centered, because the iframe isn't visible when the map starts loading, unable to determine the size of the iframe . You could use a workaround:
First load a dummy-page into the iframe, when the fancybox is open, load the map.
This can be done by adding this to the fancybox-options:
beforeLoad: function(){
//store the original href for later use
this.oldhref=this.href;
//replace the href with some dummy-page
this.href='wait.htm';
return true;
},
afterLoad:function(){
//load the originally requested page to the iframe
$('.fancybox-iframe').attr('src',this.oldhref);
}
Remove the marker label "B"
there is no option to remove the B, all you can to is replace it with an A .
Therefore you must set the marker as the marker for the start-destination (saddr)
Input my own coordinates from my database
apply the coordinates to saddr or daddr(depending on what it should be, start or destination)
Get directions - insert starting point, and get directions to pre-determined destination
see 3.
Finally: you should consider to create a own map using the Maps-Javascript-API to get a map that you can handle yourself.

how to arrange the divs in the page like google map or other map site

We are now trying to build a map library like google/bing/yahoo,we will use it offline.
However I found that I have no idea about how to arange the divs in the page,since there are some many different types of divs.
1) the map tiles (small image 256X256)
2)the overlayer(marker/informationwindow/polygon...)
3)the control.
I have to try to read the html source codes of google and bing and etc. But I found it is difficult to understand them.
For exmaple,this frangment is copyed from another online map site of China.
As you can see,it is just a exmaple for how to adding a marker to the map.
But take the code,there are so many nested divs,most of them have the property of "width:0;height:0",I do not know why?
Since in my opinion,the marker is just an icon,just put it in the page.
Why use so many nested divs and even the "map" tag?
But I think they must have the advantages which I can not find.
Any one can give some suggestions?
Typically you insert a div in HTML when you want to create a block element but there is no more semantically-loaded element available with the correct meaning.
I think the answer to your question is to use just as many div elements as you need for your purposes. Do not add more just because you can. Sometimes you don't need any div elements at all - you can use other more meaningful elements such as img, ul, p, etc. You can sometimes avoid inserting a wrapping div by using CSS to change an inline element such as a into a block element.
If you need more later then add them later. Don't worry about what Google/Bing/Yahoo do. Their requirements are probably different to yours.
Have you looked at the Google Maps sample code and demo gallery?
http://code.google.com/apis/maps/documentation/javascript/demogallery.html
http://code.google.com/apis/maps/documentation/javascript/examples/index.html
I'm not sure how you would use this "offline" considering the sample you provided makes a call to the internet to get the map. Also all of these types of maps rely heavily on javascript and ajax calls to constantly update the map. Do you mean these pages would be secured and not public?
How about you just use maybe a 5x5 grid of divs, move them as they are dragged out of view, and then texture them dynamically with AJAX calls.
If I am understanding you correctly, all of the layers can be thrown on top of each other with z-index.
<div id="control" style="z-index:-1;"></div>
<div id="overlay" style="z-index:-2;"></div>
<div id="map" style="z-index:-3;"></div>
Then you can use each of these divs as containers for different parts of your map.
As you drag 1 div off to, say, the right, then it will automatically bump itself to the left side of your grid and retexture itself (background-image) through an ajax call.
That's what I would do, at least.
Use the Google Maps API you can see an example of custom tiles here: http://code.google.com/apis/maps/documentation/javascript/examples/maptype-base.html
You would need to copy all the files to your computer to be exceccible offline. Your javascript would look something like this:
function CoordMapType() {
}
CoordMapType.prototype.tileSize = new google.maps.Size(256,256);
CoordMapType.prototype.maxZoom = 19;
CoordMapType.prototype.getTile = function(coord, zoom, ownerDocument) {
var div = ownerDocument.createElement('DIV');
div.style.backgroundImage=coord+'.js';
return div;
};
CoordMapType.prototype.name = "Tile #s";
CoordMapType.prototype.alt = "Tile Coordinate Map Type";
var map;
var chicago = new google.maps.LatLng(41.850033,-87.6500523);
var coordinateMapType = new CoordMapType();
function initialize() {
var mapOptions = {
zoom: 10,
streetViewControl: false,
mapTypeId: 'coordinate',
mapTypeControlOptions: {}
};
map = new google.maps.Map(document.getElementById("map_canvas"),
mapOptions);
google.maps.event.addListener(map, 'maptypeid_changed', function() {
var showStreetViewControl = map.getMapTypeId() != 'coordinate';
map.setOptions({'streetViewControl': showStreetViewControl});
});
// Now attach the coordinate map type to the map's registry
map.mapTypes.set('coordinate', coordinateMapType);
}