Google Maps API - Find matching place between two points? - google-maps

I explain my issue in below image (I'm sorry I speak English not well).
I'm using Google Maps Directions API to calculate directions between start point and destination and there is a place between them (you see in the map). How can I know that place is on route?

The only way I can think of (and I have used this method) is thus:
When you make a directions request, you receive a DirectionsResult object.
If you drill down into that result you can get latitudes and longitudes.
If you are using the JavaScript API, for example:
// Every step is about 22 LatLngs per mile; we'll try one LatLng per 3
// miles or so:
var specRoute = directionResult.routes[0].legs[0].steps[i];
for (var j = 0; j < specRoute.path.length; j = j + 171) {
var lattotal = latarr.push(specRoute.path[j].lat());
var lngtotal = lngarr.push(specRoute.path[j].lng());
}
In this example I am formatting arrays of lats and lngs to be compared to a lot of other lats and lngs in a php file elsewhere. It sounds like you only need to check one latlng so it should be simpler.
What I did thereafter (and what you could do) is to determine how close to the path your point is required to be, and write some logic to check. You could do that with straight latitude and longitude numbers, or convert to another distance measurement (miles, meters, etc).
There is a negative aspect in that you will have to deal with converging longitude lines the farther north you go (in the Northern hemisphere).
I used a regression fit workaround but there is probably a better way.
There may also be a better method for doing this, but this is the only way I could think of on past projects.

Related

Incorrect results using Autodesk Forge Viewer Geolocation Extension

This is related to my previous question. I'm posting a new question to try and explain the situation better.
I am placing marker objects on a model using data taken from drone surveys. I have access to high accuracy GPS data and also omega/phi/kappa rotation data.
I am trying to use the Autodesk.Geolocation extension to convert the lon/lat/alt data to viewer space.
All models were originally created in Revit.
When I use the Geolocation extension, it seems like the refPointLMV and GlobalOffset are not correctly being taken into account.
Here's an example:
As you can see, the selected point [0] on the model is nowhere near the real GPS coords. Also, the refPointLMV has huge values.
Something similar happens when I take some lon/lat/alt data from the drone photo. The drone GPS data will be close to the model positionLL84, e.g (4.586577106, 51.626037158, 49.095). However, when I do Geolocation.lonLatToLMV(4.586577106, 51.626037158, 49.095) I get a result way off screen.
We've had a support query open with Autodesk related to this open for over two months now, but not had much success there. They said the engineering team is too busy to work on this and recommended to try and fix the error on our side. Support ref LMV-5261.
I have been able to bring the result of Geolocation.lonLatToLMV into viewer space with the following code:
const gpsPosition = new THREE.Vector3(
longitude,
latitude,
altitude,
);
const position = viewer
.getExtension('Autodesk.Geolocation')
.lonLatToLmv(gpsPosition);
const data = viewer.model.getData();
const globalOffset = data.globalOffset;
const refPointTransform = data.refPointTransform;
// applying the transform
position.add(globalOffset)
position.applyMatrix4(refPointTransform);
// HACK: after applying the above transforms, a final
// rotation of -45 degrees is required to move the points into position
// once this has been done, the locations match up exactly to photos.
// Seems like a weird hack, but I've tested with over 20 drone photos, they all match up.
const quaterion = new THREE.Quaternion().setFromEuler(
new THREE.Euler(0, 0, -Math.PI / 4),
);
position.applyQuaternion(quaterion);
The problem here is that we are testing with a single model and this is clearly not a robust solution that we can expect to work with all future models and drone data we throw at it.
How long is it likely to take for the engineering team to fix this? Or are they likely to fix this at all?
Sorry for the delay due to the Chinese New Year. After checking with our engineering team, the current solution is to do the followings:
Move the Project base point to N0 E0, but remain the angle to true north
Copy the LAT LONG to Survey point
Afterward, the result of the GEO conversion should be expected.
Here are the snapshots of the above setting and the result

google map placing locations with some distance

I'm new to ROR and Google Maps. I need to place some markers from locations in Google Maps (having latitudes and longitudes in a database).
The problem is that I need to select some points with some random distance.
In short, I need to select the location and place it in a map, which must have 100 m distance with each and every points.
If the location is within 100 m range with any other points, it can be neglected. I need to place 10 points from database.
Is there any method?
Assuming that you are needing to find points from your database that are at least 100 meters away from all the other points in the database:
This is a fairly simple problem. It can be visualized as an nxn matrix, with the point set as the rows and columns. In Python, comparing all the distances would look like:
selected = []
for pt1 in pts:
inRange = True
for pt2 in pts:
if pt1.distanceTo(pt2) < 100:
inRange = False
break
if inRange:
selected.append(pt1)
This function iterates through the whole list of points. For each point, it checks the distance from the current point to all the other points. If all the other points are outside 100 meters, it adds the point to an array.
For the distance formula, please see the haversine formula here in code form.
Since you did not specify a language in your question, I will let you translate this into whatever language you need. This is just pseudocode, since not enough details were provided to answer your question with actual code.
Also, if I misunderstood your question, you can adapt this algorithm in some way. It is just to provide some ideas.

PhoneGap Lat Long Distance calculation

I have created a PhoneGap app so it's database is using HTML Storage. I have a table of locations which include their lat and long. I then have the users current location and need to find the closest 3 locations.
How would I go about this mathematically? Will I need to convert the lat long values or can they be directly comparable?
In my opinion the easiest way to do that is convert the distance between the coordinates to kms and then loop through the coordinates you have and figure out which ones you want. It's simple math, you can read more about it here if you want.
getDistanceBetweenCoordinates:function(lat1,lng1,lat2,lng2){
var distance = (3958*3.1415926*Math.sqrt((lat2-lat1)*(lat2-lat1) + Math.cos(lat2/57.29578)*Math.cos(lat1/57.29578)*(lng2-lng1)*(lng2-lng1))/180);
//console.log("distance:" + distance);
return Number(distance);
}

Where can I find city/town location data for location-based searching?

I need to implement find entries with x miles of town function, so I have a town box with autosuggest attached. Currently I'm using Google's Geocoding API to get the data for the autosuggest results, but we obviously can't tune or alter this data and it doesn't always come up with sensible suggestions (even with a country hint). Are there any other sources of town/city location data? I've tried to see if perhaps an export was available from OpenStreetMap or something but I can only find map tiles or vector map data and not POI data for town/city names and locations.
Any suggestions appreciated.
Thanks.
Geonames.org has a downloadable list of cities and even postal codes for many countries around the world along with their corresponding lat/lon point. It's under the Creative Commons Attribution 3.0 License which means that you can use it commercially, but you have to give attribution, among other things.
While having a downloadable list may mean more work on your end in terms of implementing all of the facets of the search algorithm, the silver lining is that you don't couple yourself to the uptime/availability of a 3rd-party web service.
The only other part that's missing is the distance-calculation formula. You can use the greater-circle distance formula to compute distance.
I've done this myself a number of times. The first time you write the code, it takes a little bit to wrap your head around everything, but once you do it's a piece of cake thereafter.
I should probably mention that I'm the founder of SmartyStreets. We do street-based address verification and not only can we tell you if an address is good or not (which helps in a significant number of ways), but we can also give you the lat/lon coordinate for a given street address which can then be plugged in to the above solution to determine proximity to nearby places.
I would say you also have two other options to get the functionality you require.
Firstly, you can use a kludge to focus the results to more meaningful ones (although I would not strongly recommend this method it may suffice for your needs.)
Before your initial query to Google's Geocoding API to get the data for the autosuggest results, firstly determine the country the town is in. Then you can use the country string as a suffix to the Geocoding query.
Something like:
var country = 'UK'; // from initial query, reverse geocode, etc.
geocoder.geocode({
'address': address + ', ' + country
},
See the answer in this question for an example.
Google's Geocoder returns wrong country, ignoring the region hint
Secondly, the premise that you "obviously can't tune or alter this data" is flawed. You can certainly filter the results based on the country. Something like the following snippet shows how to drill down the results.
geocoder.geocode({
'address': address
},
function(results, status) {
if(status == google.maps.GeocoderStatus.OK) {
for(var i = 0, l = results.length; i < l; i++) {
for (var j = 0, l2 = results[i].address_components.length; j < l2; j++) {
for (var k = 0, l3 = results[i].address_components[j].types.length; k < l3; k++) {
if(results[i].address_components[j].types[k]=="country") {
var country = results[i].address_components[j].long_name;
// do stuff based on the country
// add the result to your auto-suggest, etc...
}
}
}
}
});
}
Finally, if you implemented a CGI wrapper to do the geocoding you could cache the results. This way your application would automatically build your database for you. This would save actually having to perform geocoding at all for known results. I think this is almost what you are hinting at in your question, and there is no reason you could not pre-populate your cache with known results if you did find a reliable source of data.
Take a look at this document that outlines various Geocoding Strategies using the Maps Api - it discusses things like caching, etc.
https://developers.google.com/maps/articles/geocodestrat
EDIT
You could use something like geoPlugin
http://www.geoplugin.com/webservices/php#php_class
Take a look at the nearby places features, this does exactly what you want.
http://www.geoplugin.com/webservices/extras
Check out http://geohash.org/, this site working with many api's that you may want to use. See also the article about the history of this site

convert meters to latitude longitude from any point

In my project I have to find [latitude, longitude] coordinate(s) from one point in distance of 500 meters (this could be any random coordinate or an array of coordinates around my point). How can I do this?
Note: I need this in order to find multiple paths between points different from shortest one which is returned us via Google Maps Directions Api..So using my method I will define the center of the road from A to B and then find some coordinates below and above that center position and use this as another waypoint to go from A to B - I guess this might help me to find multiple paths...
Any suggestions from GIS professionals?
EDIT: UTM conversion is the most preferable one for such calculations, and I've created UTM Java class if anyone needs..
If I understand your question right you have a known point in Lat/Long and you need calculate the Lat/Long of another point or points 500m away from your starting point.
If this is what you are doing, you have several options most of which involve specialist GIS APIs. However, I'm guesing you're a programmer/mathematician rather than a Geographer so, you may prefer to opt for using the Haversine formula. You can find a discussion on this topic here plus the formula.
One caveat is that the distamce you are working with (500m is quite small) and the Earth is far from being a perfect sphere or even a slightly flattened spheroid. It is locally "lumpy" and that can put your calculation out. If you need more accuracy you will have to account for these imperfections by using an appropriate local Datum (model of the Earth - there are many e.g. see EPSG list) and to do that you will probably need to start using the GIS libraries as the maths gets very detailed otherwise.
This is the code used by google map (SphericalUtil.java)
// from SphericalUtil.java
// compile 'com.google.maps.android:android-maps-utils:0.4.4'
public static LatLng computeOffset(LatLng from, double distance, double heading) {
distance /= 6371009.0D; //earth_radius = 6371009 # in meters
heading = Math.toRadians(heading);
double fromLat = Math.toRadians(from.latitude);
double fromLng = Math.toRadians(from.longitude);
double cosDistance = Math.cos(distance);
double sinDistance = Math.sin(distance);
double sinFromLat = Math.sin(fromLat);
double cosFromLat = Math.cos(fromLat);
double sinLat = cosDistance * sinFromLat + sinDistance * cosFromLat * Math.cos(heading);
double dLng = Math.atan2(sinDistance * cosFromLat * Math.sin(heading), cosDistance - sinFromLat * sinLat);
return new LatLng(Math.toDegrees(Math.asin(sinLat)), Math.toDegrees(fromLng + dLng));
}
to use it, you just have to enter the centerLatLng, the distance in meters, and the heading in degrees from centerLatLng.
you can change the formula to the language of your preference.