center parameter not working in new google maps embed - google-maps

I have used the new embed code for the new google maps as documented here:
https://developers.google.com/maps/documentation/embed/guide
My code is:
<iframe width="500"
height="450"
frameborder="0" style="border:0"
src="https://www.google.com/maps/embed/v1/place?key=KEY&q=Space+Needle,Seattle+WA">
</iframe>
This works fine and displays the map correctly
When I try to add the "center" parameter using the below code it zooms the map out to the whole world view
<iframe width="500"
height="450"
frameborder="0" style="border:0"
src="https://www.google.com/maps/embed/v1/place?key=KEY&q=Space+Needle,Seattle+WA&center=47.620467,-122.349116">
</iframe>
What I ultimately want to do is move the map so it is not centered on the actual marker, but at the moment I am testing with a lat and long that are very close to the marker.
Am I using the &center parameter correctly? Has anyone else got the &center parameter working?
I have checked that the lat and long I used are in the map displayed

Alas this seems to be a bug with the current Embed API. The documentation indicates that it's possible to use 'center=' with /place but in practise this doesn't work.
I ran into the same problem, adding 'center=' to the iframe URL broke the map. It worked flawlessly when I changed '/place' into '/view' and removed the 'q=...' part. I've sent feedback to Google about this. You can do this through the 'Feedback on this document' link on this page:
https://developers.google.com/maps/documentation/embed/guide#optional_parameters
To work around this I've switched to the Google Maps Javascript API v3. This API is quite a bit more flexible and as a bonus it also rids you of the iframe.
In your case this should do the trick:
Put this just before
<script type="text/javascript">
var map;
function initialize() {
var mapOptions = {
zoom: 13,
center: new google.maps.LatLng(47.620467,-122.349116),
};
map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
var myLatlng = new google.maps.LatLng(47.620614, -122.348880);
var marker = new google.maps.Marker({
position: myLatlng,
map: map,
title: 'Space Needle, Seattle WA'
});
}
function loadScript() {
var script = document.createElement('script');
script.type = 'text/javascript';
script.src = 'https://maps.googleapis.com/maps/api/js?key={API_KEY}&callback=initialize';
document.body.appendChild(script);
}
window.onload = loadScript;
</script>
And add
<div id="map-canvas" style="width: 500px; height: 450px;"></div>
to your HTML somewhere.
A disadvantage is that you need to manually adjust the Lat/Lon for the marker by searching Google Maps, right click and then "What's here". You already need to do this for the center coordinates so this shouldn't be a big problem. If you want to automate this you can use the Google Maps Geocoding service but that would happen on every view:
https://developers.google.com/maps/documentation/javascript/examples/geocoding-simple

if you want to do this without having to use the API, you can still use the old Google Maps Classic coding. for centering, you can add the following to the "src" code:
&ll={latitude}, {longitude}
so for instance "&ll=44.013922, -88.342259". just make sure it is INSIDE the quotes for the src.
you can also set a custom zoom by using
&z=#
for example: "&z=20" will get you close to Street View.
who knows how long this will last though, so use at your own risk, and be sure to check on it regularly...

Another workaround using Google MyMap web GUI that has worked for me. Go to the create/edit map page, using mouse or cursors drag a map while it's centered as you want it to be when opened via embedded link, then click "Set default view" (same dialog where new/open/save map can be found).

Related

How do I embed fully Google map to website

