Get Road coordination From google api using geocode - google-maps

using google map api i want to get path of road using just the address
what i have made so far
code
<script type="text/javascript">
var geocoder;
var map;
var infoWindow;
function initMap() {
geocoder = new google.maps.Geocoder();
map = new google.maps.Map(document.getElementById('map'), {
zoom: 8,
center: {lat: 0, lng: -180},
});
/* var marker = new google.maps.Marker({
position: myLatLng,
map: map,
title: 'Hello World!'
});*/
codeAddress();
infoWindow = new google.maps.InfoWindow;
}
function codeAddress() {
geocoder.geocode( { 'address': "my adress goes here"}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
console.log(results);
var obj1={lat:results[0].geometry.bounds.j.H,lng:results[0].geometry.bounds.H.H};
var obj2={lat:results[0].geometry.bounds.H.j,lng:results[0].geometry.bounds.j.j};
map.setCenter(results[0].geometry.location);
var marker = new google.maps.Marker({
map: map,
position: results[0].geometry.location,
title:"my address "
});
} else {
alert("Geocode was not successful for the following reason: " + status);
}
});
}
</script>
what i need is to get start of the address and the end so that i can use polyline using google api any idea how to achieve this, thank you

What I need is to get start of the address and the end so that i can use polyline using google api any idea how to achieve this?
You can make use of google.maps.Polyline({params});
From the Map sample:
function initMap() {
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 3,
center: {lat: 0, lng: -180},
mapTypeId: google.maps.MapTypeId.TERRAIN
});
var flightPlanCoordinates = [
{lat: 37.772, lng: -122.214},
{lat: 21.291, lng: -157.821},
{lat: -18.142, lng: 178.431},
{lat: -27.467, lng: 153.027}
];
var flightPath = new google.maps.Polyline({
path: flightPlanCoordinates,
geodesic: true,
strokeColor: '#FF0000',
strokeOpacity: 1.0,
strokeWeight: 2
});
flightPath.setMap(map);
}
Additional reading from the Maps Polyline docs:
Polylines
To draw a line on your map, use a polyline. The Polyline class defines a linear overlay of connected line segments on the map. A Polyline object consists of an array of LatLng locations, and creates a series of line segments that connect those locations in an ordered sequence.
Add a polyline
The Polyline constructor takes a set of PolylineOptions specifying the LatLng coordinates of the line and a set of styles to adjust the polyline's visual behavior.
Polyline objects are drawn as a series of straight segments on the map. You can specify custom colors, weights, and opacities for the stroke of the line within the PolylineOptions when constructing your line, or you can change those properties after construction. A polyline supports the following stroke styles:
strokeColor specifies a hexadecimal HTML color of the format "#FFFFFF". The Polyline class does not support named colors.
strokeOpacity specifies a numerical value between 0.0 and 1.0 to determine the opacity of the line's color. The default is 1.0.
strokeWeight specifies the width of the line in pixels.
The polyline's editable property specifies whether users can edit the shape

Related

Google map API Error-Uncaught RangeError: Maximum call stack size exceeded

When setting the maxZoom:18
I am getting error in my console
js:101 Uncaught RangeError: Maximum call stack size exceeded
at Py._.k.get (js:101)
at ly._.k.get (js:101)
at my (map.js:12)
at ly._.k.zoom_changed (map.js:40)
at Hb (js:38)
at ly._.k.set (js:101)
at my (map.js:12)
at ly._.k.zoom_changed (map.js:40)
at Hb (js:38)
at ly._.k.set (js:101)
Here is my js code ..
$scope.map = new google.maps.Map(document.getElementById('map'), {
maxZoom: 12,
center: { lat: parseFloat(Center_lat) , lng: parseFloat(center_lang) },
// center: { lat: 22.08672 , lng: 79.42444 }
mapTypeControlOptions: {
mapTypeIds: [google.maps.MapTypeId.ROADMAP, customMapTypeId]
}
});
please help me out. Why I am getting this error?
I had the same error.
The cause for the error in my map was that an invalid latitude and/or longitude was being used when creating a polyline like so:
var polylineCoordinates = [
new google.maps.LatLng(37.772323, -122.214897),
new google.maps.LatLng(21.291982, -157.821856),
new google.maps.LatLng(-18.142599, 178.431.1156),
new google.maps.LatLng(-27.46758, 153.027892)
];
var polyline = new google.maps.Polyline({
path: polylineCoordinates,
strokeOpacity: 1.0,
strokeWeight: 2,
map: map
});
Take a look at the above code. Can you see that one of the longitudes, ‘178.431.1156‘, being used isn’t valid? After fixing this and making it a valid set of co-ordinates, the polyline should then begin to appear.

