【问题标题】:emulator show only one marker模拟器只显示一个标记
【发布时间】:2014-11-02 20:35:01
【问题描述】:

我正在尝试在 Google 地图上添加多个标记,但模拟器只显示一个标记! 这是我的代码部分: 我正在从数据库中获取 lat 和 lng 的值

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONObject;

import android.content.Intent;
import android.graphics.Color;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.Menu;
import android.widget.TextView;
import android.widget.Toast;

import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.GoogleMap.OnMapClickListener;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
 import com.google.android.gms.maps.model.MarkerOptions;
import com.google.android.gms.maps.model.PolylineOptions;


public class MapActivity extends FragmentActivity {

GoogleMap map;
ArrayList<LatLng> markerPoints;
ArrayList<LatLng> marker;
ArrayList<LatLng> markers;
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.map);

    // Initializing 
    markerPoints = new ArrayList<LatLng>();
    marker = new ArrayList<LatLng>();
    markers = new ArrayList<LatLng>();
    // Getting reference to SupportMapFragment of the activity_main
    SupportMapFragment fm = (SupportMapFragment)getSupportFragmentManager().
     findFragmentById(R.id.map);

    // Getting Map for the SupportMapFragment
    map = fm.getMap();      

    if(map!=null){

        // Enable MyLocation Button in the Map
        map.setMyLocationEnabled(true);     


        long time = System.currentTimeMillis()/1000;
       String Timestemp = Timestemp();
        int locationCount =locationCount() ;


         double Latt = Lat();
         double Lngg = Lng();

             markers.add(new LatLng(Latt, Lngg));               

                // Creating MarkerOptions
                MarkerOptions options = new MarkerOptions();

                // Setting the position of the marker
                options.position(new LatLng(Latt, Lngg));
                if(markers.size()>=1){

                    for(int i=0;i<locationCount;i++)
                     {
                        options.icon(BitmapDescriptorFactory.
                         defaultMarker(BitmapDescriptorFactory.HUE_GREEN));
                }
                options.title(time+"");         
                options.snippet(Timestemp+" last updated");
                // Add new marker to the Google Map Android API V2
                map.addMarker(options);

    }
    }
}






 public static String convertStreamToString(InputStream is) 
    {
     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();
      }
     }
     return sb.toString();
    }

 public static double Lng()
 {
    try
    {
        DefaultHttpClient httpClient=new DefaultHttpClient();

        //Connect to the server
        HttpGet httpGet=new HttpGet("http://10.0.2.2:51220/Service1.svc/Lng");
      //Get the response
      HttpResponse httpResponse = httpClient.execute(httpGet);
            HttpEntity httpEntity = httpResponse.getEntity();
         InputStream stream=httpEntity.getContent();

         //Convert the stream to readable format
            String result= convertStreamToString(stream);

            if(result.charAt(1)=='1')
            {
             return 1;
            }
            else
            {
             return 0;
            }
     }
     catch(Exception e)
     {
      return 0;
     }

    }
 public static double Lat()
 {
    try
    {
        DefaultHttpClient httpClient=new DefaultHttpClient();

        //Connect to the server
        HttpGet httpGet=new HttpGet("http://10.0.2.2:51220/Service1.svc/Lat");
      //Get the response
      HttpResponse httpResponse = httpClient.execute(httpGet);
            HttpEntity httpEntity = httpResponse.getEntity();
         InputStream stream=httpEntity.getContent();

         //Convert the stream to readable format
            String result= convertStreamToString(stream);

            if(result.charAt(1)=='1')
            {
             return 1;
            }
            else
            {
             return 0;
            }
     }
     catch(Exception e)
     {
      return 0;
     }

    }

 public static int locationCount()
 {
    try
    {
        DefaultHttpClient httpClient=new DefaultHttpClient();

        //Connect to the server
        HttpGet httpGet=new HttpGet("http://10.0.2.2:51220/Service1.svc/locationCount");
      //Get the response
      HttpResponse httpResponse = httpClient.execute(httpGet);
            HttpEntity httpEntity = httpResponse.getEntity();
         InputStream stream=httpEntity.getContent();

         //Convert the stream to readable format
            String result= convertStreamToString(stream);

            if(result.charAt(1)=='1')
            {
             return 1;
            }
            else
            {
             return 0;
            }
     }
     catch(Exception e)
     {
      return 0;
     }

    } 

         public static String Timestemp()
 {
    try
    {
        DefaultHttpClient httpClient=new DefaultHttpClient();

        //Connect to the server
        HttpGet httpGet=new HttpGet("http://10.0.2.2:51220/Service1.svc/Timestemp");
      //Get the response
      HttpResponse httpResponse = httpClient.execute(httpGet);
            HttpEntity httpEntity = httpResponse.getEntity();
         InputStream stream=httpEntity.getContent();

         //Convert the stream to readable format
            String result= convertStreamToString(stream);

            if(result.charAt(1)=='1')
            {
             return "1";
            }
            else
            {
             return "0";
            }
     }
     catch(Exception e)
     {
      return "0";
     }

    } 


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

【问题讨论】:

    标签: android google-maps


    【解决方案1】:

    我怀疑您的代码无法正常工作,因为您在主线程上执行网络操作,然后捕获所有异常,所以实际上发生的事情是这样的......

    1. 网络操作抛出NetworkOnMainThreadException
    2. NetworkOnMainThreadException 被您的 catch (Exception e) 代码块捕获并忽略。
    3. 网络方法总是来自 catch 块的return 0

    需要考虑的一些 Android 开发良好实践:

    1. 切勿在主线程上执行网络操作。
    2. 永远不要抓到Exception
    3. 在必要时通过日志记录和合理的错误向用户正确处理异常,这也将帮助您调试错误。

    【讨论】:

    • 即使显示地图也没有任何错误,但我想要多个标记它只显示一个标记:(
    • 没有网络操作如何通过网络服务从数据库中获取值??
    猜你喜欢
    • 1970-01-01
    • 2018-01-09
    • 2013-02-15
    • 2012-11-07
    • 1970-01-01
    • 1970-01-01
    • 2012-04-21
    • 2014-06-23
    • 1970-01-01
    相关资源
    最近更新 更多