[英]Problems with Geocoder in Java Swing
下午好,我正在為Java Desktop中的學術項目構建系統。 在這個項目中,我需要接收一個地址(例如“紐約第五大街”),並將該地址轉換為地理坐標(緯度和經度),以將其存儲在數據庫中。 這些坐標將顯示在地圖上。
我正在使用JxMaps API,但無法適應它們提供的示例Geocoder代碼。 不管我做什么,代碼都行不通。 但是,在示例中,它可以工作。
遵循我的代碼中的一個片段(在包模型中):
package model;
import com.teamdev.jxmaps.GeocoderCallback;
import com.teamdev.jxmaps.GeocoderRequest;
import com.teamdev.jxmaps.GeocoderResult;
import com.teamdev.jxmaps.GeocoderStatus;
import com.teamdev.jxmaps.InfoWindow;
import com.teamdev.jxmaps.LatLng;
import com.teamdev.jxmaps.Map;
import com.teamdev.jxmaps.Marker;
import com.teamdev.jxmaps.swing.MapView;
import dao.tableMapa;
public class Geocoder extends MapView {
private boolean performGeocode(String text) {
public boolean s;
// Getting the associated map object
final Map map = getMap();
// Creating a geocode request
GeocoderRequest request = new GeocoderRequest();
// Setting address to the geocode request
request.setAddress(text);
// Geocoding position by the entered address
getServices().getGeocoder().geocode(request, new GeocoderCallback(map) {
@Override
public void onComplete(GeocoderResult[] results, GeocoderStatus status) {
// Checking operation status
boolean r=false;
if ((status == GeocoderStatus.OK) && (results.length > 0)) {
// Getting the first result
GeocoderResult result = results[0];
// Getting a location of the result
LatLng location = result.getGeometry().getLocation();
tableMapa p=new tableMapa();
r = p.Geocodification(location, text);
}
}
});
}
}
這是在Dao Package上的:
package dao;
import java.sql.Connection;
import java.util.ArrayList;
import javax.swing.JOptionPane;
import com.teamdev.jxmaps.LatLng;
import model.ModeloMySql;
import model.pontoColeta;
import controller.fabricaConexao;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class tableMapa {
private ModeloMySql modelo = null;
public tableMapa() {
modelo = new ModeloMySql();
modelo.setDatabase("projeto");
modelo.setServer("localhost");
modelo.setUser("root");
modelo.setPassword("1234");
modelo.setPort("3306");
}
public boolean Geocodification(LatLng location, String address){
Boolean status = true;
Connection con;
con = fabricaConexao.getConnection(modelo);
if (con == null) {
JOptionPane.showMessageDialog(null, "Failed to connect");
}
//String sql = "UPDATE TABLE set(latitude="+location.getLat()+",longitude="+location.getLng()+" from pontocoleta where address="+address+";";
String sql = "UPDATE pontoColeta set latitude = ? ,longitude = ? from pontocoleta where address = ?";
try {
PreparedStatement statement = con.prepareStatement(sql);
statement.setDouble(1, location.getLat());
statement.setDouble(2, location.getLng());
statement.setString(3, address);
statement.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
fabricaConexao.closeConnection(con);
return false;
}
return true;
}
public ArrayList<pontoColeta> pontosSelect() {
Boolean status = true;
Connection con;
con = fabricaConexao.getConnection(modelo);
// testa se conseguiu conectar
if (con == null) {
JOptionPane.showMessageDialog(null, "Failed to connect");
}
ArrayList<pontoColeta> pontos = new ArrayList<>();
String sql = "SELECT * from pontocoleta";
pontoColeta p;
try {
PreparedStatement statement = con.prepareStatement(sql);
ResultSet result = statement.executeQuery();
while(result.next()){
p = new pontoColeta();
LatLng c = new LatLng(result.getDouble("latitude"), result.getDouble("longitude"));
p.setCoordinates(c);
p.setIdPonto(result.getInt("idPonto"));
p.setDescription(result.getString("description"));
p.setAddress(result.getString("address"));
p.setPhone(result.getString("phone"));
pontos.add(p);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
status = false;
} finally {
// Fecha a conexao
fabricaConexao.closeConnection(con);
}
return pontos;
}
}
我不能使它工作。 請幫我!!
所提供的代碼不包含您要調用performGeocode
的部分。 請在MapView
完全初始化之后檢查它是否已調用。 我建議在onMapReady
事件上調用它。 請看下面提供的示例:
MapView mapView = new MapView();
mapView.setOnMapReadyHandler(new MapReadyHandler() {
@Override
public void onMapReady(MapStatus status) {
...
performGeocode(...);
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.