How do I highlight a road on a Google map?

I wish to highlight a Google map of Lincoln, NE. according to regions defined by the Multiple Listing Services (MLS) map of the city. This region map can be found here:
http://www.lincolnrealtors.com/pdf/MLS_Area_Map
I can construct simple polygons for example hightlighting region 31 in the diagram above using:
// Define the LatLng coordinates for the polygon's path.
var Region31 = [
{lat:40.813614, lng: -96.682262},
{lat: 40.813484, lng: -96.644154},
{lat: 40.788145, lng: -96.644154},
{lat: 40.8027, lng: -96.682348},
{lat: 40.813614, lng: -96.682262}
];
But those are only lines when in reality, the MLS regions are delineated in terms of actual highways. For example, region 31 has its lower border along "Normal Blvd" which is not a straight line as you can see by looking at the Google map.
Is there a means of highlighting a particular length of highway in Google Maps and if so, could someone explain this or suggest a reference?
Thanks,
Dominic
You can use the DirectionsService to return polylines that follow the roads.
proof of concept fiddle (I needed to adjust some of your points to avoid the directions service adding extra turns)
code snippet:
var geocoder;
var map;
// Define the LatLng coordinates for the polygon's path.
var Region31 = [{
lat: 40.813436,
lng: -96.682268
}, {
lat: 40.813363,
lng: -96.644167
}, {
lat: 40.788145,
lng: -96.644154
}, {
lat: 40.8027,
lng: -96.682348
}, {
lat: 40.813435,
lng: -96.682267
}];
function initialize() {
var map = new google.maps.Map(
document.getElementById("map_canvas"), {
center: new google.maps.LatLng(37.4419, -122.1419),
zoom: 13,
mapTypeId: google.maps.MapTypeId.ROADMAP
});
var bounds = new google.maps.LatLngBounds();
for (var i = 0; i < Region31.length; i++) {
var marker = new google.maps.Marker({
position: Region31[i],
map: map,
draggable: true
});
bounds.extend(marker.getPosition());
var directionsService = new google.maps.DirectionsService();
directionsService.route({
origin: Region31[i],
destination: Region31[(i + 1) % Region31.length],
travelMode: google.maps.TravelMode.DRIVING
}, function(response, status) {
if (status === google.maps.DirectionsStatus.OK) {
var directionsDisplay = new google.maps.DirectionsRenderer({
map: map,
preserveViewport: true
})
directionsDisplay.setDirections(response);
} else {
window.alert('Directions request failed due to ' + status);
}
});
}
map.fitBounds(bounds);
}
google.maps.event.addDomListener(window, "load", initialize);
html,
body,
#map_canvas {
height: 100%;
width: 100%;
margin: 0px;
padding: 0px
}
<script src="https://maps.googleapis.com/maps/api/js?libraries=geometry,places&key=AIzaSyCkUOdZ5y7hMm0yrcCQoCvLwzdM6M8s5qk"></script>
<div id="map_canvas"></div>
The only api came in my mind is Polyline.

Can you overlay clickable shapes over a google map for display on website