I want embed fully Google map into my website. It mean includes search bar in left.
google map
The link directly on browser address bar will be work https://www.google.com/maps/search/Automation+Supplies+Limited/#53.4455454,-7.5863638,8z?hl=en-IE But when I place it in iframe it not work.
<iframe src="https://www.google.com/maps/search/Automation+Supplies+Limited/#53.4455454,-7.5863638,8z?hl=en-IE"></iframe>
Thanks
You can't.
This isn't offered by Google. All you can do is link to Google Maps.
In the google map link that you have mentioned above, open it and click on Menu bar. Choose Share or embed map and you may find your html code for website.
Here is an example of your map: <iframe src="https://www.google.com/maps/embed?pb=!1m12!1m8!1m3!1d1212742.5438103776!2d-8.069837255079928!3d53.59579689997422!3m2!1i1024!2i768!4f13.1!2m1!1sAutomation%20Supplies%20Limited!5e0!3m2!1sen!2s!4v1573529617789!5m2!1sen!2s" width="400" height="300" frameborder="0" style="border:0;" allowfullscreen=""></iframe>.
If you want to show the map in your website and put a search bar in the left side, as an alternative, you can use Google Maps Platform's Maps JavaScript API to do this.
Here is a sample code I made that looks similar to the implementation that you want which is in the image in your link.
In this example, I used the Maps JavaScript API to show the map
var map = new google.maps.Map(document.getElementById('map'), {
center: {lat: 53.467079, lng: -6.719628},
zoom: 8,
mapTypeControl: true,
mapTypeControlOptions: {
style: google.maps.MapTypeControlStyle.HORIZONTAL_BAR,
position: google.maps.ControlPosition.TOP_RIGHT
},
});
and use the Google Maps Places API library to put the Autocomplete
var autocomplete = new google.maps.places.Autocomplete(input);
as a search mechanism on the map.
Hope this helps.

How to reinitialize Google map API?

I am new to the coding part.So I am in need for your kind ideas.
How can i reinitialize the loaded Google map API in one HTML page (eg: MapView.html) from parent HTML page using IFRAME tag?
Is there any possibilities to reload the Google map to view the updated markers in new page without pressing F5 key?
The iframe part makes it very difficult as iframe is used basically for accepting content from an external source without any changes. It would be easier if you injected the script which utilizes Google Map in every page you use it.
I don't understand much what you mean by parent HTML :( If it is a single-page application and the child HTML is a subview of the parent, you could be able to access the parent HTML's controller but I have the feeling that this is not the case :)
So generally you can reinitialize the map using JavaScript. Have a look in the API Reference for all the options:
https://developers.google.com/maps/documentation/javascript/3.exp/reference
If you could create the map in your child HTML, the Marker can be created and updated by following:
var map = new google.maps.Map(document.getElementById('map'), yourMapOptions); // This is the map initiation in a #map element
var marker = new google.maps.Marker({
position: new google.maps.LatLng(...), // the old position
map: map, // The map you created in your element by new google.maps.Map(...)
icon: 'assets/marker.png',
title: 'This is the pointer',
});
setTimeout(function() {
marker.position = new google.maps.LatLng(...); // new position
marker.setMap(map); // This reinitializes the marker
}, 10000); // For this example I set a 10s timeout
Sorry if the iframe is an important thing but without any code I assumed this could help you more than hacking an iframe.

Marker drag not working in iframe under Chrome and Firefox

