最接近android的地方

时间:2011-04-20 23:38:44

标签: android

我的目标是在最近的位置搜索我当前的位置。

我有一个XML文件内容与她的经度和latutude cordinates不同的地方。

我创建了一个代码来确定我当前的位置

    package com.geoo;

import android.app.Activity;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.Toast;

public class localisation extends ActivityBase{

/** Called when the activity is first created. */

@Override

public void onCreate(Bundle savedInstanceState){

super.onCreate(savedInstanceState);
setContentView(R.layout.localisation);

/* Use the LocationManager class to obtain GPS locations */

LocationManager mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);

LocationListener mlocListener = new MyLocationListener();

mlocManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 0, 0, mlocListener);

}

/* Class My Location Listener */

public class MyLocationListener implements LocationListener

{

@Override

public void onLocationChanged(Location loc)

{

loc.getLatitude();

loc.getLongitude();

String Text = "My current location is: " + "Latitud = " + loc.getLatitude()+  "Longitud = " + loc.getLongitude();
Toast.makeText( getApplicationContext(),Text, Toast.LENGTH_SHORT).show();
}

@Override

public void onProviderDisabled(String provider)
{
Toast.makeText( getApplicationContext(),"Gps Disabled",Toast.LENGTH_SHORT ).show();
}

@Override

public void onProviderEnabled(String provider)
{
Toast.makeText( getApplicationContext(),"Gps Enabled",Toast.LENGTH_SHORT).show();
}

@Override

public void onStatusChanged(String provider, int status, Bundle extras)

{
}
}/* End of Class MyLocationListener */

}/* End of UseGps Activity */

我还创建了一个Parser XML,这是代码

package xmlparse;


import java.io.IOException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;

import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class QueryXML {
    public void query() throws ParserConfigurationException, SAXException,
            IOException, XPathExpressionException {
        // Standard of reading a XML file
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setNamespaceAware(true);
        DocumentBuilder builder;
        Document doc = null;
        XPathExpression expr = null;
        builder = factory.newDocumentBuilder();
        doc = builder.parse("a.xml");  //C:\\Users\\aymen\\Desktop\\

        // Create a XPathFactory
        XPathFactory xFactory = XPathFactory.newInstance();

        // Create a XPath object
        XPath xpath = xFactory.newXPath();

        // Compile the XPath expression
        expr = xpath.compile("/postes/poste[gouvernourat='Tunis']/longitude/text()");
        // Run the query and get a nodeset
        Object result = expr.evaluate(doc, XPathConstants.NODESET);

        // Cast the result to a DOM NodeList
        NodeList nodes = (NodeList) result;
        for (int i=0; i<nodes.getLength();i++){
            System.out.println(nodes.item(i).getNodeValue());
        }
    }

    public static void main(String[] args) throws XPathExpressionException, ParserConfigurationException, SAXException, IOException {
        QueryXML process = new QueryXML();
        process.query();
    }
}

现在,我如何确定与当前位置成比例的最近的地方?

提前谢谢

2 个答案:

答案 0 :(得分:1)

鉴于您有两组坐标,您可以使用distanceBetween方法或Haversine formula来计算接近度。

答案 1 :(得分:1)

纬度是相隔恒定的距离(每度约110公里),但距离经度取决于纬度。我认为你最好的选择是转换为XYZ笛卡尔坐标(单位为米),然后使用简单的距离方程找到最近的位置。

距离= SQRT((X2-X1)^ 2 +(Y2-Y1)^ 2 +(Z2-Z1)^ 2)。

要从Lat Long Altitude转换为XYZ(米),请使用以下方法。

R = 6,378,100 //以米为单位的地球半径

LAT =纬度* pi / 180;

LON =经度* pi / 180;

x = -R * cos(LAT)* cos(LON);

y = R * sin(LAT);

z = R * cos(LAT)* sin(LON);