How do I highlight a road on a Google map? - google-maps

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.

Related

Google Map Coordinates conversion

I have coordinates with unknown form
for example : X : 187341.9208 , Y : 665722.3982 ,
What projection are these coordinates in?
The main purpose is to convert those coordinates to Lat and Lng form that will work in Google Maps.
The source of those coordinates are unknown to me, it's a city in Israel. According to Google coordinates to this city is lat: 32.096 , lng: 34.886
Looks like those coordinates are in the Israeli Transverse Mercator Projection
https://epsg.io/transform#s_srs=2039&t_srs=4326&x=187341.9208000&y=665722.3982000
gives {lat: 32.0844147, lng: 34.8643428}, seems pretty close.
function initMap() {
var myLatLng = {
lat: 32.0844147,
lng: 34.8643428
};
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 15,
center: myLatLng
});
var marker = new google.maps.Marker({
position: myLatLng,
map: map,
title: '187341.9208,665722.3982'
});
}
html,
body,
#map {
height: 100%;
margin: 0;
padding: 0;
}
<div id="map"></div>
<!-- Replace the value of the key parameter with your own API key. -->
<script async defer src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCkUOdZ5y7hMm0yrcCQoCvLwzdM6M8s5qk&callback=initMap">
</script>

How to build this product with google maps?

I want to build an exact replica of a website with a different landmark to point towards.
Could you please tell me what I should learn in order to do it please ?
So far, I've created this and don't know what to do next :
https://jsfiddle.net/hasnain721/01v7s2m4/6/
I am a very basic coder btw!
Thanks in advance!
var map = new google.maps.Map(document.getElementById("map"), {
center: new google.maps.LatLng(53.3478, -6.2597),
zoom: 16,
mapTypeId: google.maps.MapTypeId.ROADMAP
});
var infoWindow = new google.maps.InfoWindow({
map: map
});
// Try HTML5 geolocation.
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(position) {
var pos = {
lat: position.coords.latitude,
lng: position.coords.longitude
};
//infoWindow.setPosition(pos);
// infoWindow.setContent('<b>You are here.</b><br><b>Lat:</b> '+position.coords.latitude+'<br><b>Lon:</b> '+position.coords.longitude);
map.setCenter(pos);
var marker = new google.maps.Marker({
position: pos,
map: map,
title: String(pos.lat) + ", " + String(pos.lng),
});
//draws out the path from current location to landmark
var flightPlanCoordinates = [{
lat: position.coords.latitude,
lng: position.coords.longitude
},
{
lat: 21.4224779,
lng: 39.8251832
}
];
var flightPath = new google.maps.Polyline({
path: flightPlanCoordinates,
geodesic: true,
strokeColor: '#FF0000',
strokeOpacity: 1.0,
strokeWeight: 2
});
flightPath.setMap(map);
//draws out the path from current location to landmark
},
function() {
handleLocationError(true, infoWindow, map.getCenter());
});
} else {
// Browser doesn't support Geolocation
handleLocationError(false, infoWindow, map.getCenter());
}
You need to change the coordinates of the "flightPlanCoordinates" variable.
sample pointing at the Statue of Liberty :
var flightPlanCoordinates = [
{lat: position.coords.latitude, lng: position.coords.longitude},
{lat: 40.689249, lng: -74.0445}
];

Get Road coordination From google api using geocode

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

Google Maps API LatLng NaN

I'm struggling with the Google Maps JavaScript API v3. I need to create a new LatLng object with some specific coordinates. According to the API this one should simply do the trick:
myLatLng = new google.maps.LatLng({lat: -34, lng: 151});
Currently doing so simply creates the desired object but with values representing NaN. So no matter what I'm doing, I can't create a proper LatLng object and even the docs aren't helping me much.
The google.maps.LatLng constructor takes two numbers as arguments, not a google.maps.LatLngLiteral.
myLatLng = new google.maps.LatLng({lat: -34, lng: 151});
should be:
myLatLng = new google.maps.LatLng(-34, 151);
In many cases you can use a google.maps.LatLngLiteral instead of a google.maps.LatLng.
myLatLng = {lat: -34, lng: 151};
code snippet:
var geocoder;
var map;
function initialize() {
var myLatLng = new google.maps.LatLng(-34, 151);
var myLatLng2 = {
lat: -34.05,
lng: 151
};
var map = new google.maps.Map(
document.getElementById("map_canvas"), {
center: myLatLng,
zoom: 11,
mapTypeId: google.maps.MapTypeId.ROADMAP
});
var marker = new google.maps.Marker({
position: myLatLng2,
map: map
})
}
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"></script>
<div id="map_canvas"></div>

How to determine if a point K(lat,lng) is on a simple polyline from A(lat,lng) to B(lat,lng) in Google Map [duplicate]

This question already has an answer here:
Detect if google map marker is placed on route
(1 answer)
Closed 7 years ago.
I have a polyline in google maps from point A(lat,lng) to B(lat,lng). If someone gives me a point K(lat,lng) and say that it is on the polyline from A (lat,lng) to B(lat,lng) or B(lat,lng) to A(lat,lng) then how can I determine it is true? I am thinking about something as the following :
if A.lat <= K.lat AND B.lat >= K.lat OR B.lat <= K.lat AND A.lat >= K.lat then
K is on the line
else
K is not on the line
Should this logic work? I am asking because in my test, mostly it worked but some rare cases it did not work even though the point was actually taken from the line.
NB : Using Google Map Javascrpt API
See the documenation for google.maps.geometry.poly.isLocationOnEdge:
isLocationOnEdge(point:LatLng, poly:Polygon|Polyline, tolerance?:number)
boolean
Computes whether the given point lies on or near to a polyline, or the edge of a polygon, within a specified tolerance. Returns true when the difference between the latitude and longitude of the supplied point, and the closest point on the edge, is less than the tolerance. The tolerance defaults to 10-9 degrees.
proof of concept fiddle
code snippet:
var map;
var infowindow = new google.maps.InfoWindow();
function initialize() {
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,
clickable: false,
strokeColor: '#FF0000',
strokeOpacity: 1.0,
strokeWeight: 2
});
flightPath.setMap(map);
google.maps.event.addListener(map,'click',function(e) {
var marker = new google.maps.Marker({
position: e.latLng,
map: map,
draggable: true
});
var contentStr;
if (google.maps.geometry.poly.isLocationOnEdge(marker.getPosition(), flightPath, 1e-5)) {
contentStr = "on line";
} else {
contentStr = "not on line";
}
marker.contentStr = contentStr;
google.maps.event.addListener(marker,'click',function(e) {
infowindow.setContent(this.contentStr+"<br>"+this.getPosition().toUrlValue(6)+"<br><a href='javascript:map.setCenter(new google.maps.LatLng("+this.getPosition().toUrlValue()+"));map.setZoom(19);'>Zoom In</a>");
infowindow.open(map,this);
});
google.maps.event.addListener(marker, 'dragend', function() {
if (google.maps.geometry.poly.isLocationOnEdge(this.getPosition(), flightPath, 1e-5)) {
contentStr = "on line";
} else {
contentStr = "not on line";
}
this.contentStr = contentStr;
});
google.maps.event.trigger(marker,'click');
});
}
google.maps.event.addDomListener(window, "load", initialize);
html, body, #map {
height: 100%;
width: 100%;
margin: 0px;
padding: 0px
}
<script src="https://maps.googleapis.com/maps/api/js?libraries=geometry"></script>
<div id="map" style="border: 2px solid #3872ac;"></div>