google.maps.geometry.spherical.computeOffset error (a.lat is not a function) - google-maps-api-3

I wanted a latlng value near a particular distance. I went throught the documentation of google maps api v3.
Have a look at my code :
generateCirclepolygon(){
for (let location of this.pointsLocation) {
let LatLng:any = {
lat: parseFloat(location.lat),
lng: parseFloat(location.lng)
};
let newpoints = [];
var polypoint = google.maps.geometry.spherical.computeOffset(LatLng , 3000 , 0)
console.log(polypoint , 'point');
}
}
After runing this code im getting ERROR TypeError: a.lat is not a function.
I'm sending a LatLng object to that which has lat and lng values and they are numbers. 3000 is the distance in meters and 0 is heading (i.e 0 degrees in clock wise from north).
I want the latlng value from my point to 3000 meters towards north. My main aim is to draw a circular polygon around a point by repeating this to get an array of points.

Related questions:
Google maps a.lat is not a function
Google Maps API a.lat is not a function error
Per the documentation, the computeOffset method takes a google.maps.LatLng as an argument:
computeOffset(from, distance, heading[, radius])
Parameters:
from: LatLng
distance: number
heading: number
radius (optional): number
Return Value: LatLng
Returns the LatLng resulting from moving a distance from an origin in the specified heading (expressed in degrees clockwise from north).
You are providing a LatLngLiteral (which doesn't have a .lat method, it has a .lat property.
let LatLng:any = {
lat: parseFloat(location.lat),
lng: parseFloat(location.lng)
};
should be:
let LatLng:any = new google.maps.LatLng(
parseFloat(location.lat),
parseFloat(location.lng)
);

Related

How to increase resolution of exported image in Google Earth Engine?

I have some code to export an LS8 image to Drive using GEE. Somehow, the resolution of the image seems to be lower (larger pixels) than what I am able to see on the browser. How can I increase the resolution? This is the code I´ve been using, with two different options.
I attempted to use .resample() as a solution but it produced a single band image that did not look good.
geometry2 = /* color: #57d64b */ee.Geometry.Polygon(
[[[-78.2812086867645, 2.3386717366200585],
[-77.56984394067075, 2.3729749945579948],
[-77.72227924340513, 2.776314152654142],
[-78.20842426293638, 2.725560942159387]]]);
function maskL8sr(image)
{
// Bits 3 and 5 are cloud shadow and cloud, respectively.
var cloudShadowBitMask = ee.Number(2).pow(3).int();
var cloudsBitMask = ee.Number(2).pow(5).int();
// Get the pixel QA band.
var qa = image.select('pixel_qa');
// Both flags should be set to zero, indicating clear conditions.
var mask = qa.bitwiseAnd(cloudShadowBitMask).eq(0)
.and(qa.bitwiseAnd(cloudsBitMask).eq(0));
// Return the masked image, scaled to TOA reflectance, without the QA bands.
return image.updateMask(mask).divide(10000)
.select("B[0-9]*")
.copyProperties(image, ["system:time_start"]);
}
// Map the function over one year of data.
var collection = ee.ImageCollection('LANDSAT/LC08/C01/T1_SR')
.filterDate('2016-04-01', '2018-7-31')
.map(maskL8sr)
var composite = collection.median();
var VIS = {bands: ['B5', 'B6', 'B4'], min: 0, max: 0.47};
// Display the results.
Map.addLayer(composite,VIS ,"compt2");
var params ={
crs: 'EPSG:4326',
maxPixels: 1e12,
region:geometry2
}
Export.image(composite,'Guapi',params);
Export.image.toDrive({
image: composite,
description: 'Guapi',
scale: 30,
region: geometry2,
maxPixels: 1000000000
});

Google Maps JS API Fit Bounds Not Working

I'm having a very strange problem with my Google Maps implementation. I am getting back an array from a REST service that gives me lat/lng and I am in turn using that information to put markers on the map and implement functionality.
That part works fine, it's when it comes to taking the markers output on the page and getting their bounds and zooming the map to them that I get to the problem. I have read several solutions and I have attempted implementing them to the best of my ability.
Here's my implementation:
// Reset the maps bounds
MapView.bounds = new google.maps.LatLngBounds(null);
// Get the new map bounds
for (var i=0, j=MapView.markers.length; i<j; i++){
var marker = MapView.markers[i],
lat = marker.position.lat(),
lng = marker.position.lng(),
latlng = new google.maps.LatLng(lat, lng);
MapView.bounds.extend(latlng);
};
// Set the map bounds to the new bounds
MapView.map.fitBounds(MapView.bounds);
Here is a list of the coordinates being used:
21.245445,-105.167631
41.887668,-87.622522
49.817492,15.472962
50.075538,14.4378
33.951611,-118.387578
41.887668,-87.622522
14.782827,-90.793702
33.741973,-78.817013
45.922225,-95.408973
28.320306,-81.422964
36.166667,-86.783333
-17.82922,31.053961
-8.708704,115.169616
41.901514,12.460774
-34.013717,23.054811
20.483443,-86.971039
34.933333,34.083333
6.428055,-9.429499
38.79142,-95.960607
43.771033,11.248001
46.271588,13.95641
33.773,-78.779504
40.789342,-3.249749
20.926822,-156.695125
46.271588,13.95641
18.853921,-71.300939
36.462205,-5.011611
25.788969,-80.226439
50.36809,8.73632
37.540667,126.948346
45.495992,-121.5879
14.782827,-90.793701
0,0
13,-76
20.431006,-86.908065
40.75694,-73.984872
64.143935,-21.934099
-17.816667,25.15
12.879721,121.774017
32.640054,-117.084196
-16.522046,28.850942
40.280559,22.50584
39.202686,-106.831683
36.122611,-115.170973
38.79142,-95.960607
18.126285,-65.440099
22.876396,-109.918562
30.36884,-86.324846
36.076518,-115.153343
36.0443,14.251222
41.894809,-87.624214
36.0443,14.251222
-34.035086,23.046469
36.42,25.431667
-17.82922,31.053961
20.696686,-105.292631
18.533333,-68.366667
42.407211,-71.382437
21.158964,-86.845937
9.748917,-83.753428
14.782827,-90.793701
34.852965,32.361479
34.939737,32.461585
34.052234,-118.243685
64.143935,-21.934099
35.369598,24.482727
52.407927,3.222711
47.497912,19.040235
21.158964,-86.845937
37.446719,25.328862
21.160386,-86.843338
39.770247,21.182861
36.124253,-115.168476
46.421684,15.856075
116.468401,39.947856
41.553221,-70.608589
43.706449,7.292265
39.415044,21.737618
21.158964,-86.845937
20.629559,-87.126904
36.832012,25.897065
50.075538,14.4378
8.87509,98.352656
57.702051,11.982304
-24.183889,29.012778
38.904253,-77.047904
0,0
-25.360413,27.09947
55.940209,-3.225319
36.286023,-5.27918
-3.386069,39.971999
20.749045,-105.31098
17.280151,-62.689038
21.040195,-104.358146
14.782827,-90.793702
20.696686,-105.292631
50.071287,14.397221
51.891877,-8.493827
Then I used those bounds to create a poly line to try and find out where the problem was, and the wierd part is even that is wrong, but the map isn't even zooming/panning to THOSE bounds.
Code for poly line:
var ne = MapView.bounds.getNorthEast();
var sw = MapView.bounds.getSouthWest();
var boundingBoxPoints = [
ne, new google.maps.LatLng(ne.lat(), sw.lng()),
sw, new google.maps.LatLng(sw.lat(), ne.lng()), ne
];
var boundingBox = new google.maps.Polyline({
path: boundingBoxPoints,
strokeColor: '#FF0000',
strokeOpacity: 1.0,
strokeWeight: 2
});
boundingBox.setMap(MapView.map);
And a picture of the result (this is as the map loads, un-altered):
It will either load like that (if I'm lucky) or load with most of the trips out of the top of the map with only a couple showing and most of the map canvas gray.
I am a total loss right now, could somebody please point me in the right direction?

Plot Marker on Map which are apart from 30 km

I am very new to Google Map API v3. I have referred google developer's guide on this but i didn't get proper conclusion on this.
I have list of addresses.
I want to send those as one(source) - Many addresses(Destination) to the google map and i want to plot the addresses which are 30 km apart from the source address on the map.
Is there any possible way to achieve this with google map??
Can any one be suggested on this..
One of the things you're looking for is the computeDistanceBetween() function in the geometry library (reference). This gives you the distance in meters between two LatLng objects. With that you can do whatever test you need - not clear if you meant "less than 30km" or "more than 30km" or "about 30km". But given a way to calculate the distance you can do whatever test you need as you loop through your markers.
Here's an example. Suppose you have some data like this - could be from a JSON object, or whatever:
var data = {
source: { lat: n, lng: n },
places: [
{ lat: n, lng: n, name: str },
{ lat: n, lng: n, name: str },
...
]
};
After you initialize your map, you could use some code like this to loop through data.places and create only the markers that meet your distance criterion. In this example we'll create markers that are within 30km of the source:
var sourceLatLng =
new google.maps.LatLng( data.source.lat, data.source.lng );
for( var i = 0; i < data.places.length; ++i ) {
addPlace( data.places[i] );
}
function addPlace( place ) {
var placeLatLng = new google.maps.LatLng( place.lat, place.lng );
var distance =
google.maps.geometry.spherical.computeDistanceBetween(
placeLatLng,
sourceLatLng
);
if( distance > 30000 )
return;
var marker = new google.maps.Marker({
map: map,
position: placeLatLng
});
}
You need to make sure to load the geometry library, by adding the libraries=geometry parameter when you load the Maps API, e.g.:
<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false&libraries=geometry">
</script>

Calculating a nearby street to use on streetview

I am facing an unusual problem where I need to calculate the latlong of a position on a map that has streetview imagery, without knowing the actual position.
I know the end destination of my user, but I need to calculate the latlong of a nearby position (approximately 1km away or less, this should be variable) that has streetview imagery and use that as the start destination.
An example would be that I know I need to go to Times Square, but I want to have a start destination that is about 1km away by road. I then need to verify that there is street view imagery for this co-ordinate before I decide that it's the starting point.
The function below recursively doubles the search distance, (up to a maximum of 10000 meters), until a panorama is found.
Sample code:
// Global vars
var G = google.maps;
var streetViewService = new G.StreetViewService();
function getNearSVP(latlon,maxDist) {
streetViewService.getPanoramaByLocation(latlon, maxDist, function(data, status) {
if (status == google.maps.StreetViewStatus.OK) {
return data.location.latLng;
}
else{
if (maxDist < 10000){
maxDist = maxDist * 2;
return getNearSVP(latlon, maxDist);
}
else {
alert('StreetView is not available within '+maxDist+' meters of this location');
}
}
});
}
Live demo here

Draw Route X Kilometers from Origin

Running/walking distance display.
User enters a location and a distance.
I can overlay a circle with a radius of the distance the user entered, with the user's location as the center point.
I can set four cardinal points (N, S, E, W) around the point of origin at the distance the user set and draw the routes to those points, such that point B is 100KM from point A, but the mapped route is, say, 145km along the road.
Is it possible to display a route along the road exactly 100km?
Edited to update progress.
Finally solved this and thought I'd share.
so, the user supplies a location and a distance; we'll say 100Km.
The code finds cardinal points 100Km N, S, E, W of the point of origin, then solves routes to each point. If solving for the route is successful, the result contains an array of points from the point of origin to the destination.
directionsService.route({
origin: start,
destination: end,
travelMode: google.maps.DirectionsTravelMode.DRIVING
}, function(result) {
renderDirections(result);
});
//don't use the google api DirectionsRender() to draw the route.
//instead - result holds an array of lat/long points that make up the entire route. Lets say it's latlong[123]
//iterate through that array, getting a distance from point A to latlong[0], A to latlong[1], etc...
//when that distance is >= user supplied distance, STOP, and draw a polyline from point A through the latlong points in the array latlong[78]
function computeTotalDistance(result) {
var total = 0;
var myroute = result.routes[0];
if(myroute)
{
//create a LatLon from the Starting point
var objGeo = new LatLon(Geo.parseDMS(myroute.overview_path[0].Qa), Geo.parseDMS(myroute.overview_path[0].Ra));
//call get distance from the starting point to each other point in the array
//each subsequent point should be a longer distance
var arrPointsToDraw =[];
for(var i = 1; i<=myroute.overview_path.length-1;i++)
{
try
{
var objGeo2 = new LatLon(Geo.parseDMS(myroute.overview_path[i].Qa), Geo.parseDMS(myroute.overview_path[i].Ra));
}
catch(err)
{
alert(err.description);
}
//here, total = kilometers
total = objGeo.distanceTo(objGeo2,3);
//add coordinates to our array of points that we are going to draw
arrPointsToDraw.push(new google.maps.LatLng(objGeo2._lat, objGeo2._lon));
if(parseInt(total) > parseInt(distance.value))
{
arrPointsToDraw.pop();//remove the last element of the array
break;
}
}
//at this point, arrPointsToDraw[] contains the lat/long points that are closest to our distance
//without going over
lines[lines.length] = new google.maps.Polyline({
path: arrPointsToDraw,
strokeColor: '#1589FF',
strokeOpacity: 1.0,
strokeWeight: 3
});
lines[lines.length-1].setMap(map);
}//end if(myRoute)
}
This code makes use of two fantastic collections of functions found here

Resources