OSM, OpenLayers and Google-maps always set to lon/lat (0,0) - google-maps

I am trying to adapt the following OSM/OpenLayers example that uses Google maps to a project of mine
http://openlayers.org/en/v3.0.0/examples/google-map.html
I built the following page which is pretty straightforward :
http://185.8.104.235/testOSM/testOSM.html
The problem is that the map is always set to lon/lat (0,0) when the page loads, when I would like to zoom to different coordinates.
IOW, view.setZoom(5) works, while view.setCenter([47, 3]) doesn't...
Any suggestion ?
Thanks in advance.

You are setting the center in degrees (because you maybe think coordinates are in EPSG:4326) whereas defaut coordinates in OpenLayers 3 use EPSG:3857 aka Spherical Mercator.
You must make the conversion from EPSG:4326 to EPSG:3857
In your code,
var center = ol.proj.transform(view.getCenter(), 'EPSG:3857', 'EPSG:4326');
should be
var center = ol.proj.transform(view.getCenter(), 'EPSG:4326', 'EPSG:3857');
and
view.setCenter([47, 3]);
should be
view.setCenter(ol.proj.transform([47, 3], 'EPSG:4326', 'EPSG:3857'));

Related

Cesium: Move camera to view entity if not visible

I want to make sure that an entity is always visible but it doesn't have to be in the center of the view (thus I couldn't use trackedEntity). In other words – let the entity move (it's a SampledPositionProperty) in the current view, let the user zoom and move the view as they wish but if the entity moves away from the view (or the user moves the view from the entity) then center the view around the entity once.
In Google Maps API, this is trivial:
if (!map.getBounds().contains(latLng)) {
map.setCenter(latLng);
}
I couldn't find a similar API in Cesium and I think that I must be missing something obvious. I've tried Entity.isShowing but it doesn't care about the camera view.
This is actually quite easy. What got me confused is using different objects (Cartesian3 or Cartographic) in different functions. This is my code:
// lat, lng, height defined
const cartographic = Cesium.Cartographic.fromDegrees(lng, lat, height);
if (!Cesium.Rectangle.contains(map.camera.computeViewRectangle(), cartographic)) {
const destination = Cesium.Cartesian3.fromDegrees(lng, lat, height);
map.camera.flyTo({destination, duration: .5});
}

Using a custom image marker on google static map?

how to have custom image marker on google static map, i need url format:
this is what i have tried, but its having default marker image
http://maps.google.com/maps/api/staticmap?center=25.3176452,82.97391440000001,&zoom=15&markers=25.3176452,82.97391440000001|25.3176452,82.97391440000001&path=color:0x0000FF80|weight:5|25.3176452,82.97391440000001&size=175x175&sensor=TRUE_OR_FALSE
This is what you want:
http://maps.googleapis.com/maps/api/staticmap?zoom=17&size=512x512&maptype=hybrid&markers=icon:http://cdn.sstatic.net/Sites/stackoverflow/img/favicon.ico|34.052230,-118.243680
for a custom image, put the icon:url after of markers...
Format
icon:url|lat,lng
Example
icon:http://cdn.sstatic.net/Sites/stackoverflow/img/favicon.ico|34.052230,-118.243680
a result of my example
For more information check google maps api, there are more information like size, markers position, center map, map type... etc.
https://developers.google.com/maps/documentation/static-maps/intro#MapTypes
*If the icon, img, don't appear it will be the size of image, try with another size,less than 64x64 px, format of image, "GIF, JPEG and PNG", the documentation recommend "PNG", if it doesn't appear check permision of image for public request(external request)...
According to the documentation you can specify custom icons like so:
markers=icon:...
e.g. amending your URL:
http://maps.google.com/maps/api/staticmap?center=25.3176452,82.97391440000001,&zoom=15&markers=icon:http://www.megaadresse.com/images/icons/google-maps.png|25.3176452,82.97391440000001&path=color:0x0000FF80|weight:5|25.3176452,82.97391440000001&size=175x175
Gives you:
PS: the sensor parameter is no longer required.
PPS: also you seemed to have specified the same coordinates twice in your markers parameter, so it was actually drawing two markers on the same location.
You can add an image in the marker:
var campaign_map = new google.maps.Map(document.getElementById("campaign_map_canvas"), mapOptions);
var selected_marker = new google.maps.Marker({
position: position,
map: campaign_map,
icon: 'path/to/image'
});
google.maps.event.trigger(campaign_map,'resize');
EDIT
You can use the following:
https://maps.googleapis.com/maps/api/staticmap?size=480x480&markers=icon:path/to/image.png%257C996600%7Carea/you/want/

How to replace bitmapData.copyPixels() with bitmapData.draw()

I'm trying to draw a level of my game using a tileset and a xml with the info. It works using copyPixels, but some tiles need to be flipped before they are drawn, so for that I need to use draw() instead of copyPixels(), but I can't get it to work. This is how I use copyPixes:
rectangleSelection = (desiredTile.x, desiredTile.y, tileWidth, tileHeight);
bmpData.copyPixels(tileset.bitmapData, rectangleSelection, new Point(pt.x, pt.y));
//pt.x and pt.y = tile location to be drawn().
How can I do the same thing using the bitmapData.draw() method? I just can't make it work.

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);
}