~berthold-daum/zora/trunk

« back to all changes in this revision

Viewing changes to com.bdaum.zoom.gps.naming.google/src/com/bdaum/zoom/gps/naming/google/internal/GoogleGeonamingService.java

  • Committer: bdaum
  • Date: 2015-12-26 10:21:51 UTC
  • Revision ID: berthold.daum@bdaum.de-20151226102151-44f1j5113167thb9
VersionĀ 2.4.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
3
3
import java.io.IOException;
4
4
import java.io.InputStream;
5
5
import java.net.SocketTimeoutException;
 
6
import java.net.URLEncoder;
 
7
import java.net.UnknownHostException;
6
8
import java.text.NumberFormat;
 
9
import java.util.ArrayList;
 
10
import java.util.List;
7
11
import java.util.Locale;
8
12
 
9
13
import javax.xml.parsers.ParserConfigurationException;
18
22
import com.bdaum.zoom.gps.geonames.Place;
19
23
import com.bdaum.zoom.gps.geonames.WebServiceException;
20
24
import com.bdaum.zoom.net.ui.internal.NetActivator;
 
25
import com.bdaum.zoom.ui.gps.WaypointArea;
21
26
 
22
27
@SuppressWarnings("restriction")
23
28
public class GoogleGeonamingService implements IGeonamingService {
31
36
 
32
37
        public Place fetchPlaceInfo(double lat, double lon)
33
38
                        throws SocketTimeoutException, IOException, WebServiceException,
34
 
                        SAXException, ParserConfigurationException {
 
39
                        SAXException, ParserConfigurationException, UnknownHostException {
35
40
                String lang = Locale.getDefault().getLanguage();
36
41
                String latitude = usformat.format(lat);
37
42
                String longitude = usformat.format(lon);
41
46
 
42
47
        private static Place fetchPlaceInfoFromGoogle(String[] parms, double lat,
43
48
                        double lon) throws IOException, SAXException,
44
 
                        ParserConfigurationException {
 
49
                        ParserConfigurationException, UnknownHostException {
45
50
                InputStream in = null;
46
51
                try {
47
52
                        Place place = new Place();
67
72
        }
68
73
 
69
74
        private static InputStream openGoogleService(String query)
70
 
                        throws IOException {
 
75
                        throws IOException, UnknownHostException {
71
76
                IPreferencesService preferencesService = Platform
72
77
                                .getPreferencesService();
73
78
                String clientId = preferencesService.getString(COM_BDAUM_ZOOM_GPS,
91
96
                return NetActivator.getDefault().openStream(query);
92
97
        }
93
98
 
 
99
        public WaypointArea[] findLocation(String address) throws IOException, WebServiceException, SAXException, ParserConfigurationException {
 
100
                List<WaypointArea> pnts = new ArrayList<WaypointArea>();
 
101
                InputStream in = null;
 
102
                Locale locale = Locale.getDefault();
 
103
                try {
 
104
                        in = openGoogleService(NLS
 
105
                                        .bind("http://maps.googleapis.com/maps/api/geocode/xml?address={0}&region={1}&language={2}", //$NON-NLS-1$
 
106
                                                        new Object[]{URLEncoder.encode(address, "UTF-8"), locale.getCountry(), locale.getLanguage()})); //$NON-NLS-1$
 
107
                        try {
 
108
                                new GoogleGeoCodeParser(in).parse(pnts);
 
109
                        } catch (DoneParsingException e) {
 
110
                                // everything okay
 
111
                        }
 
112
                        return pnts.toArray(new WaypointArea[pnts.size()]);
 
113
                } finally {
 
114
                        if (in != null)
 
115
                                try {
 
116
                                        in.close();
 
117
                                } catch (IOException e) {
 
118
                                        // do nothing
 
119
                                }
 
120
                }
 
121
        }
 
122
 
94
123
 
95
124
}