yahoogazetteer.java

来自「world wind java sdk 源码」· Java 代码 · 共 97 行

JAVA
97
字号
/*Copyright (C) 2001, 2008 United States Governmentas represented by the Administrator of theNational Aeronautics and Space Administration.All Rights Reserved.*/package gov.nasa.worldwind.poi;import gov.nasa.worldwind.WWObjectImpl;import gov.nasa.worldwind.avlist.AVKey;import gov.nasa.worldwind.exception.*;import gov.nasa.worldwind.geom.LatLon;import gov.nasa.worldwind.util.Logging;import javax.xml.parsers.*;import javax.xml.xpath.*;import java.io.ByteArrayInputStream;import java.util.*;import java.util.logging.Level;/** * A gazetteer that uses Yahoo's geocoding service to find locations for requested places. * * @author tag * @version $Id: YahooGazetteer.java 8480 2009-01-16 19:29:45Z jparsons $ */public class YahooGazetteer extends WWObjectImpl implements Gazetteer{    protected static final String GEOCODE_SERVICE =        "http://local.yahooapis.com/MapsService/V1/geocode?appid=nasaworldwind&location=";    public List<PointOfInterest> findPlaces(String lookupString) throws NoItemException, ServiceException    {        if (lookupString == null || lookupString.length() < 1)            return null;        String locationString = POIUtils.callService(GEOCODE_SERVICE + lookupString.replaceAll(" ", "+"));        if (locationString == null || locationString.length() < 1)            return null;        return this.parseLocationString(locationString);    }    protected ArrayList<PointOfInterest> parseLocationString(String locationString) throws WWRuntimeException    {        try        {            DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();            docBuilderFactory.setNamespaceAware(false);            DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();            org.w3c.dom.Document doc = docBuilder.parse(new ByteArrayInputStream(locationString.getBytes("UTF-8")));            XPathFactory xpFactory = XPathFactory.newInstance();            XPath xpath = xpFactory.newXPath();            org.w3c.dom.NodeList resultNodes =                (org.w3c.dom.NodeList) xpath.evaluate("/ResultSet/Result", doc, XPathConstants.NODESET);            ArrayList<PointOfInterest> positions = new ArrayList<PointOfInterest>(resultNodes.getLength());            for (int i = 0; i < resultNodes.getLength(); i++)            {                org.w3c.dom.Node location = resultNodes.item(i);                String lat = xpath.evaluate("Latitude", location);                String lon = xpath.evaluate("Longitude", location);                StringBuilder displayName = new StringBuilder();                String str = xpath.evaluate("Address", location);                               if (str.length() > 0)                {                    displayName.append(str);                    displayName.append(", ");                }                displayName.append(xpath.evaluate("City", location));                displayName.append(", ");                displayName.append(xpath.evaluate("State", location));                if (lat != null && lon != null)                {                    LatLon latlon = LatLon.fromDegrees(Double.parseDouble(lat), Double.parseDouble(lon));                    PointOfInterest loc = new BasicPointOfInterest(latlon);                    loc.setValue(AVKey.DISPLAY_NAME, displayName.toString());                    positions.add(loc);                }            }            return positions;        }        catch (Exception e)        {            String msg = Logging.getMessage("Gazetteer.URLException", locationString);            Logging.logger().log(Level.SEVERE, msg);            throw new WWRuntimeException(msg);        }    }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?