Find the nearest index of polyline to the given location











up vote
0
down vote

favorite












How can I get the nearest point from my location in a polyline? I have used the below code to do my job



click here to view code



but it gives me output something like this:



List<LatLng> points = new ArrayList<>();
points.add(new LatLng(2, 2));
points.add(new LatLng(4, 2));
points.add(new LatLng(4, 4));
points.add(new LatLng(2, 4));
points.add(new LatLng(2, 2));

LatLng testPoint = new LatLng(3, 0);

LatLng nearestPoint = findNearestPoint(testPoint, points);
Log.e("NEAREST POINT: ", "" + nearestPoint); // lat/lng: (3.0,2.0)


But I want the index of the polyline (ArrayList containing a list of Polyline) which is closest to the given point, I have tried many methods but none of them worked out.



Please let me know if you got the solution.










share|improve this question
























  • That function findNearestPoint is finding a point somewhere on the line segments specified in the list closest to the specified point - it is not necessarily finding the closest vertex (or the points in your list)..
    – Andy
    Nov 7 at 17:10












  • Take a look at stackoverflow.com/questions/36104809/…
    – antonio
    Nov 7 at 20:06










  • @antonio Hi, I used your code to get the closest polyline index of the given point but as Andy said it is finding a point somewhere in the line segment. So, How can I get the index of polyline list ?
    – Adarsh
    Nov 9 at 3:57










  • @Adarsh Don't use that function - just loop on your points, use SphericalUtil.computeDistanceBetween and find the minimum distance and that is your point/index. For long lists you'll have to consider optimization.
    – Andy
    Nov 10 at 15:20










  • @Andy ok thanks for your answer anyhow I have used this code github.com/googlemaps/android-maps-utils/blob/master/library/… to get the index and it's working perfectly
    – Adarsh
    Nov 11 at 7:13















up vote
0
down vote

favorite












How can I get the nearest point from my location in a polyline? I have used the below code to do my job



click here to view code



but it gives me output something like this:



List<LatLng> points = new ArrayList<>();
points.add(new LatLng(2, 2));
points.add(new LatLng(4, 2));
points.add(new LatLng(4, 4));
points.add(new LatLng(2, 4));
points.add(new LatLng(2, 2));

LatLng testPoint = new LatLng(3, 0);

LatLng nearestPoint = findNearestPoint(testPoint, points);
Log.e("NEAREST POINT: ", "" + nearestPoint); // lat/lng: (3.0,2.0)


But I want the index of the polyline (ArrayList containing a list of Polyline) which is closest to the given point, I have tried many methods but none of them worked out.



Please let me know if you got the solution.










share|improve this question
























  • That function findNearestPoint is finding a point somewhere on the line segments specified in the list closest to the specified point - it is not necessarily finding the closest vertex (or the points in your list)..
    – Andy
    Nov 7 at 17:10












  • Take a look at stackoverflow.com/questions/36104809/…
    – antonio
    Nov 7 at 20:06










  • @antonio Hi, I used your code to get the closest polyline index of the given point but as Andy said it is finding a point somewhere in the line segment. So, How can I get the index of polyline list ?
    – Adarsh
    Nov 9 at 3:57










  • @Adarsh Don't use that function - just loop on your points, use SphericalUtil.computeDistanceBetween and find the minimum distance and that is your point/index. For long lists you'll have to consider optimization.
    – Andy
    Nov 10 at 15:20










  • @Andy ok thanks for your answer anyhow I have used this code github.com/googlemaps/android-maps-utils/blob/master/library/… to get the index and it's working perfectly
    – Adarsh
    Nov 11 at 7:13













up vote
0
down vote

favorite









up vote
0
down vote

favorite











How can I get the nearest point from my location in a polyline? I have used the below code to do my job



click here to view code



but it gives me output something like this:



List<LatLng> points = new ArrayList<>();
points.add(new LatLng(2, 2));
points.add(new LatLng(4, 2));
points.add(new LatLng(4, 4));
points.add(new LatLng(2, 4));
points.add(new LatLng(2, 2));

LatLng testPoint = new LatLng(3, 0);

LatLng nearestPoint = findNearestPoint(testPoint, points);
Log.e("NEAREST POINT: ", "" + nearestPoint); // lat/lng: (3.0,2.0)


But I want the index of the polyline (ArrayList containing a list of Polyline) which is closest to the given point, I have tried many methods but none of them worked out.



Please let me know if you got the solution.










share|improve this question















How can I get the nearest point from my location in a polyline? I have used the below code to do my job



click here to view code



but it gives me output something like this:



List<LatLng> points = new ArrayList<>();
points.add(new LatLng(2, 2));
points.add(new LatLng(4, 2));
points.add(new LatLng(4, 4));
points.add(new LatLng(2, 4));
points.add(new LatLng(2, 2));

