Java如何实现地图选择地址
目标:
实现地图选择地址
内容:
Java可以使用第三方地图开放平台提供的API,或者使用Java Swing或JavaFX框架自带的组件实现地图选择地址功能。
- 使用第三方地图开放平台API 获取一个地图开放平台的开发者账号,并使用该账号获取API密钥。根据该地图开放平台提供的API文档编写Java代码,调用API接口获取地图数据,并将地图显示在Java应用程序界面的图形组件中。例如,使用高德地图API实现的Java地图应用:
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;import javax.swing.JButton;
import javax.swing.JFrame;import com.alibaba.fastjson.JSONObject;
import com.amap.api.maps.MapView;
import com.amap.api.services.core.LatLonPoint;
import com.amap.api.services.geocoder.GeocodeSearch;
import com.amap.api.services.geocoder.GeocodeSearch.OnGeocodeSearchListener;
import com.amap.api.services.geocoder.RegeocodeResult;public class MapDemo {private JFrame frame;private MapView mapView;private JButton button;private GeocodeSearch geocoderSearch;public static void main(String[] args) {EventQueue.invokeLater(new Runnable() {public void run() {try {MapDemo window = new MapDemo();window.frame.setVisible(true);} catch (Exception e) {e.printStackTrace();}}});}public MapDemo() {initialize();}private void initialize() {frame = new JFrame();frame.setBounds(100, 100, 800, 600);frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);frame.getContentPane().setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));mapView = new MapView(frame.getContentPane().getSize());button = new JButton("选择地址");button.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {GeocodeSearch search = new GeocodeSearch(mapView.getMap());search.setOnGeocodeSearchListener(new OnGeocodeSearchListener() {public void onRegeocodeSearched(RegeocodeResult result, int rCode) {if (rCode == 1000) {JSONObject jsonObject = new JSONObject();jsonObject.put("latitude", result.getRegeocodeQuery().getPoint().getLatitude());jsonObject.put("longitude", result.getRegeocodeQuery().getPoint().getLongitude());jsonObject.put("address", result.getRegeocodeAddress().getFormatAddress());System.out.println(jsonObject.toJSONString());}}public void onGeocodeSearched(LatLonPoint arg0, int arg1) {}});search.getFromLocation(mapView.getMap().getCameraPosition().target, 300);}});frame.getContentPane().add(mapView, FlowLayout.LEFT);frame.getContentPane().add(button, FlowLayout.RIGHT);mapView.onCreate(null);geocoderSearch = new GeocodeSearch(frame);}}
- 使用Java Swing或JavaFX框架自带的组件 Java Swing和JavaFX框架都提供了内置的图形组件用于显示地图数据,例如JMapViewer和MapView。您可以使用这些组件实现地图选择地址功能。例如,使用JMapViewer实现的Java地图应用:
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;import org.openstreetmap.gui.jmapviewer.JMapViewer;
import org.openstreetmap.gui.jmapviewer.Layer;
import org.openstreetmap.gui.jmapviewer.LayerGroup;
import org.openstreetmap.gui.jmapviewer.MapMarkerDot;
import org.openstreetmap.gui.jmapviewer.MapMouseEvent;
import org.openstreetmap.gui.jmapviewer.interfaces.MapMarker;
import org.openstreetmap.gui.jmapviewer.tilesources.BingAerialTileSource;
import org.openstreetmap.gui.jmapviewer.tilesources.OsmTileSource;public class MapDemo {private JFrame frame;private JMapViewer mapViewer;private JTextField addressField;private JButton searchButton;public static void main(String[] args) {EventQueue.invokeLater(new Runnable() {public void run() {try {MapDemo window = new MapDemo();window.frame.setVisible(true);} catch (Exception e) {e.printStackTrace();}}});}public MapDemo() {initialize();}private void initialize() {frame = new JFrame();frame.setBounds(100, 100, 800, 600);frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);mapViewer = new JMapViewer();mapViewer.setZoomControlsVisible(true);mapViewer.setDisplayPositionByLatLon(39.9042, 116.4074, 10);LayerGroup mainLayerGroup = new LayerGroup("Main");Layer mainLayer = new Layer("Main");mainLayerGroup.addLayer(mainLayer);MapMarkerDot marker = new MapMarkerDot(mainLayer, 39.9042, 116.4074);marker.setName("北京");mapViewer.addMapMarker(marker);mapViewer.setMapMarkerVisible(true);mapViewer.setTileSource(new OsmTileSource.Mapnik());mapViewer.setTileSource(new BingAerialTileSource());mapViewer.addMouseListener(new JMapViewerMouseListener());frame.getContentPane().setLayout(new BorderLayout());frame.getContentPane().add(mapViewer, BorderLayout.CENTER);JPanel searchPanel = new JPanel(new FlowLayout());searchPanel.setBackground(Color.WHITE);addressField = new JTextField(20);searchButton = new JButton("搜索");searchButton.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {String address = addressField.getText();System.out.println("search address: " + address);}});searchPanel.add(addressField);searchPanel.add(searchButton);frame.getContentPane().add(searchPanel, BorderLayout.SOUTH);}private class JMapViewerMouseListener implements org.openstreetmap.gui.jmapviewer.interfaces.MapMouseListener {@Overridepublic void mapClicked(MapMouseEvent arg0) {if (arg0.getButton() == MouseEvent.BUTTON1) {double latitude = arg0.getLat();double longitude = arg0.getLon();// Remove old markerfor (MapMarker m : mapViewer.getMapMarkerList()) {if (m instanceof MapMarkerDot) {mapViewer.removeMapMarker(m);}}// Add new markerMapMarkerDot marker = new MapMarkerDot(latitude, longitude);marker.setName("选择地址");mapViewer.addMapMarker(marker);// Reverse geocodingString address = "未知地址";System.out.println("select address: (" + latitude + "," + longitude + ")");addressField.setText(address);}}@Overridepublic void mapMoved(MapMouseEvent arg0) {}}}
以上内容便是我总结的地图选择地址的相关实现代码,如有错误,请指正!
本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!
