Google Maps api - drawing library fill polygon before closing shape - google-maps-api-3

I have a google map and I am using the drawing library to let the user draw polygons on the map by clicking to add points.
As a comparison I looked at trulia.com. I don't know how the drawing library setup is being made on trulia (uses backbone and other stuff). While I draw a shape, I want it to get filled as soon as I have 3 points, even if I am still drawing, and the fill should change as I add new points (indicating what the shape/area would be if you'd close it in that moment).
On trulia.com, as soon as you have a 3rd point, the area designated by the existing points gets filled, even though you haven't finished adding points. They're using google maps api, right? But I can't find the setting for something like "fill shape as you add points". I've searched google a lot, no luck.
Does anyone know how to setup the map or the drawing library to have that behavior? I don't think that this behavior can be setup in the polygonOptions (I've looked at all the options documented on developers.google.com)... so, the setting must be somewhere else...

MrCroft, it seems #ddtpoison777 asked a similar question and found the solution among some Google Maps API samples. This is the relevant code taken from the example:
var poly;
var path = new google.maps.MVCArray;
function initialize() {
poly = new google.maps.Polygon({
strokeWeight: 3,
fillColor: '#5555FF'
});
poly.setMap(map);
poly.setPaths(new google.maps.MVCArray([path]));
google.maps.event.addListener(map, 'click', addPoint);
}
function addPoint(event) {
path.insertAt(path.length, event.latLng);
var marker = new google.maps.Marker({
position: event.latLng,
map: map,
draggable: true
});
markers.push(marker);
marker.setTitle("#" + path.length);
}

Related

Google Map API : Marker within marked polygon

I have nearly 4 to 5 thousand markers plotted on the map. I want user to allow to draw a polygon on the map and then delete those markers which are within the shaded polygon. Can anyone please guide me on how to find which markers are within the shaded area of polygon ?
Firstly you'll need to use the geometry library. Append libraries=geometry to the query string of the URL you use for loading the Maps API:
<script src="https://maps.googleapis.com/maps/api/js?libraries=geometry"></script>
Then you can use the containsLocation function to check if each marker is inside the polygon.
for (var i = 0; i < markers.length; i++) {
if (google.maps.geometry.poly.containsLocation(markers[i].getPosition(), yourPolygon)) {
markers[i].setMap(null);
}
}
I'm assuming here you've got these 5k markers in an array called markers. And by 'delete those markers' you simply mean remove them from the view. You'd perhaps also want to fire off an ajax request at this point, and/or remove them from that markers array.

Maps API v3 clustering at different zoom levels

I am working on a map project where I need clustering. I implemented a store locator and used MarkerClusterer which works fine. Now I have another requirement from the customer and I wonder which solution I should use to achieve these goals:
Zoom level 0: Cluster markers within 1500km distance to each other
Zoom level 1: Cluster within 1000km
Zoom level 2: Cluster within 750km
Zoom level 3: Cluster within 400km
Zoom level 4 and above: no clustering
I was thinking I should be using MarkerManager and MarkerClusterer together, but I am not sure about that. Any help to get me on the right path?
Thanks in advance!
You can only use MarkerClusterer, but you have to modify some options.
To prevent the clusterer acts after level 4, you have to use maxZoom property. It defines the max level where the clusterer can cluster marker, so in your case, you have to fix it at 4.
Then, to change the size of the grid depending the zoom level, use gridSize property. You can find a definition of all option fields on the doc.
So, your MarkerClusterer instanciation will look like something like this :
var mcOptions = {gridSize: /*Your value*/, maxZoom: 4};
var markerCluster = new MarkerClusterer(map, /*your array of markers*/ mcOptions);

Draw line between given coordinates using Google Map API [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
polyline snap to road using google maps api v3
My usecase is very common but somehow not finding right solution. I keep recording the coordinates of a moving vehicle and then display the predefined route along with the traveled path. The predefined route and traveled path should be displayed in different color. For displaying predefined path I am using DirectionsService.route. And for traveled path I tried different approaches and none of them worked perfectly.
Draw a Polyline between all the coordinates recorded in path traveled. But in case turn falls between 2 recorded coordinates then the line directly joins the points ignoring the turn.
var line = new google.maps.Polyline({
path: arr_all_coordinates, strokeColor: "red", strokeOpacity: 1, strokeWeight: 1
});
Use new DirectionsRenderer objects to form a route between 2 consecutive coordinates and display with DirectionsService.route till the last coordinate. In this case map keep jumping to the last 2 coordinates and gives focus to the last route drawn. I tried setting preserveViewport to true but didnt work.
directionsService.route(request, function(response, status) {
if (status == google.maps.DirectionsStatus.OK) {
directionsRenderer.setDirections(response);
}
Any suggestion please.
This is basically the same problem as drawing a line that snaps to roads. See my solution for that problem: polyline snap to road using google maps api v3

postal code to google map

I am a newbie in google map. I learn from google map documentation but I don’t found anything about postal code to google map. I only have a postal code to get the google map. If so can I get a map from postal code.
I also find in google for tutorials for that but I don't found anything. If you have a useful code or tutorial, please help me.
You first need to get the latitude/longitude from address/postcode. There is a Google GeoCodeing API for getting longitude and lattitude from address and possibly post code. It got some usage limits though. Have a look
http://code.google.com/apis/maps/documentation/geocoding/
Once u get the long/lat u can then feed these into google mapping api
E.g. Using the following javascript api
http://maps.google.com/maps/api/js?sensor=false
You can do
RenderMap(54.1109429427243, -3.197265625, 6, "resultsMapHome");
function RenderMap(lat, lng, zoomLevel, divName) {
    var position = new google.maps.LatLng(lat, lng);
    var options = {
        zoom: zoomLevel,
        center: position,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    };
    map = new google.maps.Map(document.getElementById(divName), options);
    map.setCenter(position);
}
I can't format the script as I am typing from my iPhone. I will reformat it later when I get to work.
http://maps.google.com
Put in the post code, zoom in / out until you have the position you'd like the map on your own website. Then click the 'link' button to the right of the search bar (looks like a chain). This gives you the appropriate code to put that map into your website.
Is that what you wanted? I couldn't understand your English very well.

Google Maps Javascript API accidentally displaying view 'off the map'

I'm using the Google Maps Javascript API and I have LatLng coordinates that are dynamically generated. The problem is, sometimes I get coordinates and a zoom level that creates a map view that's 'off the chart' and this confuses users into thinking their map is not working. Is there a set of coordinates I should restrict values to in order to avoid this?
Example:
map = new google.maps.Map(document.getElementById("map"), {
center: new google.maps.LatLng(-89.16090481395844, 59.24382269379974),
zoom: 13,
mapTypeId: 'roadmap',
scaleControl: true,
navigationControlOptions: { style: google.maps.NavigationControlStyle.ZOOM_PAN }
});
Users don't know what's happening unless they zoom out:
Google Maps imagery stops at lat 85.0511 (-85.0511).
The value comes from features of spherical mercator projection that Google Maps is based on. You can find the formula used to calculate the value here: http://en.wikipedia.org/wiki/Mercator_projection#Uses

Resources