I'm working on embedding the google map, into a page, containing multiple iframes. It can happen, that I load 2 maps, into 2 different iframes at the same time. This is the reason why the Map API loaded only once, in the main document's head section:
<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyDY0kkJiTPVd2U7aTOAwhc9ySH6oHxOIYM&v=3&sensor=false" type="text/javascript"></script>
For loading the map inside the iframe, I wrote the following code, which runs inside the iframe:
var googleAPI = window.top.google;
var latLong = new googleAPI.maps.LatLng(31.7655374094844,93.515625);
var map = new googleAPI.maps.Map(document.getElementById("map-container"), {
zoom: 8,
center: latLong,
mapTypeId: googleAPI.maps.MapTypeId.ROADMAP
});
The map appears correctly on the page, without any errors. I also add a marker on the map and I want to allow the users to change the marker's position, by drag&drop
var simpleMarker = new googleAPI.maps.Marker({
position: latLong,
map: map,
draggable: true,
animation: googleAPI.maps.Animation.DROP,
title: 'Marker'
});
While under IE the marker's drag&drop without any problems, under Chrome&Firefox the drag it's not working as expected: sometimes the drag stops or you even get an endless drag.
My guess is that this is caused because the API was loaded in the main document, and after referenced from inside the iframes. I do not wish to change this, mostly because like I previously mentioned, I can have several maps on the page.
Is there a fix for the drag&drop issue?
I would appreciate any help you could give me,
Thanks
I don't have a solution, I'm afraid this kind of API-usage is not intended.
Take a look at the start of the API (https://maps.gstatic.com/intl/en_ALL/mapfiles/api-3/16/10/main.js)
(function(){'use strict';var k=window/*..more code..*/}).call(this);
This function will be called in the parent document of the iframe, the variable k(which will be used internally by the API as reference to the window-object) will always refer to the parent window of the iframe what will result in incorrect values when observing events (e.g. mousemove when you drag a marker) in the iframe.

Recreating Standard Google Map embed with Google Maps API

So, having been recently somewhat dissapointed with lack of customizability of the regular google maps "embed" (iframe) code; I have started tinkering with the Google Maps API v3. Really, all I want to do is show a marker for a business on the map, so that you can click it and go to that "place" at mapsgoogle.com.
So pretty much, I just want to recreate the functionality of the iframe code below. I put in about an hour of reading the docs, but it seems extremely complicated just to get the marker associated with a 'place'
The place
https://maps.google.com/maps?cid=1311411133662139490
The standard Embed
<iframe width="425" height="350" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="https://maps.google.com/maps?cid=1311411133662139490&ie=UTF8&hq=&hnear=&t=m&iwloc=A&ll=41.097905,-73.405006&spn=0.006295,0.006295&output=embed"></iframe><br /><small>View Larger Map</small>
It appears as though there is no functionality in the api to use the cid.
To Elaborate a little
Generally I would use this just for small business websites. I was frustrated with the regular iframe embed and lack of customizability. Essentially I want a starting point from which I can play with stuff and heavily customize the look/feel, but have been unable to put a marker in that's associated with the data for a "place" - allowing for the little pop-up window, etc..
Honestly, I didn't really do enough research before asking this question - and came in with some misconceptions. I think, and I may be wrong, that the API is still what I want to be using ultimately, but had I know about the functionality in Rick's answer, I probably would have settled on that and procrastinated longer on learning the gmaps API.
Allow me to explain one option of achieving your goal. I use the marker and infoWindow objects that Google Maps API v3 offers, which you can find in the document I attached in the link. Feel free to follow along in the jsFiddle I created: http://jsfiddle.net/bgvYH/
First thing is first, you want to initiate your map with its options - I'm going to assume you know what the different variables in following code snippet represent:
var myLatlng = new google.maps.LatLng(41.097905,-73.405006);
var myOptions = {
zoom: 16,
center: myLatlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
If you want to customize your map even more to your liking, have a look at the different options you can set in the API reference, you'll set these options in the myOptions object ( https://developers.google.com/maps/documentation/javascript/reference#MapOptions ).
Note: I set the center of the map to the Lat/Long coordinates of the restaurant - which I took from the URL you provided in the iframe ll=41.097905,-73.405006.
Now what you want to do next is determine the content you want to display in your infoWindow, so the restaurant information:
var contentString = "<div id='content'>";
contentString += "<div id='title'>Mr. Frosty's Deli and Grill</div>";
contentString += "<div id='info'><p>10 1st Street</p><p>Norwalk, CT 06855</p><p>(203) 956-5767</p><p><a href='http://thebeachburger.com/'>thebeachburger.com‎</a></p></div></div>";
You may even end up pulling this information from a database or JSON object in the future, depending on how deep you go into this project (for now I have it as static HTML).
Next we initialize the infoWindow object and set the contentString to the content option of the infoWindow. There are other options you can customize here (just like the map options, again look at the reference for InfoWindowOptions: https://developers.google.com/maps/documentation/javascript/reference#InfoWindowOptions )
var infowindow = new google.maps.InfoWindow({
content: contentString
});
After setting up your infoWindow object, you initialize your marker object - which will place the drop the bubble on the map. Once again, you set up the options for the marker when initializing much like you did with the map object and the infoWindow object - you can further customize it to your liking by looking at the reference (I think there's even an option in there for the marker where you can use custom icons - you can get pretty creative here).
var marker = new google.maps.Marker({
position: myLatlng,
map: map,
title:"Mr. Frosty's Deli and Grill"
});
And finally, you need to bind the Marker and the infoWindow together - so that when a user clicks on the marker the info pops up. This is achieved by using the event listener, and you listen for a "click" action on the marker variable. Read this document for information on events on google maps https://developers.google.com/maps/documentation/javascript/events. Likewise look through the API Reference for the different events you can listen to on an object.
google.maps.event.addListener(marker, 'click', function() {
infowindow.open(map,marker);
});
That should do it, you should have a working alternative to the iframe you include - except now you can customize the map and the actions you perform on it to however you want. In the jsFiddle I also included some styling, just to make things look nice inside the infoWindow.
Now, I want to let you know - I believe there is another option to what your looking for - but I have yet to experiment with this API. It is the Google Places API, which you'll have register for. But from what I read through the documents, I think you may be able to achieve what you want to do. Have a look at it ( https://developers.google.com/maps/documentation/places/ ), and see what's good.
It looks like this was created through 'My Places' and made public. If you don't want to mess with the API then that's your best bet.
Visit maps.google.com, click 'My Places' and 'Create Map'. Customize and grab the embed code.
If the map doesn't need to be interactive (beyond the click action), use a static map. It's just an image so you can wrap it in an anchor that points exactly where you want.

How to update Bing Maps or Google Maps dynamically, depending on the address given to it

On a project I'm building, I want the user to be able to select an address which is stored in the database, and then click on that address to view more information. When the user clicks on the link, it will take them to another page which will display more information, as well as a Map of the address.
What I want to do is get the map to locate the address dynamically, instead of using a pre-defined embed link. But I'm not entirely sure if what I want to do can be done, and if it can be done I don't know how to do it.
So essentially, I want to know how I can take the address from the database, and pass it to the embedded map on my project?
It doesn't really make a difference to me if Bing or Google is used.
Cheers!
Yes what you want can easily be done. Both Bing Maps and Google Maps offer geocoding services, where you give the service an address, and the services returns as part of its results longitude and latitude coordinate of the address on the map. I could paste a bunch of code here but you can find all the code sample you need for both maps in my links above.
When the user clicks on your address link, you bring them to your detailed information page. On that page, you will have javascript to set up your map. In addition to the javascript setting up your map, you will need more javascript, detailed in the links above, to call the geolocation services. In essences, you pass to the services the address you want to look up, and if the address is valid the service will return to you the longitude and latitude coordinates of that address. Using this information, you can then zoom to that location on the map, and optionally put a pushpin there to indicate the address.
See here for an example in google maps.
Interactive SDK of Bing Maps Click on REST Services -> Find a location by query.
I am assuming you are interested in the ajax version of the map controls, but the other versions should be quite similar and should work more or less the same.
You can always do something like this:
<div id="location"></div>
<script type="text/javascript">
function initialize() {
var gpLatlng = new google.maps.LatLng(latitude, longitude);
var gpOptions = {
zoom: 15,
center: gpLatlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
var map = new google.maps.Map(document.getElementById("location"), gpOptions);
var marker = new google.maps.Marker({
position: gpLatlng,
map: map,
title:"Title"
});
}
function loadScript() {
var script = document.createElement("script");
script.type = "text/javascript";
script.src = "http://maps.google.com/maps/api/js?sensor=false&callback=initialize&region=GB";
document.body.appendChild(script);
}
window.onload = loadScript;
</script>