我有一个用Java编写地址的代码,用于生成给定地址的纬度和经度,但在我的项目中,我想要生成地址的3D坐标,如(x1,x2,x3)和(y1,y2) ,y3)形式。我怎么得到这个?
import java.net.HttpURLConnection;`
import java.net.URL;
import java.net.URLEncoder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import java.io.IOException;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPathExpressionException;
import org.xml.sax.SAXException;
public class GeocodingSample {
// URL prefix to the geocoder
private static final String GEOCODER_REQUEST_PREFIX_FOR_XML =
"http://maps.google.com/maps/api/geocode/xml";
public static final void main (String[] argv) throws IOException,
XPathExpressionException, ParserConfigurationException, SAXException {
// query address
String address = "1600 Amphitheatre Parkway, Mountain View, CA";
// prepare a URL to the geocoder
URL url = new URL(GEOCODER_REQUEST_PREFIX_FOR_XML + "?address=" +
URLEncoder.encode(address, "UTF-8") + "&sensor=false");
// prepare an HTTP connection to the geocoder
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
Document geocoderResultDocument = null;
try {
// open the connection and get results as InputSource.
conn.connect();
InputSource geocoderResultInputSource = new
InputSource(conn.getInputStream());
// read result and parse into XML Document
geocoderResultDocument =
DocumentBuilderFactory.newInstance().newDocumentBuilder()
.parse(geocoderResultInputSource);
} finally {
conn.disconnect();
}
// prepare XPath
XPath xpath = XPathFactory.newInstance().newXPath();
// extract the result
NodeList resultNodeList = null;
// a) obtain the formatted_address field for every result
resultNodeList = (NodeList)
xpath.evaluate("/GeocodeResponse/result/formatted_address",
geocoderResultDocument, XPathConstants.NODESET);
for(int i=0; i<resultNodeList.getLength(); ++i) {
System.out.println(resultNodeList.item(i).getTextContent());
}
// b) extract the locality for the first result
resultNodeList = (NodeList)
xpath.evaluate("/GeocodeResponse/result[1]/
address_component[type/text()='locality']/long_name",
geocoderResultDocument, XPathConstants.NODESET);
for(int i=0; i<resultNodeList.getLength(); ++i) {
System.out.println(resultNodeList.item(i).getTextContent());
}
// c) extract the coordinates of the first result
resultNodeList = (NodeList)
xpath.evaluate("/GeocodeResponse/result[1]/geometry/location/*",
geocoderResultDocument, XPathConstants.NODESET);
float lat = Float.NaN;
float lng = Float.NaN;
for(int i=0; i<resultNodeList.getLength(); ++i) {
Node node = resultNodeList.item(i);
if("lat".equals(node.getNodeName())) lat =
Float.parseFloat(node.getTextContent());
if("lng".equals(node.getNodeName())) lng =
Float.parseFloat(node.getTextContent());
}
System.out.println("lat/lng=" + lat + "," + lng);
// c) extract the coordinates of the first result
resultNodeList = (NodeList)
xpath.evaluate("/GeocodeResponse/result[1]/address_component[type/text()
= 'administrative_area_level_1']/country[short_name/text() = 'US']/*",
geocoderResultDocument, XPathConstants.NODESET);
float lat = Float.NaN;
float lng = Float.NaN;
for(int i=0; i<resultNodeList.getLength(); ++i) {
Node node = resultNodeList.item(i);
if("lat".equals(node.getNodeName())) lat =
Float.parseFloat(node.getTextContent());
if("lng".equals(node.getNodeName())) lng =
Float.parseFloat(node.getTextContent());
}
System.out.println("lat/lng=" + lat + "," + lng);
}
}
答案 0 :(得分:0)
您想要获取特定点的海拔高度,我找到了提供此工具的页面,页面http://www.daftlogic.com/sandbox-google-maps-find-altitude.htm中提供了一个代码 我希望它会回答这个问题。
答案 1 :(得分:0)
感谢您的贡献...... 我得到了将纬度和经度转换为3d坐标(x,y,z)的解决方案......就是这样 假设地球模型的半径为r,那么
LAT=latitude*pi/180
LON=longitude*pi/180
x=r*cos(LAT)*cos(LON)
y=r*sin(LAT)
z=r*cos(LAT)*sin(LON)..