From f150ba9823901afa864a208b0b59fe4c6725ef83 Mon Sep 17 00:00:00 2001 From: Victor Shcherb Date: Thu, 6 May 2010 09:08:58 +0000 Subject: [PATCH] fix point location layer refactor place of swing ui git-svn-id: https://osmand.googlecode.com/svn/trunk@36 e29c36b1-1cfa-d876-8d93-3434fc2bb7b8 --- .../src/com/osmand/DataExtraction.java | 310 +--------------- .../src/com/osmand/ToDoConstants.java | 15 +- .../src/com/osmand/data/DataTileManager.java | 42 +-- .../src/com/osmand/data/Region.java | 4 + .../src/com/osmand/{ => swing}/MapPanel.java | 9 +- .../src/com/osmand/swing/OsmExtractionUI.java | 331 ++++++++++++++++++ OsmAnd/.classpath | 2 +- OsmAnd/res/layout/main.xml | 1 - OsmAnd/src/com/osmand/ResourceManager.java | 2 +- .../com/osmand/activities/MapActivity.java | 49 +-- .../com/osmand/views/PointLocationLayer.java | 107 ++++++ OsmAnd/src/com/osmand/views/PointOfView.java | 79 ----- 12 files changed, 488 insertions(+), 463 deletions(-) rename DataExtractionOSM/src/com/osmand/{ => swing}/MapPanel.java (94%) create mode 100644 DataExtractionOSM/src/com/osmand/swing/OsmExtractionUI.java create mode 100644 OsmAnd/src/com/osmand/views/PointLocationLayer.java delete mode 100644 OsmAnd/src/com/osmand/views/PointOfView.java diff --git a/DataExtractionOSM/src/com/osmand/DataExtraction.java b/DataExtractionOSM/src/com/osmand/DataExtraction.java index f35fff1992..f2aab9f1cd 100644 --- a/DataExtractionOSM/src/com/osmand/DataExtraction.java +++ b/DataExtractionOSM/src/com/osmand/DataExtraction.java @@ -1,45 +1,13 @@ package com.osmand; -import java.awt.BorderLayout; -import java.awt.Component; -import java.awt.Container; -import java.awt.MenuBar; -import java.awt.event.ActionEvent; -import java.awt.event.ActionListener; -import java.awt.event.WindowAdapter; -import java.awt.event.WindowEvent; -import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.util.ArrayList; -import java.util.Collection; -import java.util.Collections; -import java.util.Comparator; import java.util.List; -import java.util.Map; -import java.util.TreeMap; -import javax.swing.DefaultListCellRenderer; -import javax.swing.JButton; -import javax.swing.JFrame; -import javax.swing.JLabel; -import javax.swing.JList; -import javax.swing.JPanel; -import javax.swing.JScrollPane; -import javax.swing.JSplitPane; -import javax.swing.JTextField; -import javax.swing.JTree; -import javax.swing.UIManager; -import javax.swing.event.ListSelectionEvent; -import javax.swing.event.ListSelectionListener; -import javax.swing.event.TreeSelectionEvent; -import javax.swing.event.TreeSelectionListener; -import javax.swing.event.UndoableEditEvent; -import javax.swing.event.UndoableEditListener; -import javax.swing.tree.DefaultMutableTreeNode; import javax.xml.parsers.ParserConfigurationException; import javax.xml.stream.XMLStreamException; @@ -51,11 +19,8 @@ import com.osmand.data.Amenity; import com.osmand.data.City; import com.osmand.data.DataTileManager; import com.osmand.data.Region; -import com.osmand.data.Street; -import com.osmand.data.City.CityType; import com.osmand.osm.Entity; import com.osmand.osm.LatLon; -import com.osmand.osm.MapUtils; import com.osmand.osm.Node; import com.osmand.osm.OSMSettings; import com.osmand.osm.Relation; @@ -63,6 +28,7 @@ import com.osmand.osm.Way; import com.osmand.osm.OSMSettings.OSMTagKey; import com.osmand.osm.io.OSMStorageWriter; import com.osmand.osm.io.OsmBaseStorage; +import com.osmand.swing.OsmExtractionUI; // TO implement @@ -99,7 +65,7 @@ import com.osmand.osm.io.OsmBaseStorage; * 7. node, way - amenity=? * */ -public class DataExtraction implements IMapLocationListener { +public class DataExtraction { public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException, XMLStreamException { new DataExtraction().testReadingOsmFile(); @@ -216,10 +182,9 @@ public class DataExtraction implements IMapLocationListener { } } - DataTileManager amenitiesManager = new DataTileManager(); + for(Amenity a: amenities){ country.registerAmenity(a); - amenitiesManager.registerObject(a.getNode().getLatitude(), a.getNode().getLongitude(), a.getNode().getLatLon()); } @@ -232,10 +197,10 @@ public class DataExtraction implements IMapLocationListener { } } } - mapPanel.setPoints(amenitiesManager); - - runUI(country); + OsmExtractionUI ui = new OsmExtractionUI(country); + ui.runUI(); + List interestedObjects = new ArrayList(); // MapUtils.addIdsToList(places, interestedObjects); for(Amenity a : amenities){ @@ -263,268 +228,5 @@ public class DataExtraction implements IMapLocationListener { /////////////////////////////////////////// // 2. Showing UI - protected City selectedCity; - - private MapPanel mapPanel = new MapPanel(new File(DefaultLauncherConstants.pathToDirWithTiles)); - - private DefaultMutableTreeNode amenitiesTree; - private JTree treePlaces; - - public void runUI(final Region r){ - JFrame frame = new JFrame("Tree of choose"); - try { - UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); - } catch (Exception e) { - e.printStackTrace(); - } - DefaultMutableTreeNode root = new DataExtractionTreeNode(r.getName(), r); - amenitiesTree = new DataExtractionTreeNode("Amenities", r); - amenitiesTree.add(new DataExtractionTreeNode("closest", r)); - root.add(amenitiesTree); - for(CityType t : CityType.values()){ - DefaultMutableTreeNode cityTree = new DataExtractionTreeNode(t.toString(), t); - root.add(cityTree); - for(City ct : r.getCitiesByType(t)){ - DefaultMutableTreeNode cityNodeTree = new DataExtractionTreeNode(ct.getName(), ct); - cityTree.add(cityNodeTree); - - for(Street str : ct.getStreets()){ - DefaultMutableTreeNode strTree = new DataExtractionTreeNode(str.getName(), str); - cityNodeTree.add(strTree); - for(Entity e : str.getBuildings()){ - DefaultMutableTreeNode building = new DataExtractionTreeNode(e.getTag(OSMTagKey.ADDR_HOUSE_NUMBER), e); - strTree.add(building); - - } - } - } - } - - - - frame.addWindowListener(new ExitListener()); - Container content = frame.getContentPane(); - frame.setFocusable(true); - - - treePlaces = new JTree(root); - final JList jList = new JList(); - jList.setCellRenderer(new DefaultListCellRenderer(){ - private static final long serialVersionUID = 4661949460526837891L; - - @Override - public Component getListCellRendererComponent(JList list, - Object value, int index, boolean isSelected, - boolean cellHasFocus) { - super.getListCellRendererComponent(list, value, index, isSelected, - cellHasFocus); - if(value instanceof City){ - setText(((City)value).getName()); - } - return this; - } - }); - - JSplitPane panelForTreeAndImage = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, new JScrollPane(treePlaces), mapPanel); - panelForTreeAndImage.setResizeWeight(0.2); - mapPanel.setFocusable(true); - mapPanel.addMapLocationListener(this); - - - - JSplitPane pane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, new JScrollPane(jList), panelForTreeAndImage); - pane.setResizeWeight(0.2); - content.add(pane, BorderLayout.CENTER); - - final JLabel label = new JLabel(); - content.add(label, BorderLayout.SOUTH); - - JPanel panel = new JPanel(new BorderLayout()); - final JTextField textField = new JTextField(); - final JButton button = new JButton(); - button.setText("Set town"); - - - panel.add(textField, BorderLayout.CENTER); - panel.add(button, BorderLayout.WEST); - - content.add(panel, BorderLayout.NORTH); - - - updateListCities(r, textField.getText(), jList); - textField.getDocument().addUndoableEditListener(new UndoableEditListener(){ - @Override - public void undoableEditHappened(UndoableEditEvent e) { - updateListCities(r, textField.getText(), jList); - } - }); - - button.addActionListener(new ActionListener(){ - @Override - public void actionPerformed(ActionEvent e) { - selectedCity = (City)jList.getSelectedValue(); - } - }); - - jList.addListSelectionListener(new ListSelectionListener(){ - @Override - public void valueChanged(ListSelectionEvent e) { - if(jList.getSelectedValue() != null){ - Node node = ((City)jList.getSelectedValue()).getNode(); - String text = "Lat : " + node.getLatitude() + " Lon " + node.getLongitude(); - if(selectedCity != null){ - text += " distance " + MapUtils.getDistance(selectedCity.getNode(), node); - } - label.setText(text); - mapPanel.setLatLon(node.getLatitude(), node.getLongitude()); - } else { - String text = selectedCity == null ? "" : selectedCity.getName(); - label.setText(text); - } - - } - - }); - - treePlaces.addTreeSelectionListener(new TreeSelectionListener(){ - @Override - public void valueChanged(TreeSelectionEvent e) { - if (e.getPath() != null) { - if (e.getPath().getLastPathComponent() instanceof DefaultMutableTreeNode) { - Object o = ((DefaultMutableTreeNode) e.getPath().getLastPathComponent()).getUserObject(); - - if (o instanceof City) { - City c = (City) o; - mapPanel.setLatLon(c.getNode().getLatitude(), c.getNode().getLongitude()); - mapPanel.requestFocus(); - } - - if (o instanceof Entity) { - Entity c = (Entity) o; - if (c instanceof Node) { - mapPanel.setLatLon(((Node) c).getLatitude(), ((Node) c).getLongitude()); -// mapPanel.requestFocus(); - } else { - DefaultMutableTreeNode n = (DefaultMutableTreeNode) e.getPath().getPathComponent( - e.getPath().getPathCount() - 2); - if (n.getUserObject() instanceof Street) { - Street str = (Street) n.getUserObject(); - LatLon l = str.getLocationBuilding(c); - mapPanel.setLatLon(l.getLatitude(), l.getLongitude()); - mapPanel.requestFocus(); - } - } - } - } - } - - } - }); - - MenuBar bar = new MenuBar(); - bar.add(MapPanel.getMenuToChooseSource(mapPanel)); - frame.setMenuBar(bar); - frame.setSize(1024, 768); - frame.setVisible(true); - } - - @Override - public void locationChanged(final double newLatitude, final double newLongitude, Object source){ - Region reg = (Region) amenitiesTree.getUserObject(); - List closestAmenities = reg.getClosestAmenities(newLatitude, newLongitude); - Collections.sort(closestAmenities, new Comparator(){ - @Override - public int compare(Amenity o1, Amenity o2) { - return Double.compare(MapUtils.getDistance(o1.getNode(), newLatitude, newLongitude), - MapUtils.getDistance(o2.getNode(), newLatitude, newLongitude)); - } - - }); - - Map> filter = new TreeMap>(); - for(Amenity n : closestAmenities){ - String type = n.getType().toString(); - if(!filter.containsKey(type)){ - filter.put(type, new ArrayList()); - } - filter.get(type).add(n); - } - for(int i=1; i< amenitiesTree.getChildCount(); ){ - if(!filter.containsKey(((DefaultMutableTreeNode)amenitiesTree.getChildAt(i)).getUserObject())){ - amenitiesTree.remove(i); - } else { - i++; - } - } - - ((DefaultMutableTreeNode)amenitiesTree.getChildAt(0)).removeAllChildren(); - - - for(int i=0; i<15 && i < closestAmenities.size(); i++){ - Amenity n = closestAmenities.get(i); - int dist = (int) (MapUtils.getDistance(n.getNode(), newLatitude, newLongitude)); - String str = n.getSimpleFormat() + " [" +dist+" m ]"; - ((DefaultMutableTreeNode)amenitiesTree.getChildAt(0)).add( - new DataExtractionTreeNode(str, n)); - } - - for(String s : filter.keySet()){ - DefaultMutableTreeNode p = null; - for(int i=0; i< amenitiesTree.getChildCount(); i++){ - if(s.equals(((DefaultMutableTreeNode)amenitiesTree.getChildAt(i)).getUserObject())){ - p = ((DefaultMutableTreeNode)amenitiesTree.getChildAt(i)); - break; - } - } - if (p == null) { - p = new DefaultMutableTreeNode(s); - } - - p.removeAllChildren(); - for (Amenity n : filter.get(s)) { - int dist = (int) (MapUtils.getDistance(n.getNode(), newLatitude, newLongitude)); - String str = n.getSimpleFormat() + " [" + dist + " m ]"; - DataExtractionTreeNode node = new DataExtractionTreeNode(str, n); - p.add(node); - } - amenitiesTree.add(p); - } - treePlaces.updateUI(); - } - - public void updateListCities(Region r, String text, JList jList){ - Collection city = r.getSuggestedCities(text, 100); - City[] names = new City[city.size()]; - int i=0; - for(City c : city){ - names[i++] = c; - } - jList.setListData(names); - } - - - public static class DataExtractionTreeNode extends DefaultMutableTreeNode { - private static final long serialVersionUID = 1L; - private String name; - - public DataExtractionTreeNode(String name, Object userObject){ - super(userObject); - this.name = name; - } - - public void setName(String name) { - this.name = name; - } - @Override - public String toString() { - return name; - } - - } - public static class ExitListener extends WindowAdapter { - public void windowClosing(WindowEvent event) { - System.exit(0); - } - } } diff --git a/DataExtractionOSM/src/com/osmand/ToDoConstants.java b/DataExtractionOSM/src/com/osmand/ToDoConstants.java index 4c4b68665a..3968d098fc 100644 --- a/DataExtractionOSM/src/com/osmand/ToDoConstants.java +++ b/DataExtractionOSM/src/com/osmand/ToDoConstants.java @@ -22,13 +22,22 @@ public class ToDoConstants { public int EXTRACT_COMMON_PARTS_FROM_MAPPANEL_AND_OSMMAPVIEW = 5; - public int REVISE_MAP_ACTIVITY_HOLD_ALL_ZOOM_LATLON_IN_ONEPLACE = 6; - - /** * Write activity to show something about authors / donation .... */ public int DESCRIBE_ABOUT_AUTHORS = 8; + // TODO see all calculations x, y for layers & for MapView + + //// TODO for releasing version + // 1. POI SEARCH NEAR TO YOU + // 2. FIX BACK TO your location & gps & point of view (may be compass) + // 3. Save settings between session + // 4. Revise UI icons/layout + // 5. Fix Java Spring to prepare your data!!! (add progress, import/export data) + // 6. Enable city/streets/buildings index + // 7. Search for city/streets/buildings! + // ------------------- + } diff --git a/DataExtractionOSM/src/com/osmand/data/DataTileManager.java b/DataExtractionOSM/src/com/osmand/data/DataTileManager.java index ce5d3a4537..b0c6c1d1f3 100644 --- a/DataExtractionOSM/src/com/osmand/data/DataTileManager.java +++ b/DataExtractionOSM/src/com/osmand/data/DataTileManager.java @@ -38,6 +38,14 @@ public class DataTileManager { } } + public List getAllObjects(){ + List l = new ArrayList(); + for(String s : objects.keySet()){ + l.addAll(objects.get(s)); + } + return l; + } + public List getObjects(double latitudeUp, double longitudeUp, double latitudeDown, double longitudeDown) { int tileXUp = (int) MapUtils.getTileNumberX(zoom, longitudeUp); int tileYUp = (int) MapUtils.getTileNumberY(zoom, latitudeUp); @@ -72,7 +80,7 @@ public class DataTileManager { // go through circle for (int i = 1; i <= depth; i++) { - // goes à + // goes � for (int j = 0; j <= i; j++) { // left & right int dx = j == 0 ? 0 : -1; @@ -112,37 +120,5 @@ public class DataTileManager { } - - // testing way to search - public static void print(int x, int y){ - System.out.println(x + (y-1)*5); - } - - public static void main(String[] args) { - int tileX = 3; - int tileY = 3; - int depth = 3; - for(int i=1; i<=depth; i++){ - - // goes à - for(int j=0; j<=i; j++){ - // left & right - int dx = j==0 ? 0 : -1; - for(; dx < 1 || (j < i && dx == 1); dx +=2){ - // north - print(tileX + dx * j, tileY + i); - // east - print(tileX + i, tileY - dx *j); - // south - print(tileX - dx * j, tileY - i); - // west - print(tileX - i, tileY + dx *j); - } - } - } - } - - - } diff --git a/DataExtractionOSM/src/com/osmand/data/Region.java b/DataExtractionOSM/src/com/osmand/data/Region.java index 3c4e37edc9..e292d04814 100644 --- a/DataExtractionOSM/src/com/osmand/data/Region.java +++ b/DataExtractionOSM/src/com/osmand/data/Region.java @@ -91,6 +91,10 @@ public class Region { return amenities.getClosestObjects(latitude, longitude, 2); } + public DataTileManager getAmenityManager(){ + return amenities; + } + public void registerAmenity(Amenity a){ amenities.registerObject(a.getNode().getLatitude(), a.getNode().getLongitude(), a); } diff --git a/DataExtractionOSM/src/com/osmand/MapPanel.java b/DataExtractionOSM/src/com/osmand/swing/MapPanel.java similarity index 94% rename from DataExtractionOSM/src/com/osmand/MapPanel.java rename to DataExtractionOSM/src/com/osmand/swing/MapPanel.java index c498904679..b1f2e0c0da 100644 --- a/DataExtractionOSM/src/com/osmand/MapPanel.java +++ b/DataExtractionOSM/src/com/osmand/swing/MapPanel.java @@ -1,4 +1,4 @@ -package com.osmand; +package com.osmand.swing; import java.awt.BorderLayout; import java.awt.Color; @@ -33,7 +33,10 @@ import javax.swing.UIManager; import org.apache.commons.logging.Log; -import com.osmand.DataExtraction.ExitListener; +import com.osmand.DefaultLauncherConstants; +import com.osmand.IMapLocationListener; +import com.osmand.LogUtil; +import com.osmand.MapTileDownloader; import com.osmand.MapTileDownloader.DownloadRequest; import com.osmand.MapTileDownloader.IMapDownloaderCallback; import com.osmand.data.DataTileManager; @@ -77,7 +80,7 @@ public class MapPanel extends JPanel implements IMapDownloaderCallback { } - frame.addWindowListener(new ExitListener()); + frame.addWindowListener(new OsmExtractionUI.ExitListener()); Container content = frame.getContentPane(); MapPanel panel = new MapPanel(new File(DefaultLauncherConstants.pathToDirWithTiles)); diff --git a/DataExtractionOSM/src/com/osmand/swing/OsmExtractionUI.java b/DataExtractionOSM/src/com/osmand/swing/OsmExtractionUI.java new file mode 100644 index 0000000000..e5b725ebb2 --- /dev/null +++ b/DataExtractionOSM/src/com/osmand/swing/OsmExtractionUI.java @@ -0,0 +1,331 @@ +package com.osmand.swing; + +import java.awt.BorderLayout; +import java.awt.Component; +import java.awt.Container; +import java.awt.MenuBar; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; +import java.awt.event.WindowAdapter; +import java.awt.event.WindowEvent; +import java.io.File; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.Comparator; +import java.util.List; +import java.util.Map; +import java.util.TreeMap; + +import javax.swing.DefaultListCellRenderer; +import javax.swing.JButton; +import javax.swing.JFrame; +import javax.swing.JLabel; +import javax.swing.JList; +import javax.swing.JPanel; +import javax.swing.JScrollPane; +import javax.swing.JSplitPane; +import javax.swing.JTextField; +import javax.swing.JTree; +import javax.swing.UIManager; +import javax.swing.event.ListSelectionEvent; +import javax.swing.event.ListSelectionListener; +import javax.swing.event.TreeSelectionEvent; +import javax.swing.event.TreeSelectionListener; +import javax.swing.event.UndoableEditEvent; +import javax.swing.event.UndoableEditListener; +import javax.swing.tree.DefaultMutableTreeNode; + +import com.osmand.DefaultLauncherConstants; +import com.osmand.IMapLocationListener; +import com.osmand.data.Amenity; +import com.osmand.data.City; +import com.osmand.data.DataTileManager; +import com.osmand.data.Region; +import com.osmand.data.Street; +import com.osmand.data.City.CityType; +import com.osmand.osm.Entity; +import com.osmand.osm.LatLon; +import com.osmand.osm.MapUtils; +import com.osmand.osm.Node; +import com.osmand.osm.OSMSettings.OSMTagKey; + +public class OsmExtractionUI implements IMapLocationListener { + protected City selectedCity; + + private MapPanel mapPanel = new MapPanel(new File(DefaultLauncherConstants.pathToDirWithTiles)); + + private DefaultMutableTreeNode amenitiesTree; + private JTree treePlaces; + + private final Region r; + + public OsmExtractionUI(final Region r){ + this.r = r; + } + + + public void runUI(){ + JFrame frame = new JFrame("Tree of choose"); + try { + UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); + } catch (Exception e) { + e.printStackTrace(); + } + DefaultMutableTreeNode root = new DataExtractionTreeNode(r.getName(), r); + amenitiesTree = new DataExtractionTreeNode("Amenities", r); + amenitiesTree.add(new DataExtractionTreeNode("closest", r)); + root.add(amenitiesTree); + for(CityType t : CityType.values()){ + DefaultMutableTreeNode cityTree = new DataExtractionTreeNode(t.toString(), t); + root.add(cityTree); + for(City ct : r.getCitiesByType(t)){ + DefaultMutableTreeNode cityNodeTree = new DataExtractionTreeNode(ct.getName(), ct); + cityTree.add(cityNodeTree); + + for(Street str : ct.getStreets()){ + DefaultMutableTreeNode strTree = new DataExtractionTreeNode(str.getName(), str); + cityNodeTree.add(strTree); + for(Entity e : str.getBuildings()){ + DefaultMutableTreeNode building = new DataExtractionTreeNode(e.getTag(OSMTagKey.ADDR_HOUSE_NUMBER), e); + strTree.add(building); + + } + } + } + } + + + + frame.addWindowListener(new ExitListener()); + Container content = frame.getContentPane(); + frame.setFocusable(true); + + + treePlaces = new JTree(root); + final JList jList = new JList(); + jList.setCellRenderer(new DefaultListCellRenderer(){ + private static final long serialVersionUID = 4661949460526837891L; + + @Override + public Component getListCellRendererComponent(JList list, + Object value, int index, boolean isSelected, + boolean cellHasFocus) { + super.getListCellRendererComponent(list, value, index, isSelected, + cellHasFocus); + if(value instanceof City){ + setText(((City)value).getName()); + } + return this; + } + }); + + JSplitPane panelForTreeAndImage = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, new JScrollPane(treePlaces), mapPanel); + panelForTreeAndImage.setResizeWeight(0.2); + mapPanel.setFocusable(true); + mapPanel.addMapLocationListener(this); + + + DataTileManager amenitiesManager = new DataTileManager(); + for(Amenity a : r.getAmenityManager().getAllObjects()){ + amenitiesManager.registerObject(a.getNode().getLatitude(), a.getNode().getLongitude(), a.getNode().getLatLon()); + } + mapPanel.setPoints(amenitiesManager); + + + JSplitPane pane = new JSplitPane(JSplitPane.VERTICAL_SPLIT, new JScrollPane(jList), panelForTreeAndImage); + pane.setResizeWeight(0.2); + content.add(pane, BorderLayout.CENTER); + + final JLabel label = new JLabel(); + content.add(label, BorderLayout.SOUTH); + + JPanel panel = new JPanel(new BorderLayout()); + final JTextField textField = new JTextField(); + final JButton button = new JButton(); + button.setText("Set town"); + + + panel.add(textField, BorderLayout.CENTER); + panel.add(button, BorderLayout.WEST); + + content.add(panel, BorderLayout.NORTH); + + + updateListCities(r, textField.getText(), jList); + textField.getDocument().addUndoableEditListener(new UndoableEditListener(){ + @Override + public void undoableEditHappened(UndoableEditEvent e) { + updateListCities(r, textField.getText(), jList); + } + }); + + button.addActionListener(new ActionListener(){ + @Override + public void actionPerformed(ActionEvent e) { + selectedCity = (City)jList.getSelectedValue(); + } + }); + + jList.addListSelectionListener(new ListSelectionListener(){ + @Override + public void valueChanged(ListSelectionEvent e) { + if(jList.getSelectedValue() != null){ + Node node = ((City)jList.getSelectedValue()).getNode(); + String text = "Lat : " + node.getLatitude() + " Lon " + node.getLongitude(); + if(selectedCity != null){ + text += " distance " + MapUtils.getDistance(selectedCity.getNode(), node); + } + label.setText(text); + mapPanel.setLatLon(node.getLatitude(), node.getLongitude()); + } else { + String text = selectedCity == null ? "" : selectedCity.getName(); + label.setText(text); + } + + } + + }); + + treePlaces.addTreeSelectionListener(new TreeSelectionListener(){ + @Override + public void valueChanged(TreeSelectionEvent e) { + if (e.getPath() != null) { + if (e.getPath().getLastPathComponent() instanceof DefaultMutableTreeNode) { + Object o = ((DefaultMutableTreeNode) e.getPath().getLastPathComponent()).getUserObject(); + + if (o instanceof City) { + City c = (City) o; + mapPanel.setLatLon(c.getNode().getLatitude(), c.getNode().getLongitude()); + mapPanel.requestFocus(); + } + + if (o instanceof Entity) { + Entity c = (Entity) o; + if (c instanceof Node) { + mapPanel.setLatLon(((Node) c).getLatitude(), ((Node) c).getLongitude()); +// mapPanel.requestFocus(); + } else { + DefaultMutableTreeNode n = (DefaultMutableTreeNode) e.getPath().getPathComponent( + e.getPath().getPathCount() - 2); + if (n.getUserObject() instanceof Street) { + Street str = (Street) n.getUserObject(); + LatLon l = str.getLocationBuilding(c); + mapPanel.setLatLon(l.getLatitude(), l.getLongitude()); + mapPanel.requestFocus(); + } + } + } + } + } + + } + }); + + MenuBar bar = new MenuBar(); + bar.add(MapPanel.getMenuToChooseSource(mapPanel)); + frame.setMenuBar(bar); + frame.setSize(1024, 768); + frame.setVisible(true); + } + + @Override + public void locationChanged(final double newLatitude, final double newLongitude, Object source){ + Region reg = (Region) amenitiesTree.getUserObject(); + List closestAmenities = reg.getClosestAmenities(newLatitude, newLongitude); + Collections.sort(closestAmenities, new Comparator(){ + @Override + public int compare(Amenity o1, Amenity o2) { + return Double.compare(MapUtils.getDistance(o1.getNode(), newLatitude, newLongitude), + MapUtils.getDistance(o2.getNode(), newLatitude, newLongitude)); + } + + }); + + Map> filter = new TreeMap>(); + for(Amenity n : closestAmenities){ + String type = n.getType().toString(); + if(!filter.containsKey(type)){ + filter.put(type, new ArrayList()); + } + filter.get(type).add(n); + } + for(int i=1; i< amenitiesTree.getChildCount(); ){ + if(!filter.containsKey(((DefaultMutableTreeNode)amenitiesTree.getChildAt(i)).getUserObject())){ + amenitiesTree.remove(i); + } else { + i++; + } + } + + ((DefaultMutableTreeNode)amenitiesTree.getChildAt(0)).removeAllChildren(); + + + for(int i=0; i<15 && i < closestAmenities.size(); i++){ + Amenity n = closestAmenities.get(i); + int dist = (int) (MapUtils.getDistance(n.getNode(), newLatitude, newLongitude)); + String str = n.getSimpleFormat() + " [" +dist+" m ]"; + ((DefaultMutableTreeNode)amenitiesTree.getChildAt(0)).add( + new DataExtractionTreeNode(str, n)); + } + + for(String s : filter.keySet()){ + DefaultMutableTreeNode p = null; + for(int i=0; i< amenitiesTree.getChildCount(); i++){ + if(s.equals(((DefaultMutableTreeNode)amenitiesTree.getChildAt(i)).getUserObject())){ + p = ((DefaultMutableTreeNode)amenitiesTree.getChildAt(i)); + break; + } + } + if (p == null) { + p = new DefaultMutableTreeNode(s); + } + + p.removeAllChildren(); + for (Amenity n : filter.get(s)) { + int dist = (int) (MapUtils.getDistance(n.getNode(), newLatitude, newLongitude)); + String str = n.getSimpleFormat() + " [" + dist + " m ]"; + DataExtractionTreeNode node = new DataExtractionTreeNode(str, n); + p.add(node); + } + amenitiesTree.add(p); + } + treePlaces.updateUI(); + } + + public void updateListCities(Region r, String text, JList jList){ + Collection city = r.getSuggestedCities(text, 100); + City[] names = new City[city.size()]; + int i=0; + for(City c : city){ + names[i++] = c; + } + jList.setListData(names); + } + + + public static class DataExtractionTreeNode extends DefaultMutableTreeNode { + private static final long serialVersionUID = 1L; + private String name; + + public DataExtractionTreeNode(String name, Object userObject){ + super(userObject); + this.name = name; + } + + public void setName(String name) { + this.name = name; + } + @Override + public String toString() { + return name; + } + + } + public static class ExitListener extends WindowAdapter { + public void windowClosing(WindowEvent event) { + System.exit(0); + } + } + +} diff --git a/OsmAnd/.classpath b/OsmAnd/.classpath index 75991137f3..82f47e085f 100644 --- a/OsmAnd/.classpath +++ b/OsmAnd/.classpath @@ -1,7 +1,7 @@ - + diff --git a/OsmAnd/res/layout/main.xml b/OsmAnd/res/layout/main.xml index ba1575331b..0fae651b40 100644 --- a/OsmAnd/res/layout/main.xml +++ b/OsmAnd/res/layout/main.xml @@ -4,7 +4,6 @@ android:layout_width="fill_parent" android:layout_height="fill_parent"> - diff --git a/OsmAnd/src/com/osmand/ResourceManager.java b/OsmAnd/src/com/osmand/ResourceManager.java index c8a3613251..13bb8cd8d4 100644 --- a/OsmAnd/src/com/osmand/ResourceManager.java +++ b/OsmAnd/src/com/osmand/ResourceManager.java @@ -163,7 +163,7 @@ public class ResourceManager { onLowMemory(); } - if (!downloader.isFileCurrentlyDownloaded(en)) { + if (!downloader.isFileCurrentlyDownloaded(en) && req.dirWithTiles.canRead()) { if (en.exists()) { long time = System.currentTimeMillis(); cacheOfImages.put(req.fileToLoad, BitmapFactory.decodeFile(en.getAbsolutePath())); diff --git a/OsmAnd/src/com/osmand/activities/MapActivity.java b/OsmAnd/src/com/osmand/activities/MapActivity.java index 0cda4ee094..8acd92179c 100644 --- a/OsmAnd/src/com/osmand/activities/MapActivity.java +++ b/OsmAnd/src/com/osmand/activities/MapActivity.java @@ -22,10 +22,9 @@ import com.osmand.IMapLocationListener; import com.osmand.OsmandSettings; import com.osmand.R; import com.osmand.ResourceManager; -import com.osmand.osm.MapUtils; import com.osmand.views.OsmandMapTileView; import com.osmand.views.POIMapLayer; -import com.osmand.views.PointOfView; +import com.osmand.views.PointLocationLayer; public class MapActivity extends Activity implements LocationListener, IMapLocationListener { /** Called when the activity is first created. */ @@ -39,7 +38,7 @@ public class MapActivity extends Activity implements LocationListener, IMapLocat private ImageButton backToMenu; - private PointOfView pointOfView; + private PointLocationLayer locationLayer; private POIMapLayer poiMapLayer; @@ -58,6 +57,8 @@ public class MapActivity extends Activity implements LocationListener, IMapLocat poiMapLayer = new POIMapLayer(); poiMapLayer.setNodeManager(ResourceManager.getResourceManager().getPoiIndex()); mapView.addLayer(poiMapLayer); + locationLayer = new PointLocationLayer(); + mapView.addLayer(locationLayer); ZoomControls zoomControls = (ZoomControls) findViewById(R.id.ZoomControls01); @@ -74,8 +75,6 @@ public class MapActivity extends Activity implements LocationListener, IMapLocat } }); - pointOfView = (PointOfView)findViewById(R.id.PointOfView); - backToLocation = (ImageButton)findViewById(R.id.BackToLocation); backToLocation.setVisibility(linkLocationWithMap ? View.INVISIBLE : View.VISIBLE); backToLocation.setOnClickListener(new OnClickListener(){ @@ -104,10 +103,6 @@ public class MapActivity extends Activity implements LocationListener, IMapLocat }); - LocationManager service = (LocationManager) getSystemService(LOCATION_SERVICE); - service.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 0, this); - - } @@ -118,8 +113,9 @@ public class MapActivity extends Activity implements LocationListener, IMapLocat lastKnownLocation = location; if(linkLocationWithMap){ mapView.setLatLon(location.getLatitude(), location.getLongitude()); - } - validatePointOfView(); + locationLayer.setLastKnownLocation(lastKnownLocation); + } + } @Override @@ -137,32 +133,6 @@ public class MapActivity extends Activity implements LocationListener, IMapLocat // TODO when provider disabled reset lastKnownLocation! } - public void validatePointOfView(){ - if(lastKnownLocation == null){ - if(pointOfView.isVisible()){ - pointOfView.setLocationX(-1); - pointOfView.setLocationY(-1); - pointOfView.setAreaRadius(0); - pointOfView.invalidate(); - } - } else { - int newX = MapUtils.getPixelShiftX(mapView.getZoom(), - lastKnownLocation.getLongitude(), mapView.getLongitude(), mapView.getTileSize()) + - mapView.getWidth()/2; - int newY = MapUtils.getPixelShiftY(mapView.getZoom(), - lastKnownLocation.getLatitude(), mapView.getLatitude() , mapView.getTileSize()) + - mapView.getHeight()/2; - // TODO specify bearing! - int radius = MapUtils.getLengthXFromMeters(mapView.getZoom(), mapView.getLatitude(), mapView.getLongitude(), - lastKnownLocation.getAccuracy(), mapView.getTileSize(), mapView.getWidth()); - - pointOfView.setLocationX(newX); - pointOfView.setLocationY(newY); - pointOfView.setAreaRadius(radius); - pointOfView.invalidate(); - } - } - @Override protected void onSaveInstanceState(Bundle outState) { // TODO Auto-generated method stub @@ -172,6 +142,8 @@ public class MapActivity extends Activity implements LocationListener, IMapLocat @Override protected void onPause() { + LocationManager service = (LocationManager) getSystemService(LOCATION_SERVICE); + service.removeUpdates(this); // TODO switch off gps super.onPause(); } @@ -190,6 +162,8 @@ public class MapActivity extends Activity implements LocationListener, IMapLocat mapView.removeLayer(poiMapLayer); } } + LocationManager service = (LocationManager) getSystemService(LOCATION_SERVICE); + service.requestLocationUpdates(LocationManager.GPS_PROVIDER, 2000, 0, this); } @@ -208,7 +182,6 @@ public class MapActivity extends Activity implements LocationListener, IMapLocat linkLocationWithMap = false; backToLocation.setVisibility(View.VISIBLE); } - validatePointOfView(); } public boolean onCreateOptionsMenu(Menu menu) { diff --git a/OsmAnd/src/com/osmand/views/PointLocationLayer.java b/OsmAnd/src/com/osmand/views/PointLocationLayer.java new file mode 100644 index 0000000000..2b8832d3c3 --- /dev/null +++ b/OsmAnd/src/com/osmand/views/PointLocationLayer.java @@ -0,0 +1,107 @@ +package com.osmand.views; + +import android.graphics.Canvas; +import android.graphics.Color; +import android.graphics.Paint; +import android.location.Location; +import android.view.MotionEvent; + +import com.osmand.osm.MapUtils; + +public class PointLocationLayer implements OsmandMapLayer { + private Paint location; + private Paint area; + + protected Location lastKnownLocation = null; + private OsmandMapTileView view; + + private void initUI() { + location = new Paint(); + location.setColor(Color.BLUE); + location.setAlpha(150); + location.setAntiAlias(true); + + area = new Paint(); + area.setColor(Color.BLUE); + area.setAlpha(40); + } + + public void initLayer(OsmandMapTileView view) { + this.view = view; + initUI(); + } + + + @Override + public boolean onTouchEvent(MotionEvent event) { + return false; + } + + + @Override + public void onDraw(Canvas canvas) { + if (isLocationVisible(lastKnownLocation)) { + int locationX = MapUtils.getPixelShiftX(view.getZoom(), lastKnownLocation.getLongitude(), view.getLongitude(), view + .getTileSize()) + + view.getWidth() / 2; + int locationY = MapUtils + .getPixelShiftY(view.getZoom(), lastKnownLocation.getLatitude(), view.getLatitude(), view.getTileSize()) + + view.getHeight() / 2; + // TODO specify bearing! + int radius = MapUtils.getLengthXFromMeters(view.getZoom(), view.getLatitude(), view.getLongitude(), lastKnownLocation + .getAccuracy(), view.getTileSize(), view.getWidth()); + + if (locationX >= 0 && locationY >= 0) { + canvas.drawCircle(locationX, locationY, 4, location); + } + if (radius > 4) { + canvas.drawCircle(locationX, locationY, radius, area); + } + } + } + + public boolean isLocationVisible(Location l){ + if(l == null || view == null){ + return false; + } + int newX = MapUtils.getPixelShiftX(view.getZoom(), + l.getLongitude(), view.getLongitude(), view.getTileSize()) + + view.getWidth()/2; + int newY = MapUtils.getPixelShiftY(view.getZoom(), + l.getLatitude(), view.getLatitude() , view.getTileSize()) + + view.getHeight()/2; + int radius = MapUtils.getLengthXFromMeters(view.getZoom(), view.getLatitude(), view.getLongitude(), + l.getAccuracy(), view.getTileSize(), view.getWidth()); + if(newX >= 0 && newX <= view.getWidth() && newY >=0 && newY <= view.getHeight()){ + return true; + } else { + // check radius (simplified version + if (newX + radius >= 0 && newX - radius <= view.getWidth() && newY + radius >= 0 && newY - radius <= view.getHeight()) { + return true; + } + } + return false; + } + + + public Location getLastKnownLocation() { + return lastKnownLocation; + } + + public void setLastKnownLocation(Location lastKnownLocation) { + boolean redraw = isLocationVisible(this.lastKnownLocation) || isLocationVisible(lastKnownLocation); + this.lastKnownLocation = lastKnownLocation; + if(redraw){ + view.prepareImage(); + } + } + + @Override + public void destroyLayer() { + + } + + + + +} diff --git a/OsmAnd/src/com/osmand/views/PointOfView.java b/OsmAnd/src/com/osmand/views/PointOfView.java deleted file mode 100644 index d3b502f3dc..0000000000 --- a/OsmAnd/src/com/osmand/views/PointOfView.java +++ /dev/null @@ -1,79 +0,0 @@ -package com.osmand.views; - -import android.content.Context; -import android.graphics.Canvas; -import android.graphics.Color; -import android.graphics.Paint; -import android.util.AttributeSet; -import android.view.View; - -public class PointOfView extends View { - private Paint location; - private Paint area; - - - private int areaRadius = 0; - private int locationX = -1; - private int locationY = -1; - - public PointOfView(Context context, AttributeSet attrs) { - super(context, attrs); - initUI(); - } - - public PointOfView(Context context) { - super(context); - initUI(); - } - - private void initUI() { - location = new Paint(); - location.setColor(Color.BLUE); - location.setAlpha(150); - location.setAntiAlias(true); - - area = new Paint(); - area.setColor(Color.BLUE); - area.setAlpha(40); - } - - - @Override - protected void onDraw(Canvas canvas) { - if(locationX >= 0 && locationY >=0){ - canvas.drawCircle(locationX, locationY, 4, location); - } - if(areaRadius > 4){ - canvas.drawCircle(locationX, locationY, areaRadius, area); - } - } - - public void setLocationX(int locationX) { - this.locationX = locationX; - } - - public void setLocationY(int locationY) { - this.locationY = locationY; - } - - public int getLocationX() { - return locationX; - } - - public int getLocationY() { - return locationY; - } - - public int getAreaRadius() { - return areaRadius; - } - - public boolean isVisible() { - return locationX >= 0 && locationY >= 0; - } - - public void setAreaRadius(int areaRadius) { - this.areaRadius = areaRadius; - } - -}