LatLng testPoint = new LatLng(3, 0);

LatLng nearestPoint = findNearestPoint(testPoint, points);
Log.e("NEAREST POINT: ", "" + nearestPoint); // lat/lng: (3.0,2.0)


But I want the index of the polyline (ArrayList containing a list of Polyline) which is closest to the given point, I have tried many methods but none of them worked out.



Please let me know if you got the solution.







android google-maps maps polyline






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 9 at 10:16









MrUpsidown

14.5k74893




14.5k74893










asked Nov 7 at 12:19









Adarsh

22




22












  • That function findNearestPoint is finding a point somewhere on the line segments specified in the list closest to the specified point - it is not necessarily finding the closest vertex (or the points in your list)..
    – Andy
    Nov 7 at 17:10












  • Take a look at stackoverflow.com/questions/36104809/…
    – antonio
    Nov 7 at 20:06










  • @antonio Hi, I used your code to get the closest polyline index of the given point but as Andy said it is finding a point somewhere in the line segment. So, How can I get the index of polyline list ?
    – Adarsh
    Nov 9 at 3:57










  • @Adarsh Don't use that function - just loop on your points, use SphericalUtil.computeDistanceBetween and find the minimum distance and that is your point/index. For long lists you'll have to consider optimization.
    – Andy
    Nov 10 at 15:20










  • @Andy ok thanks for your answer anyhow I have used this code github.com/googlemaps/android-maps-utils/blob/master/library/… to get the index and it's working perfectly
    – Adarsh
    Nov 11 at 7:13


















  • That function findNearestPoint is finding a point somewhere on the line segments specified in the list closest to the specified point - it is not necessarily finding the closest vertex (or the points in your list)..
    – Andy
    Nov 7 at 17:10












  • Take a look at stackoverflow.com/questions/36104809/…
    – antonio
    Nov 7 at 20:06










  • @antonio Hi, I used your code to get the closest polyline index of the given point but as Andy said it is finding a point somewhere in the line segment. So, How can I get the index of polyline list ?
    – Adarsh
    Nov 9 at 3:57










  • @Adarsh Don't use that function - just loop on your points, use SphericalUtil.computeDistanceBetween and find the minimum distance and that is your point/index. For long lists you'll have to consider optimization.
    – Andy
    Nov 10 at 15:20










  • @Andy ok thanks for your answer anyhow I have used this code github.com/googlemaps/android-maps-utils/blob/master/library/… to get the index and it's working perfectly
    – Adarsh
    Nov 11 at 7:13
















That function findNearestPoint is finding a point somewhere on the line segments specified in the list closest to the specified point - it is not necessarily finding the closest vertex (or the points in your list)..
– Andy
Nov 7 at 17:10






That function findNearestPoint is finding a point somewhere on the line segments specified in the list closest to the specified point - it is not necessarily finding the closest vertex (or the points in your list)..
– Andy
Nov 7 at 17:10














Take a look at stackoverflow.com/questions/36104809/…
– antonio
Nov 7 at 20:06




Take a look at stackoverflow.com/questions/36104809/…
– antonio
Nov 7 at 20:06












@antonio Hi, I used your code to get the closest polyline index of the given point but as Andy said it is finding a point somewhere in the line segment. So, How can I get the index of polyline list ?
– Adarsh
Nov 9 at 3:57




@antonio Hi, I used your code to get the closest polyline index of the given point but as Andy said it is finding a point somewhere in the line segment. So, How can I get the index of polyline list ?
– Adarsh
Nov 9 at 3:57












@Adarsh Don't use that function - just loop on your points, use SphericalUtil.computeDistanceBetween and find the minimum distance and that is your point/index. For long lists you'll have to consider optimization.
– Andy
Nov 10 at 15:20




@Adarsh Don't use that function - just loop on your points, use SphericalUtil.computeDistanceBetween and find the minimum distance and that is your point/index. For long lists you'll have to consider optimization.
– Andy
Nov 10 at 15:20












@Andy ok thanks for your answer anyhow I have used this code github.com/googlemaps/android-maps-utils/blob/master/library/… to get the index and it's working perfectly
– Adarsh
Nov 11 at 7:13




@Andy ok thanks for your answer anyhow I have used this code github.com/googlemaps/android-maps-utils/blob/master/library/… to get the index and it's working perfectly
– Adarsh
Nov 11 at 7:13

















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53189359%2ffind-the-nearest-index-of-polyline-to-the-given-location%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















 

draft saved


draft discarded



















































 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53189359%2ffind-the-nearest-index-of-polyline-to-the-given-location%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

Hercules Kyvelos

Tangent Lines Diagram Along Smooth Curve

Yusuf al-Mu'taman ibn Hud