Google Maps API: Get url for current view - javascript

I have a form where users can introduce google maps urls to specify the address of some stuff.
I've been thinking in showing a map through Google Map API v3, letting user move to desired location and through a button or something automatically get the url of the place and copy it to an input.
I've been able to display the map using the tutorial, but I haven't been able to find in the documentation how I could get the url...
I think you will not need it, but this is the simple code I'm using:
var latlng = new google.maps.LatLng(-34.397, 150.644);
var options = {
zoom: 8,
center: latlng,
mapTypeId: google.maps.MapTypeId.ROADMAP,
mapTypeControl: false
};
var map = new google.maps.Map(document.getElementById('map-box'), options );

I'd suggest using map.getCenter().toUrlValue() and map.getZoom() to obtain the centre and zoom state of the current map view. That information should let you build a URI that you can then use, bare in mind that you'll need to write some code to take the values off the URI and pass them to the map API.

Related

"Cachebusting" / Obtain latest KMZ from NWS with GMaps API and JS [duplicate]

[EDIT] it seems the problem comes from google maps which takes some time to update the KML link...I'm not sure, but in the end, it works...[/EDIT]
I embedded an existing public google map on this website : http://www.ridetheflavour.fr
Here is the link of the public map : https://maps.google.fr/maps/ms?msa=0&msid=211027213468691902621.0004c8616605648d245b2
As you can see, the markers of the website's embedded map don't match the public google map markers. It seems it's not a matter of browser cache...
Here is the javascript snippet i'm using (Google map API V3) :
var mapOptions = {
center: new google.maps.LatLng(24.797409,-5.449219),
zoom: 3,
mapTypeId: google.maps.MapTypeId.TERRAIN,
overviewMapControl: false,
streetViewControl: false
};
var map = new google.maps.Map(document.getElementById("map_canvas"),
mapOptions);
var georssLayer = new google.maps.KmlLayer('https://maps.google.fr/maps/ms?ie=UTF8&authuser=0&msa=0&output=kml&msid=211027213468691902621.0004c8616605648d245b2');
georssLayer.setMap(map);
Any help would be greatly appreciated.
Google's servers cache the KML content for some period of time. To force the rendered KML to update, add a cache busting parameter to the URL. I usually use a function of the date/time if I need to do it programmatically, or if it is just a one time edit a manual ?a=0 and incrementing that as I make changes works.
Something like this (if you don't have any other query parameters in the URL):
var URL = filename+"?dummy="+(new Date()).getTime();
Or you can simplify it even further and use:
var URL = '[your kml url here]&ver=' + Date.now();
var georssLayer = new google.maps.KmlLayer(URL);

Google maps api weather layer and geolocating

I'm currently trying to use google maps to first display a map of United States with the weather and cloud google map layers and then zoom into the user's location using geolocation google maps api. My problem right now is I am only able to display the map without any of the weather, cloud, or geolocation information and only using the iframe basic view mode maps api. I am fairly new to javascript but have a div with an id "map-canvas" in my body tags and a javascript file mapWeather.js in my head tags.
mapWeather.js:
function initialize() {
var mapOptions = {
zoom: 4,
center: new google.maps.LatLng(37.6,-95.665)
};
var map = new google.maps.Map(document.getElementById('map-canvas'),
mapOptions);
var weatherLayer = new google.maps.weather.WeatherLayer({
temperatureUnits: google.maps.weather.TemperatureUnit.FAHRENHEIT
});
weatherLayer.setMap(map);
var cloudLayer = new google.maps.weather.CloudLayer();
cloudLayer.setMap(map);
}
google.maps.event.addDomListener(window, 'load', initialize);
You probably already know, but do note that this library has been deprecated and will stop working in June.

Show current user location in Google Maps (JavaScript)

I want to be able to show the current user location, and to automatically put a pin where the user is located. I need this in HTML/JavaScript, full code. Please help.
You've to use the Google API (JavaScript in this case) to show current location in maps. You've to import the library with:
<script src="https://maps.googleapis.com/maps/api/js?signed_in=true&callback=initMap" async defer>
And use the function initMap:
var map = new google.maps.Map(document.getElementById('map'), {
center: {lat: initLat, lng: initLon},
zoom: 6
});
When you obtain lat and lon from the navigation.geolocation, you've to update the map with map.setCenter(pos);, when pos is an array with lat and lon.
Here you've a full example.
Google official doc
I hope it helps :-)

mapquest pointing to center the map for a POI in Javascript API

I am using mapquest for adding new locations with following code from Javascript API.
window.map = new MQA.TileMap( /*constructs an instance of MQA.TileMap*/
document.getElementById('map'), /*ID of element on the page where you want the map added*/
7, /*intial zoom level of the map*/
{lat:17.73, lng:83.3}, /*center of map in latitude/longitude */
'map'); /*map type (map)*/
var poi=new MQA.Poi({lat:data.lat, lng:data.lng});
map.addShape(poi);
Now, what I want is, the map should be center to the newly added POI instead of the default one. I think there might be some API for this, but so far I could not trace it out. Please help me.
You should use
map.setCenter({lat:data.lat, lng:data.lng});
It will work.
What you need to do is call the slideMapToPoint function against the map with the latLng property of your newly created poi object.
// Create new POI
var poi = new MQA.Poi({lat:data.lat, lng:data.lng});
map.addShape(poi);
// Centre the map to the given POI.
map.slideMapToPoint(map.llToPix(poi.latLng));

Display retailers in google maps

I have a list of retailers and their logos and what I wanted to do was display them on a google map. I suppose the purpose of it is to demonstrate the extend of our market reach.
What I want to do is be able to add the markers, however when I click on them, I would want to replace the text in the speech bubble to my own text, and also insert the retailer logo for that location.
Is this possible? If anyone had any advice as to how I might go about doing this also would be fantastic.
This is certainly possible. First you need to do is use the Google Geocoding service to convert your list of retailers (I assume you have addresses) to a list of retailers with latitudes and longitudes.
Once you have a latitude, longitude information for your retailers you can create google.maps.marker objects for each one and attach them to a google.maps.Map object:
var myLatlng = new google.maps.LatLng(-25.363882,131.044922);
var myOptions = {
zoom: 4,
center: myLatlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
// where map_canvas is the id of a div that will contain the map on your page
var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
// for each of your retailers
{
var retailerLocation = new google.maps.LatLng(-25.363882,131.044922);
var retailerMarker = new google.maps.Marker({
position: retailerLocation ,
map: map,
title:retailerName
});
}
You can handle the click event on each of the retailerMarkers and display a google.maps.InfoWindow with the appropriate content (you can control the content of the InfoWindow as you would any other piece of your web UI).

Categories

Resources