0

こんにちは、ウェブサーバーの座標を利用するアプリを書いています。Web サービスから座標を取得する方法は知っていますが、座標を取得したら、それらをマップ上に常に表示するにはどうすればよいでしょうか。Location Manager からの場所が変更されたときに、Location Listener がマップ上のポイントを更新するのと似ています。Webサービスでもらえるポイントを常に更新したい。これはサービスで行われますか? もしそうなら、どのように?

        public class GeoUpdateHandler implements LocationListener {  

        @Override  
        public void onLocationChanged(Location location) {  
            ...
                GeoPoint point = new GeoPoint(lat, lng);  
            createMarker();  
            mapController.animateTo(point); // mapController.setCenter(point);  
        }  

更新: Createmarker のコード

private void createMarker() {  
        GeoPoint p = mapView.getMapCenter();  
        OverlayItem overlayitem = new OverlayItem(p, "", "");  
        itemizedoverlay.addOverlay(overlayitem);  
        mapView.getOverlays().add(itemizedoverlay);  
    }  

更新: Web サービスから座標を取得するためのコード...

HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost(".../android/serverFile.php");
        JSONObject json = new JSONObject();
        try {
            JSONArray postjson=new JSONArray();
            postjson.put(json);
            // Execute HTTP Post Request
            System.out.print(json);
            HttpResponse response = httpclient.execute(httppost);
            // for JSON:
            if(response != null)
            {
                InputStream is = response.getEntity().getContent();
                BufferedReader reader = new BufferedReader(new InputStreamReader(is));
                StringBuilder sb = new StringBuilder();
                String line = null;
                try {
                    while ((line = reader.readLine()) != null) {
                        sb.append(line + "\n");
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                } finally {
                    try {
                        is.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                String jsonStr = sb.toString();
                JSONObject jsonObj = new JSONObject(jsonStr);
                String longitudecord = jsonObj.getString("lon");
                String latitudecord = jsonObj.getString("lat");

}

タイマーで更新:

public void startService(){
    timer.scheduleAtFixedRate(new TimerTask(){
        @Override
        public void run() {
                 //Call the method that contacts the server and  
                 //adds the coordinates to a list. Get those values from the list.  
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost("http://www.kre8tiveinspired.com/android/serverFile.php");
            JSONObject json = new JSONObject();

            try {
                // JSON data:
                JSONArray postjson=new JSONArray();
                postjson.put(json);
                // Execute HTTP Post Request
                System.out.print(json);
                HttpResponse response = httpclient.execute(httppost);
                // for JSON:
                if(response != null)
                {
                    InputStream is = response.getEntity().getContent();
                    BufferedReader reader = new BufferedReader(new InputStreamReader(is));
                    StringBuilder sb = new StringBuilder();
                    String line = null;
                    try {
                        while ((line = reader.readLine()) != null) {
                            sb.append(line + "\n");
                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                    } finally {
                        try {
                            is.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                    String jsonStr = sb.toString();
                    JSONObject jsonObj = new JSONObject(jsonStr);
                    // grabbing the menu object 
                    String longitudecord = jsonObj.getString("lon");
                    String latitudecord = jsonObj.getString("lat");
                    latcord = latitudecord;
                    loncord = longitudecord;
                }

            }catch (ClientProtocolException e) {
                // TODO Auto-generated catch block
            } catch (IOException e) {
                // TODO Auto-generated catch block
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
                 Handler handler = new Handler();
                 handler.post(new Runnable(){  
                       public void run(){

                        double aDouble = Double.parseDouble(loncord);
                    double bDouble = Double.parseDouble(latcord);

                    int lat = (int) (aDouble * 1E6);  
                        int lng = (int) (bDouble * 1E6);  
                        GeoPoint point = new GeoPoint(lat, lng);  
                        createMarker();  
                        mapController.animateTo(point); // mapController.setCenter(point);
                            //add the overlays to the map
                            // and call invalidate for the mapview.
                             mapView.invalidate();

                       }
                 });
            }

    }, 0, 1000); // Here im calling the timer every 10 seconds
4

2 に答える 2

2

できることは、 a を使用しtimerてマップにデータを入力することです。基本的に、サーバーに接続し、座標を取得して配列リストに追加する 1 つのメソッドを持つことができます。これで、このメソッドをタイマー内から呼び出すことができます (必要に応じて、数秒ごとに実行できます)。また、このタイマー クラス内で、マップを再設定できます。これのためのいくつかのコード:

timer=new Timer();  // Refresh map via timer task.
    timer.scheduleAtFixedRate(new TimerTask(){
        @Override
        public void run() {
                 //Call the method that contacts the server and  
                 //adds the coordinates to a list. Get those values from the list.  
                 Handler handler = new Handler();
                 handler.post(new Runnable(){  
                       public void run(){
                            //add the overlays to the map
                            // and call invalidate for the mapview.
                       }
                 });
            }

    }, 0, 10000); // Here im calling the timer every 10 seconds

このアイデアを使用したサンプル プロジェクトを見たい場合は、ここに進み、内部のタイマーを見てくださいonResume()FetchParseこれは、サーバーに接続して座標の更新リストを取得するという別のクラスを使用します。

于 2012-04-29T05:12:47.213 に答える
0
GeoPoint p = mapView.getProjection().fromPixels((int) event.getX(),(int) event.getY());
Drawable srcdrawable = getApplicationContext().getResources().getDrawable(R.drawable.pin_blue);
CustomItemizedOverlay srcitemizedOverlay = new CustomItemizedOverlay(srcdrawable, getApplicationContext());
OverlayItem srcoverlayitem = new OverlayItem(p, "Hello!", "This is your Location.");
srcitemizedOverlay.addOverlay(srcoverlayitem);
mapView.getOverlays().clear();
mapView.getOverlays().add(srcitemizedOverlay);

作成マーカーでこのコードを試してください

于 2012-04-29T05:01:52.547 に答える