TMapView std::vector<TMapMarker *>

This is the forum for miscellaneous technical/programming questions.

Moderator: 2ffat

Post Reply
Lena
BCBJ Master
BCBJ Master
Posts: 696
Joined: Sun Feb 06, 2011 1:28 pm

TMapView std::vector<TMapMarker *>

Post by Lena »

Hi.
I want remove old marker from TMapView (my name MapPiter) and set new one. Help please add C++ code.

Code: Select all

private:	// User declarations
	std::vector<TMapMarker *> FMarkers;

public:		// User declarations
	__fastcall TForm1(TComponent* Owner);
	void __fastcall ShowStateMarker(double x, double y, String s);


//cpp
void __fastcall TForm1::ShowStateMarker(double x, double y, String s)
{

//how conver in C++ ???
//I need remove old marker from TMapView and after call FMarkers.clear()
//	var
//	Marker: TMapMarker;
//	begin
//	for Marker in FMarkers do
//	Marker.Remove;
//	FMarkers.Clear;
//	end;

//new marker
	TMapCoordinate Position;
	Position.Latitude = x;
	Position.Longitude = y;
	TMapMarkerDescriptor myMarker = TMapMarkerDescriptor::Create(Position, s);
	myMarker.Draggable = false;
	myMarker.Visible = true;
	FMarkers.push_back(MapPiter->AddMarker(myMarker));
}

//---------------------------------------------------------------------------

void __fastcall TForm1::ComboBoxStateChange(TObject *Sender)
{
 if(ComboBoxState->ItemIndex != -1)
 {
  if(ComboBoxState->Selected->Text == L"Alabama")
   {
	MapPiter->Location = TMapCoordinate::Create(32.318231, -86.902298);
	MapPiter->Zoom = 4;
	ShowStateMarker(32.318231, -86.902298, L"Alabama");
   }
   else if(ComboBoxState->Selected->Text == L"Alaska")
	 {
	  MapPiter->Location = TMapCoordinate::Create(64.200841, -149.493673);
	  MapPiter->Zoom = 4;
	  ShowStateMarker(64.200841, -149.493673, L"Alaska");
	 }
 }
}

I found pascal code here:
Working with TMapView on iOS and Android
Lena
BCBJ Master
BCBJ Master
Posts: 696
Joined: Sun Feb 06, 2011 1:28 pm

Re: TMapView std::vector<TMapMarker *>

Post by Lena »

It seems to work:

Code: Select all

//Remove all markers from the map
 TMapMarker * Marker;
 int vector_size = FMarkers.size();
    for (int i = 0; i < vector_size; i++)
    {
     Marker = FMarkers[i];
     Marker->Remove();
    }
 FMarkers.clear()

rlebeau
BCBJ Author
BCBJ Author
Posts: 1716
Joined: Wed Jun 01, 2005 3:21 am
Location: California, USA
Contact:

Re: TMapView std::vector<TMapMarker *>

Post by rlebeau »

Lena wrote:It seems to work
Yes, that will work, though note that std::vector::size() does not return an int. It returns a std::vector::size_type (which is typically std::size_t), eg:

Code: Select all

size_t vector_size = FMarkers.size();
// better: auto vector_size = FMarkers.size();
for (int i = 0; i < vector_size; ++i)
{
    TMapMarker *Marker = FMarkers[i];
    Marker->Remove();
}
FMarkers.clear();
A range-based for loop would be closer to the original Pascal code, eg:

Code: Select all

for(TMapMarker *Marker : FMarkers)
{
    Marker->Remove();
}
FMarkers.clear();
Which is just a cleaner way of using an iterator loop, eg:

Code: Select all

for(auto iter = FMarkers.begin(), end = FMarkers.end(); iter != end; ++iter)
{
    TMapMarker *Marker = *iter: 
    Marker->Remove();
}
FMarkers.clear();
Or, you could use the std::for_each() algorithm, eg:

Code: Select all

#include <algorithm>

std::for_each(FMarkers.begin(), FMarkers.end(),
    [](TMapMarker *Marker){ Marker->Remove(); }
);
FMarkers.clear();
Remy Lebeau (TeamB)
Lebeau Software
Lena
BCBJ Master
BCBJ Master
Posts: 696
Joined: Sun Feb 06, 2011 1:28 pm

Re: TMapView std::vector<TMapMarker *>

Post by Lena »

Thank you very much for the detailed information!
Post Reply