I want to have a google map on my website overlaid with rectangular shapes that can then be clicked onto to open a link. The rectangular shapes relate to buildings, so a larger building would be overlaid with a larger shape.
Clearly the shapes relate to an area of the map so would reduce in size as you zoomed out, and increase as you zoom in.
Is this possible with a google api, Can I do this ?
Yes, you can make clickable shapes with the GoogleMaps API,
You have to define the latitude and longitude points of the shape in an array, like the following:
var StackExchangeNYLatLon = [
{lat: 40.708788, lon: -74.006676},
{lat: 40.708808, lon: -74.006822},
{lat: 40.709009, lon: -74.006582},
{lat: 40.708963, lon: -74.006515}
];
Make a div element for the map to populate,
Then define the map (with the div id and options as parameters into new google.maps.Map):
map = new google.maps.Map(document.getElementById('your-div-id'), {
zoom: 5,
center: {lat: 40.709009, lon: -74.006582}, //This is the one of the provided above
mapTypeId: google.maps.MapTypeId.TERRAIN
});
Then, construct the polygon and set the map:
var StackExchangeNYHeadquarters = new google.maps.Polygon({
paths: StackExchangeNYLatLon,
strokeColor: 'blue',
strokeOpacity: 0.8,
strokeWeight: 3,
fillColor: 'blue',
fillOpacity: 0.35
});
StackExchangeNYHeadquarters.setMap(map);
If you wanted a marker you could do the following code instead:
var marker = new google.maps.Marker({
position: options.latLonData,
map: map,
title: 'My House'
});
Now, add an eventListener to the polygon:
This is the part where you define what happens when you click the shape,
You could make it directly open the link, or open a nice little dialog box with content you can fill.
If you wanted to open a link you could have document.location.href, whereas if you wanted content, you'd need to append a contentString to the infoWindow.
StackExchangeNYHeadquarters.addListener('click', function(event){
document.location.href = 'http://example.com/mylink/';
});
and now, make the infoWindow:
infoWindow = new google.maps.InfoWindow;
All of the above code should be wrapped in a callback handler that is passed along as a query string in the API call:
<script src="https://maps.googleapis.com/maps/api/js?key=YOUR_API_KEY&signed_in=true&callback=startMap"></script>
So the above code should look like:
function startMap(options){
var latLonData = options.latLonData;
var map = new google.maps.Map(document.getElementById('your-div-id'), {
zoom: 5,
center: options.center,
mapTypeId: google.maps.MapTypeId.TERRAIN
});
var polygon = new google.maps.Polygon({
paths: StackExchangeNYLatLon,
strokeColor: 'blue',
strokeOpacity: 0.8,
strokeWeight: 3,
fillColor: 'blue',
fillOpacity: 0.35
});
polygon.setMap(map);
polygon.addListener('click', , function(event){
document.location.href = 'http://example.com/mylink/';
});
var infoWindow = new google.maps.InfoWindow;
}
function StackExchangeHeadquartersBuild(){
options = {
latlonData: [
{lat: 40.708788, lon: -74.006676},
{lat: 40.708808, lon: -74.006822},
{lat: 40.709009, lon: -74.006582},
{lat: 40.708963, lon: -74.006515}
],
center: {lat: 40.709009, lon: -74.006582}
};
startMap(options);
}
You could attach the callback to a factory, so that a lot of elements can be created at once without 500 individual functions for each location.

Google Maps API: Radius circle not drawing

