It is very simple to create a google map and center on a given location (see below). It is also similarity simple to create a google map and show markers on it by doing something like markers=color:blue%7Clabel:S%7C11211%7C11206%7C11222.
How would I just create a simple centered map like the below, but add a single marker in the very center? Thanks
<img alt="Map" src="http://maps.google.com/maps/api/staticmap?center=+AUBURN+WA+98001&zoom=14&size=400x400&sensor=false">
I guess that the map automatically will fit to show the specified markers,
here you will find
Marker Locations
Each marker descriptor must contain a set of one or more locations
defining where to place the marker on the map. These locations may be
either specified as latitude/longitude values or as addresses. These
locations are separated using the pipe character (|).
The location parameters define the marker's location on the map. If
the location is off the map, that marker will not appear in the
constructed image provided that center and zoom parameters are
supplied. However, if these parameters are not supplied, the Static
Map server will automatically construct an image which contains the
supplied markers. (See Implicit Positioning below.)
You could also add your own centered background image ontop if the map :)
background: url('yourmarker.png' ) center no-repeat, url(http://maps.google.com/maps/api/staticmap?center=AUBURN+WA+98001&zoom=14&&size=400x400);
here i am giving the code in step by step
NSString *strLat = [NSString stringWithFormat:#"%f",_message.latitude];
NSString *strLong = [NSString stringWithFormat:#"%f",_message.longitude];
NSString *strURL = [NSString stringWithFormat:#"%#center=%#,%#&zoom=15&size=200x200&markers=color:red%%7Clabel:C%%7C%#,%#&key=%#",https://maps.googleapis.com/maps/api/staticmap?,strLat,strLong,strLat,strLong,GOOGLE_API_BROWSER_KEY];
NSLog(#"strURL %#",strURL);
strURL = [strURL stringByReplacingOccurrencesOfString:#"%%" withString:#"%"];
now you have a image url
Google Static Maps Reference For Addresses:
https://developers.google.com/maps/documentation/maps-static/dev-guide#Addresses
Here's a JavaScript function that I've used recently to do this. The addressElements parameter passed to the function is an array of address elements e.g. ['10 Main St', 'Brownsville', 'MS', '27123', 'USA'].
You can see that I've commented out the center and zoom properties as you don't need those when you define a marker. To remove the marker, just uncomment those two lines and comment out the markers line.
You can obviously construct this image yourself, but I used the URLSearchParams to simplify parameter creation and have them URL encoded automagically.
function getGoogleMapsImage(addressElements) {
var image = Nucleus.element('img');
image.width = '256';
image.height = '256';
var joined = addressElements.join(',');
var params = new URLSearchParams();
//params.append('center', joined);
//params.append('zoom', '15');
params.append('size', '256x256');
params.append('maptype', 'roadmap');
params.append('key', 'YOUR_API_KEY_HERE');
params.append('markers', 'color:red|label:C|' + joined);
var url = 'https://maps.googleapis.com/maps/api/staticmap?' + params.toString();
image.src = url;
return image;
}
Related
I am using google-map-react to render maps in my react app.I am displaying dynamic number of markers on the map.I need to fit the view of map so that it just encloses all the markers.I went through this thread but found it a bit complicated.Can anyone please give me an overview of how this can be done?
It happens that I had to do the exact same thing recently. I found that existing libs were more getting on my way than actually helping so I just went with writing my own component.
In case you decide going the same way - here's how to do what you want:
First - you need to get the bounds object enclosing all your markers. Here we assume that this.state.markers contains an array of google.maps.Marker objects
Your component could have the following method to do that:
getObjectsBounds(){
let objectsBounds = new google.maps.LatLngBounds();
Object.values(this.state.markers).map((marker) => {
let lat = parseFloat(marker.getPosition().lat());
let long = parseFloat(marker.getPosition().lng());
let point = new google.maps.LatLng(lat, long);
objectsBounds.extend(point);
});
return objectsBounds;
}
Now we just use fitBounds on the above funtion's return results. Here we assume that this.map is your gmap, i.e. what google.maps.Map( ... your args ... ) returns.
fitBounds(){
let bounds = this.getObjectsBounds();
this.map.fitBounds(bounds);
}
I have two pages.
On the first page, there is a google searchbox.
On the second page, there is the google map.
When the user searches for a place on the map, I get it's lat and lng and send the user to the second page, setting the map's center to that location.
google.maps.event.addListener(this.sb, 'places_changed', function() {
var place = this.getPlaces()[0],
viewport,
lat,
lng;
if (place) {
lat = place.geometry.location.lat();
lng = place.geometry.location.lng();
// viewport = place.geometry.viewport;
// sessionStorage.setItem('sViewport', JSON.stringify(viewport));
// How can I find out the proper zoom for this search and se it as a query string param?
window.location.href= 'rezultate.html?lat='+lat+'&lng='+lng.toString()+'&zoom=?????????;
return;
}
$('.search-error').css('display', 'block');
});
The problem is I cannot find a way to get the proper zoom for that search.
Any ideas?
What you need to use is the place's geometry's viewport bounds rather than the place location.
If you use those coordinates to fit bounds on your map, you will get the correct viewport for that place.
Example:
const viewportBounds = place.geometry.viewport;
in google maps how can weput numbers on map instead of generic (if multiple number of icons listed means number have to increment like 1,2,3 )
instead of
var iconBlue = new GIcon();
iconBlue.image = 'http://labs.google.com/ridefinder/images/mm_20_blue.png';
iconBlue.shadow = 'http://labs.google.com/ridefinder/images/mm_20_shadow.png';
iconBlue.iconSize = new GSize(12, 20);
iconBlue.shadowSize = new GSize(22, 20);
iconBlue.iconAnchor = new GPoint(6, 20);
iconBlue.infoWindowAnchor = new GPoint(5, 1);
is this possible in google maps ???
here i found some link but it's not working.
It's not possible to do with the current API (v3) - but you can use the StyledMarker Library. It extends the marker class and allows you to style the icon and put text inside the icons if you want as well (which is what I believe you're looking for). Just include the script in your application (after you load the Maps API script). And then add the StyledMarker like you would the regular marker object:
var styleMaker1 = new StyledMarker({
styleIcon: new StyledIcon(StyledIconTypes.MARKER, {
color: "FFFFFF",
text: "1" //this is where you'll set your incremented value
}),
position: myLatLng,
map: map
});
Here's a fiddle of how it looks: http://jsfiddle.net/svigna/7L2wY/
I have been making a script using a fusion table's layer in google maps.
I am using geocoder and get the coordinates of a point that I need.
I put a script that changes the style of a polygon from the fusion table when you click on it, using the google.maps.event.addListener(layer, "click", function(e) {});
I would like to use the same function that I call in the case of a click on the layer, but this time with a click with the coordinates that I got.
I have tried google.maps.event.trigger(map, 'click', {latLng: new google.maps.LatLng(42.701487,26.772308)});
As well as the example here > Google Fusion Table Double Click (dblClick)
I have tried changing map with layer...
I am sorry if my question is quite stupid, but I have tried many options.
P.S. I have seen many post about getting the info from the table, but I do not need that. I want to change the style of the KML element in the selected row, so I do not see it happening by a query.
Here is the model of my script:
function initialize()
{
geocoder = new google.maps.Geocoder();
map = new google.maps.Map(document.getElementById("map_canvas"),myOptions);
layer = new google.maps.FusionTablesLayer({
suppressInfoWindows:true,
map : map,
query : {
select: 'Местоположение',
from: '12ZoroPjIfBR4J-XwM6Rex7LmfhzCDJc9_vyG5SM'
}
});
google.maps.event.addListener(layer, "click", function(e) {
SmeniStilRaionni(layer,e);
marker.setMap(null);
});
}
function SmeniStilRaionni(layer,e)
{
...
}
function showAddress(address)
{
geocoder.geocode( { 'address': address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
var point = results[0].geometry.location;
//IMITATE THE CLICK
}
});
}
In response to geocodezip
This way you hide all the other elements... I do not wish that. It is like if I want to change the border of the selected element. And I do not wish for a new layer.
In the function that I use now I push the style of the options of the layer and then set the option. I use the e from google.maps.event.addListener(layer, "click", function(e)); by inserting e.row['Name'].value inside the where rule.
I would like to ask you if there is any info on the e variable in google.maps.event.addListener(layer, "click", function(e));
I found out how to get the results I wanted:
For my query after I get the point I use this:
var queryText ="SELECT 'Районен съд','Окръжен съд','Апелативен съд','Местоположение' FROM "+FusionTableID+" WHERE ST_INTERSECTS(\'Местоположение\', CIRCLE(LATLNG(" + point.toUrlValue(6) + "),0.5));";
queryText = encodeURIComponent(queryText);
document.getElementById("vij query").innerHTML = queryText;
var query = new google.visualization.Query('http://www.google.com/fusiontables/gvizdata?tq=' + queryText);
And then I get these results:
var rsyd = response.getDataTable().getValue(0,0);
var osyd = response.getDataTable().getValue(0,1);
var apsyd = response.getDataTable().getValue(0,2);
And then, I use the following:
where: "'Районен съд' = '"+rsyd+"'",
Which is the same as:
where: "'Районен съд' = '"+e.row['Районен съд'].value+"'",
in the click function.
This is a working solution for my problem.
But still, I cannot find a way to Imitate a Mouse click.
The issue is this: the data(row) for the layers will be requested via AJAX when you click on a layer.
In theory it's possible to select a geometry(polygon) by a given LatLng, the geometry-library has a method for this: google.maps.geometry.poly.containsLocation().
Unfortunately the FusionTableAPI does not support such queries(ST_CONTAINS), you cannot select a row by supplying a LatLng and selecting the rows where the geometry contains the LatLng.
So what you can do: create a copy of the table that contains the columns needed to select a row(Община...the distinct value, and Местоположение.... the geometry). This copy may be requested via AJAX, but when the FusionTable will not be modified anymore I would suggest to use a hardcoded copy.
What to do now when you want to simulate the click:
Iterate over all rows of the copy, use the mentioned containsLocation()-method to check if the geometry(Местоположение) contains the LatLng and when it does apply the query by using the value of the distinct column Община .
Of course it would take some time to check all the geometries, but the FusionTable is not very large, it should be a possible approach in your case.
Here is a demo: http://jsfiddle.net/doktormolle/sSwj3/
The size of the stored data is approximately 500kb, you should store the data in an external script, so they may be cached.
You may notice that the highligthning of the selected layer will be much faster when triggered via the links, because there will no data be requested via AJAX. When the highlightning of the selected feature is the only thing you need you may ommit the observation of the layer-click completely and observe the map-click instead. Use the returned LatLng to retrieve the selected row from the data and set the style:
Demo: http://jsfiddle.net/doktormolle/swdX8/
I am using Google Map v2 and I only want map view and nothing more. I also would like to get rid of the zoom in and out buttons.
If anyone knows what I need to add to the following that would be great.
function stores()
{
$('#storelist ul#stores').html("");
fetch(203,"task=location&location=vic");
map = new GMap2(document.getElementById("map_canvas"));
map.setCenter(new GLatLng(-37.810013, 144.962683), 8);
map.setUIToDefault();
yellowIcon.image = "http://gmaps-samples.googlecode.com/svn/trunk/markers/orange/blank.png";
markerOptions = { icon:yellowIcon };
}
Remove map.setUIToDefault(). It adds the default look and feel to the Map.
(Reference docs: http://code.google.com/apis/maps/documentation/javascript/v2/reference.html#GMap2.setUIToDefault)
If you want, you can also customise how the map can be interacted with. For example map.disableDoubleClickZoom() or map.disableDragging(). See the reference above for details.