我有一个问题,就是在我的应用中从谷歌地图添加多个标记的数据列表中从数据库中获取值
当我编写这段代码时,我只得到一个标记,它是数组的最后一个地址,但我希望地图中的所有地址都带有标记。
package com.example.rajkot_tourism;
import handler.handler;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import property.property;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import android.location.Address;
import android.location.Criteria;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.Toast;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import com.google.android.gms.maps.GoogleMap;
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.MarkerOptions;
public class mainmap extends android.support.v4.app.FragmentActivity
{
ArrayList arlat=new ArrayList();
ArrayList arlang=new ArrayList();
ArrayList<String> arname=new ArrayList<String>();
private GoogleMap googlemap;
private LocationManager loc;
private MarkerOptions marker;
double lng;
double lat;
String name;
String name1;
double lat1;
double lng1;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main_map);
googlemap=((SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
Bundle b=getIntent().getExtras();
int idc=b.getInt("idc");
handler h=new handler(mainmap.this);
ArrayList<property> p=h.cat_map(idc);
for(property p1 : p)
{
double lat1=p1.getPlace_latitude();
double lng1=p1.getPlace_longitude();
String placename=p1.getPlace_name();
arname.add(placename);
arlat.add(lat1);
arlang.add(lng1);
}
Criteria criteria=new Criteria();
criteria.setBearingRequired(false);
criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setCostAllowed(true);
criteria.setAltitudeRequired(false);
criteria.setPowerRequirement(Criteria.POWER_LOW);
loc=(LocationManager)getSystemService(LOCATION_SERVICE);
String provider=loc.getBestProvider(criteria, true);
Location location=loc.getLastKnownLocation(provider);
updatewithnewlocation(location);
}
LocationListener loclist=new LocationListener() {
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
}
@Override
public void onLocationChanged(Location location) {
Toast.makeText(mainmap.this,"Location Changed", Toast.LENGTH_LONG).show();
}
};
String lastloc;
private void updatewithnewlocation(Location location) {
// for(int i=0;i<arname.size();i++)
//
// {
name=arname.get(0);
lat=(Double) arlat.get(0);
lng=(Double) arlang.get(0);
marker = new MarkerOptions().position(new LatLng(lat, lng)).title(name);
marker.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_VIOLET));
googlemap.addMarker(marker);
//}
name1=arname.get(1);
lat1=(Double) arlat.get(1);
lng1=(Double) arlang.get(1);
MarkerOptions marker1 = new MarkerOptions().position(new LatLng(lat1, lng1)).title(name1);
marker1.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_VIOLET));
googlemap.addMarker(marker1);
}
}
答案 0 :(得分:0)
在你的for循环中,当你添加所有标记时,你每次都得到相同的值而不是循环使用你的变量i。当你获得值时,这就是你正在做的事情:
arname.get(0);
你需要这样做:
arname.get(i);