I've followed the instruction given on the Google Maps API site, but my circle is never being drawn to my map, and I don't really understand what I am missing, could anybody point me in the right direction?
Here is my method to add the new address marker and search radius to the map (Note that the marker is added as expected):
// Add the users point to the map
function addAddress() {
// Get the users current location
var location = document.getElementById("P1_LOCATION").value; // Users postcode
var radius_size = document.getElementById("P1_RADIUS").value; // Users radius size in miles
var search_radius;
// Translate the users location onto the map
geocoder.geocode({ 'address': location}, function(results, status) {
if(status == google.maps.GeocoderStatus.OK) {
// Center around the users location
map.setCenter(results[0].geometry.location);
// Place a marker where the user is situated
var marker = new google.maps.Marker({
map:map,
position: results[0].geometry.location
});
// configure the radius
// Construct the radius circle
var radiusOptions = {
strokeColor: "#FF0000",
strokeOpacity: 0.8,
strokeWeight: 2,
fillColor: '#FF0000',
fillOpacity: 0.35,
map: map,
center: marker.center, // I want to set the center around the users location
radius: radius_size // I want the radius to be in miles, how do I do this?
};
// add the radius circle to the map
search_radius = new google.maps.Circle(radiusOptions);
}
});
}
And I'm sure someone will ask if I have configured a base map object, this is done in my initializer method:
var geocoder;
var map;
// Create our base map object
function initialize()
{
geocoder = new google.maps.Geocoder();
var latlng = new google.maps.LatLng(0,0);
var mapOptions = {
zoom: 12,
center: latlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
map = new google.maps.Map(document.getElementById("map"), mapOptions);
}
How would I go about getting the radius to display around the given point? any suggestiosn would be greatly appreciated.
I get this error with the posted code: Uncaught InvalidValueError: setRadius: not a number
But the real issues was this: center: marker.center, should be center: marker.getPosition(), (a google.maps.Marker doesn't have a "center" property)
working code snippet:
// Add the users point to the map
function addAddress() {
// Get the users current location
var location = document.getElementById("P1_LOCATION").value; // Users postcode
var radius_size = parseFloat(document.getElementById("P1_RADIUS").value); // Users radius size in meters (unless you scale it to miles)
var search_radius;
// Translate the users location onto the map
geocoder.geocode({
'address': location
}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
// Center around the users location
map.setCenter(results[0].geometry.location);
// Place a marker where the user is situated
var marker = new google.maps.Marker({
map: map,
position: results[0].geometry.location
});
// configure the radius
// Construct the radius circle
var radiusOptions = {
strokeColor: "#FF0000",
strokeOpacity: 0.8,
strokeWeight: 2,
fillColor: '#FF0000',
fillOpacity: 0.35,
map: map,
center: marker.getPosition(), // I want to set the center around the users location
radius: radius_size // I want the radius to be in miles, how do I do this?
};
// add the radius circle to the map
search_radius = new google.maps.Circle(radiusOptions);
}
});
}
var geocoder;
var map;
// Create our base map object
function initialize() {
geocoder = new google.maps.Geocoder();
var latlng = new google.maps.LatLng(0, 0);
var mapOptions = {
zoom: 12,
center: latlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
map = new google.maps.Map(document.getElementById("map"), mapOptions);
}
google.maps.event.addDomListener(window, "load", initialize);
html,
body,
#map {
height: 500px;
width: 500px;
margin: 0px;
padding: 0px
}
<script src="https://maps.googleapis.com/maps/api/js"></script>
<input id="P1_LOCATION" value="08646" type="text" />
<input id="P1_RADIUS" value="10000" type="text" />
<input id="geocode" value="geocode" type="button" onclick="addAddress()" />
<div id="map" style="width:750px; height:450px; border: 2px solid #3872ac;"></div>
For anybody in future who encounters the same problem, make sure you take the extra step which is not demonstrated in the API guide, and that is to set the Map which the radius object belongs too, in my case:
search_radius.setMap(map);

Creating a polyline for Google driving directions

I reading stuff about the Google API and I wanted to implement the usual Google driving directions that Google maps has for 2 different points. Here's my code so far
(function() {
window.onload = function() {
// Creating a map
var options = {
zoom: 5,
center: new google.maps.LatLng(36.1834, -117.4960),
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById('map'), options);
// Creating an array that will contain the points for the polyline
var route = [
new google.maps.LatLng(37.7671, -122.4206),
new google.maps.LatLng(34.0485, -118.2568)
];
// Creating the polyline object
var polyline = new google.maps.Polyline({
path: route,
strokeColor: "#ff0000",
strokeOpacity: 0.6,
strokeWeight: 5
});
// Adding the polyline to the map
polyline.setMap(map);
};
})();
It has a straight line between the two cities...
Here's an example of using the DirectionsService:
var directionsService = new google.maps.DirectionsService();
var directionsDisplay;
var homeLatlng;
function initialize() {
homeLatlng = new google.maps.LatLng(37.7671, -122.4206);
var myOptions = {
zoom: 10,
center: homeLatlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
var infowindow = new google.maps.InfoWindow({
content: ''
});
directionsDisplay = new google.maps.DirectionsRenderer({
draggable: false,
map: map,
markerOptions: {
draggable: false
},
panel: document.getElementById("directionsPanel"),
infoWindow: infowindow
});
var request = {
origin:homeLatlng,
destination:new google.maps.LatLng(34.0485, -118.2568),
travelMode: google.maps.DirectionsTravelMode[DRIVING],
unitSystem: google.maps.UnitSystem[METRIC]
};
directionsService.route(request, function(result, status) {
if (status == google.maps.DirectionsStatus.OK) {
directionsDisplay.setDirections(result);
}
});
}
If you want to draw a route based on driving directions (or walking directions or transit directions or biking directions), use the Google Directions API. I don't believe the API won't draw the route for you, but it will give you all the lat/lng points that you need to connect in your polyline to show the route.