0

新版本 1.1

嗨,大家好。我尝试遵循您的建议,现在我正在尝试重做代码。我添加了这部分代码来处理 GoogleMaps 标记的主线程。

问题是,当我调用 Ubic() 函数时,出现此错误:

http://i.imgur.com/QD7JZjRh.jpg

http://i.imgur.com/xvzJ9ckh.jpg

这是我添加的代码,错误是由 String Latlon[][] = com.Ubic();

public class BuscarContrincantes extends FragmentActivity implements
    LocationListener {

GoogleMap googlemapa;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.buscarcontrincante);

    SupportMapFragment maps = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map);

    googlemapa = maps.getMap();
    googlemapa.setMyLocationEnabled(true);
    googlemapa.setMapType(GoogleMap.MAP_TYPE_HYBRID);
    googlemapa.getUiSettings().setCompassEnabled(true);
    googlemapa.getUiSettings().setZoomControlsEnabled(true);
    googlemapa.getUiSettings().setAllGesturesEnabled(true);

    double latitud;
    double longitud;
    String nombre;

    Datos com = new Datos();

    String latlon[][] = com.Ubic(); //HERE IS THE PROBLEM!

    for (int i = 0; i < latlon.length - 1; i++) {
        latitud = Double.parseDouble(latlon[i][0]);
        longitud = Double.parseDouble(latlon[i][3]);
        nombre = (latlon[i][2]);

        LatLng posicion = new LatLng(latitud, longitud);

        googlemapa.addMarker(new MarkerOptions()
                .icon(BitmapDescriptorFactory
                        .fromResource(R.drawable.icono_canchas))
                .position(posicion).title(nombre));


}

}

版本 1.0

我正在为我的大学做的一个项目遇到问题。

实际上,我正在尝试在地图(使用谷歌地图)上设置一个标记,表明他的信息位于 MySQL 数据库中。

代码是:

BuscarContrincantes.java

public class BuscarContrincantes extends FragmentActivity implements
    LocationListener {

GoogleMap googlemapa;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.buscarcontrincante);

    SupportMapFragment maps = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map);

    googlemapa = maps.getMap();
    googlemapa.setMyLocationEnabled(true);
    googlemapa.setMapType(GoogleMap.MAP_TYPE_HYBRID);
    googlemapa.getUiSettings().setCompassEnabled(true);
    googlemapa.getUiSettings().setZoomControlsEnabled(true);
    googlemapa.getUiSettings().setAllGesturesEnabled(true);

    new RetreiveFeedTask().execute();

}

class RetreiveFeedTask extends AsyncTask<String, Void, String> {

    protected String doInBackground(String... args) {
        double lat;
        double lon;
        String nombre;

        Datos com = new Datos(); // File Datos.java
        String latlon[][] = com.Ubic();

        for (int i = 0; i < latlon.length - 1; i++) {
            lat = Double.parseDouble(latlon[i][0]);
            lon = Double.parseDouble(latlon[i][4]);
            nombre = (latlon[i][2]);

            LatLng pos3 = new LatLng(lat, lon);

            googlemapa.addMarker(new MarkerOptions()
                    .icon(BitmapDescriptorFactory
                            .fromResource(R.drawable.icono_canchas))
                    .position(pos3).title(nombre));

        }
        return null;

    }

}

@Override
public void onLocationChanged(Location arg0) {
    // TODO Auto-generated method stub

}}

达托斯.java

public class Datos {
public String[][] Ubic() {

    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost(
            "http://MY.IP/FOLDER/QUERY.php");

    try {

        HttpResponse response = httpclient.execute(httppost);
        String jsonResult = inputStreamToString(
                response.getEntity().getContent()).toString();
        JSONObject object = new JSONObject(jsonResult);


        JSONArray arr = object.getJSONArray("products");
        String[] lat = new String[arr.length()+1];
        String[] lon = new String[arr.length()+1];
        String[] nombre = new String[arr.length() + 1];

        String[][] latlon = new String[arr.length() + 1][5];

        for (int i = 0; i < arr.length(); i++) {
            lat[i] = arr.getJSONObject(i).getString("latitude");
            lon[i] = arr.getJSONObject(i).getString("longitude");
            name[i] = arr.getJSONObject(i).getString("name");
            latlon[i][0] = lat[i];
            latlon[i][6] = lon[i];
            latlon[i][2] = name[i];
        }
        return latlon;

    } catch (JSONException e) {

    } catch (ClientProtocolException e) {

    } catch (IOException e) {

    }
    return null;

}

private static String parse(String string) {
    return null;

}

private StringBuilder inputStreamToString(InputStream is) {
    String rLine = "";
    StringBuilder answer = new StringBuilder();
    BufferedReader rd = new BufferedReader(new InputStreamReader(is));

    try {
        while ((rLine = rd.readLine()) != null) {
            answer.append(rLine);
        }
    } catch (IOException e) {

    }
    return answer;
}}

此代码试图获取 MySQL 数据库上的对象的纬度、经度和名称,并且必须将此对象放在地图上,但我收到 AsynkTask #1 错误,我不知道为什么会这样。你能帮我吗?我对这段代码感到非常生气,我不知道为什么这不起作用。

PS:对不起我的英语不好。我来自智利:P

4

1 回答 1

0

您无法在doInBackground方法中访问 GoogleMap 实例。GoogleMap 只能从主线程读取和修改。只是 HTTP 请求应在后台任务上。

您需要修改doInBackground以返回坐标列表并在onPostExecute方法(在主线程)处实现 GoogleMap 更新。

以下是基于版本 1.1 的代码:

GoogleMap googlemapa;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.buscarcontrincante);

    SupportMapFragment maps = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);

    googlemapa = maps.getMap();
    googlemapa.setMyLocationEnabled(true);
    googlemapa.setMapType(GoogleMap.MAP_TYPE_HYBRID);
    googlemapa.getUiSettings().setCompassEnabled(true);
    googlemapa.getUiSettings().setZoomControlsEnabled(true);
    googlemapa.getUiSettings().setAllGesturesEnabled(true);

    new RetreiveFeedTask().execute();
}

class RetreiveFeedTask extends AsyncTask<Void, Void, String[][]> {

    @Override
    protected String[][] doInBackground(Void... args) {
        // Execute the HTTP request on background task
        Datos com = new Datos(); // File Datos.java
        return com.Ubic();
    }

    @Override
    protected void onPostExecute(String[][] latlon) {
        double latitud;
        double longitud;
        String nombre;

        // Update the UI
        for (int i = 0; i < latlon.length - 1; i++) {
            latitud = Double.parseDouble(latlon[i][0]);
            longitud = Double.parseDouble(latlon[i][3]);
            nombre = (latlon[i][2]);

            LatLng posicion = new LatLng(latitud, longitud);

            googlemapa.addMarker(new MarkerOptions().icon(BitmapDescriptorFactory.fromResource(R.drawable.icono_canchas)).position(posicion).title(nombre));

        }
    }
}
于 2013-11-06T01:00:24.557 回答