我在如何在地图中显示/绘制位置时遇到问题。我想在每个特定的位置放置一个标记。我创建了sqlite数据库来存储我的数据(MallName,地址,纬度和经度)。一切正常。它从我的sqlite返回值。我用Log测试了它。参见MallMarkers.java
也许我在使用MarkerOptions时犯了一个错误。请帮我。 这是我的代码。
类GoogleMapsApiActivity.java
public class GoogleMapsApiActivity extends FragmentActivity implements LocationListener{
private ArrayList<MallMarkers> malls = new ArrayList<MallMarkers>();
private LocationManager locationManager;
private GoogleMap googleMap;
private static final String DEBUG_TAG = "GoogleMapsApiActivity";
private Location myLocation = null;
private UiSettings mapInterface;
private boolean zoom = true;
private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10;
private static final long MIN_TIME_BW_UPDATES = 1000*60*1;
private DataSource datasource;
private SQLiteDatabase database;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.map_api);
zoom = true;
GooglePlayServicesUtil.isGooglePlayServicesAvailable(getApplicationContext());
}
private void setUpMapIfNeeded() {
if (googleMap == null) {
googleMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
if (googleMap != null) {
setUpMapSetings();
}
}
}
private void setUpMapSetings(){
SharedPreferences sharedMapSettings = PreferenceManager.getDefaultSharedPreferences(this);
String mapViewLayout = sharedMapSettings.getString("map_layer", "Normal");
Boolean mapTraffic = sharedMapSettings.getBoolean("key_mapTraffic", false);
Boolean mapCompass = sharedMapSettings.getBoolean("key_mapCompass", true);
boolean mapMyLocation = sharedMapSettings.getBoolean("key_mapMyLocation", true);
boolean mapButtonZoom = sharedMapSettings.getBoolean("key_mapButtonZoom", true);
mapInterface = googleMap.getUiSettings();
//plotMalls();
if(mapViewLayout.equals("Normal")){
googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
}
else if(mapViewLayout.equals("Hybrid")){
googleMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
}
else if(mapViewLayout.equals("Satellite")){
googleMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
}
else if(mapViewLayout.equals("Terrain")){
googleMap.setMapType(GoogleMap.MAP_TYPE_TERRAIN);
}
googleMap.setTrafficEnabled(mapTraffic);
mapInterface.setCompassEnabled(mapCompass);
mapInterface.setMyLocationButtonEnabled(mapMyLocation);
mapInterface.setZoomControlsEnabled(mapButtonZoom);
googleMap.getUiSettings().setAllGesturesEnabled(true);
googleMap.setMyLocationEnabled(true);
}
//I used cursor to query my data from sqlite (Works fine)
private void plotLocationMallMapsUI(){
database=openOrCreateDatabase(com.example.sqlite.MySQLiteHelper.DATABASE_NAME, Context.MODE_PRIVATE, null);
String sqlcommand = "SELECT MallName, MallStreetAddress, MallCityAddress, MallContactNumber, MallLatitude, MallLongitude FROM mallsinfo";
Cursor cursor = database.rawQuery(sqlcommand, null);
malls = new ArrayList<MallMarkers>();
if(cursor.moveToFirst()){
do{
MallMarkers markers = new MallMarkers(cursor.getString(0),
cursor.getString(1),
cursor.getString(2),
cursor.getString(3),
cursor.getString(4),
cursor.getString(5), this);
malls.add(markers);
}while(cursor.moveToNext());
}
cursor.close();
database.close();
}
@Override
public void onRestart() {
super.onRestart();
setUpMapSetings();
}
@Override
public void onResume() {
super.onResume();
setUpMapIfNeeded();
plotLocationMallMapsUI();
}
@Override
public void onPause() {
locationManager.removeUpdates(this);
super.onPause();
}
@Override
public void onLocationChanged(Location location) {
LatLng latlng = new LatLng(location.getLatitude(), location.getLongitude());
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
provider = locationManager.getBestProvider(criteria, false);
location = locationManager.getLastKnownLocation(provider);
double lat = (double) (location.getLatitude());
double lng = (double) (location.getLongitude());
Log.d(DEBUG_TAG, "Latitude: " + String.valueOf(lat));
Log.d(DEBUG_TAG, "Longitude: " + String.valueOf(lng));
}
@Override
public void onProviderDisabled(String provider) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
}
它从sqlite返回值。我用Log测试了它。但它没有显示图。请帮帮我。如何使用标记显示它?
Class MallMarkers.java
public class MallMarkers {
private String mMallId = "";
private String mMallName = "";
private String mMallStreetAddress = "";
private String mMallCityAddress = "";
private String mMallContactNumber = "";
private String mMallLatitude = "";
private String mMallLongitude = "";
private Context mContext;
private LatLng mMallCoordinates = null;
private MarkerOptions mMallMarkerOptions=null;
private Double mLatitude;
private Double mLongitude;
private GoogleMap googlemap;
public MallMarkers(String mallname, String mallstreetaddress, String mallcityaddress, String mallcontactnumber, String malllatitude, String malllongitude, Context context){
mContext = context;
mMallName = mallname;
mMallStreetAddress = mallstreetaddress;
mMallCityAddress = mallcityaddress;
mMallContactNumber = mallcontactnumber;
mMallLatitude = malllatitude;
mMallLongitude = malllongitude;
mLatitude = Double.parseDouble(mMallLatitude);
mLongitude = Double.parseDouble(mMallLongitude);
String FilterMallName;
String FilterMallStreetAddress;
String FilterMallCityAddress;
String FilterMallContacts;
if(mMallName.equals("")){
FilterMallName = "Blank";
}else{
FilterMallName = mMallName;
}
if(mMallStreetAddress.equals("")){
FilterMallStreetAddress = "Blank";
}else{
FilterMallStreetAddress = mMallStreetAddress;
}
if(mMallCityAddress.equals("")){
FilterMallCityAddress = "Blank";
}else{
FilterMallCityAddress = mMallCityAddress;
}
if(mMallContactNumber.equals("")){
FilterMallContacts = "Blank";
}else{
FilterMallContacts = mMallContactNumber;
}
Log.d("TAG", FilterMallName + ", \n"
+ FilterMallStreetAddress + ", \n"
+ FilterMallCityAddress+ ", \n"
+ FilterMallContacts + "\n"
+ String.valueOf(mLatitude) + " ," + String.valueOf(mLongitude)
+ "\n------------------------\n");
mMallCoordinates = new LatLng(mLatitude, mLongitude);
mMallMarkerOptions = new MarkerOptions();
mMallMarkerOptions.position(mMallCoordinates);
mMallMarkerOptions.title(FilterMallName);
mMallMarkerOptions.snippet(FilterMallStreetAddress + ", " + FilterMallCityAddress + ", " + FilterMallContacts);
mMallMarkerOptions.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_RED));
}
}
答案 0 :(得分:1)
这是一个循环,我在地图上设置标记,它对我很有用:
for (Task tempTask : TasksListAppObj.getInstance().tasksRepository.getTasksRepository())
{
LatLng latlng = new LatLng(tempTask.getLatitude(), tempTask.getLongtitude());
if (tempTask.getStatus().contentEquals(TasksListActivity.STATUS_WAITING))
{
newmarker = map.addMarker(new MarkerOptions().position(latlng).title(tempTask.getTitle()).icon(BitmapDescriptorFactory.fromResource(R.drawable.marker_for_map_blue)));
}
else if (tempTask.getStatus().contentEquals(TasksListActivity.STATUS_IN_PROGRESS))
{
newmarker = map.addMarker(new MarkerOptions().position(latlng).title(tempTask.getTitle()).icon(BitmapDescriptorFactory.fromResource(R.drawable.marker_for_map_bordo)));
}
else if (tempTask.getStatus().contentEquals(TasksListActivity.STATUS_ON_THE_WAY))
{
newmarker = map.addMarker(new MarkerOptions().position(latlng).title(tempTask.getTitle()).icon(BitmapDescriptorFactory.fromResource(R.drawable.marker_for_map_turkiz)));
}
else if (tempTask.getStatus().contentEquals(TasksListActivity.STATUS_COMPLETE))
{
newmarker = map.addMarker(new MarkerOptions().position(latlng).title(tempTask.getTitle()).icon(BitmapDescriptorFactory.fromResource(R.drawable.marker_for_map_orange)));
}
else if (tempTask.getStatus().contentEquals(TasksListActivity.STATUS_FAILED))
{
newmarker = map.addMarker(new MarkerOptions().position(latlng).title(tempTask.getTitle()).icon(BitmapDescriptorFactory.fromResource(R.drawable.marker_for_map_purpul)));
}
}
看看它是否对你有帮助。
if语句用于更改标记图标。
您缺少的是应用于地图的addMarker
方法,以便将Marker
对象实际添加到GoogleMap
对象。
答案 1 :(得分:0)
在plotLocationMallMapsUI()
database.close()
中尝试这样的事情
for (MallMarkers mm : malls) {
Marker marker = googlemap.addMarker(new MarkerOptions().position(mm.getmalllatitude, mm.getmalllongitude));
}