Aditi
Aditi

Reputation: 31

google map api to find hospital within 10km

I have used google map api for my php project just to point markers. Now I am building a android app. I have a list of hospitals of my state with their longitude and latitude in my own mysql database. I want to build the app like that it will fetch the coords from the gps and search my database for hospitals within 10km radius. I have seen API like

maps.googleapis.com/maps/api/place/search/output?parameters

But here I want to use my own database. Is there any way out ?

Thanks in advance

Upvotes: 1

Views: 2472

Answers (2)

Eduardo Almeida
Eduardo Almeida

Reputation: 171

If your problem is finding the hospitals that are near, you can use this code:

private boolean isItemInRange(double radius,
        double latOrigin, double lonOrigin, double latDestination,
        double lonDestination) {

    double resultRadius = 6371 * Math.acos(Math.cos(Math.PI
            * (90 - latDestination) / 180)
            * Math.cos((90 - latOrigin) * Math.PI / 180)
            + Math.sin((90 - latDestination) * Math.PI / 180)
            * Math.sin((90 - latOrigin) * Math.PI / 180)
            * Math.cos((lonOrigin - lonDestination) * Math.PI / 180));

    if (resultRadius <= radius){
        return true;
    } else {
        return false;
    }
}

Just pass the coordinates of the device and the coordinates of each hospital. If the data is on the server, just send the device coordinates and you can implement the function above in your server or in the database.

Hope it can help you.

Upvotes: 0

La bla bla
La bla bla

Reputation: 8708

I did something quite similar and had that issue too.

I received this answer https://stackoverflow.com/a/10771017/975959

Basically, you can write a php file to interact with you database (a better solution than accessing directly via Android) and let the MySQL do the necessary calculations and only return the one which are closer than 10 Km.

Good luck

Upvotes: 0

Related Questions