我正在尝试在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;
}
}
答案 0 :(得分:0)
我怀疑你的代码不能正常工作,因为你在主线程上进行网络操作然后捕获所有异常,所以实际发生的事情是这样的......
NetworkOnMainThreadException
。NetworkOnMainThreadException
被catch (Exception e)
代码块捕获并忽略。return 0
。要考虑Android开发的一些好方法:
Exception
。