⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 unit1.cpp

📁 1.目录GPS_Bus为工程文件
💻 CPP
字号:
//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "Unit1.h"
#include "Unit3.h"
#include "Unit4.h"
#include "Unit2.h"
#include "Unit6.h"
#include "stdio.h"
#include "Unit8.h"

//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "MapObjects2_OCX"
#pragma resource "*.dfm"
TForm1 *Form1;

CInfo m_info;
CSmpp m_smpp;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
{

    IMoLabelRendererPtr lr = (IDispatch*)CreateOleObject("MapObjects2.LabelRenderer");
    IMoLayersPtr layers = Map1->Layers;
    IMoMapLayerPtr layer = layers->Item(1);
    IMoTableDescPtr tDesc;
    IMoRecordsetPtr recs = layer->Records;
    tDesc = recs->TableDesc;
    AnsiString hh = tDesc->get_FieldName(2);

    lr->Field = tDesc->get_FieldName(2);
    lr->SplinedText = true;
    layer->Renderer = lr;
    Map1->Refresh();

    Form1->Memo1->Enabled = false;
    Form1->Button3->Enabled =false;
    //iii=0;


}
//---------------------------------------------------------------------------

void __fastcall TForm1::Timer1Timer(TObject *Sender)
{
   //double maxDist;

   //timess++;

   double x = 120.1563;
   double y = 30.2609;
   double xx,yy;
   char latitude[50],longitude[50];

   strcpy(latitude,"0.00");
   strcpy(longitude,"0.00");
   long phonenumber;
   int nEventCount,iIndex;
   IMoGeoEventDisp geoEvnt;
   IMoRectangleDisp rect;
   IMoTrackingLayerDisp tLayer;

   //画线
   /*
   IMoLinePtr drawline = (IDispatch*)CreateOleObject("MapObjects2.Line");
   IMoPointsPtr pointss = (IDispatch*)CreateOleObject("MapObjects2.Points");
   IMoPointPtr point11=(IDispatch*)CreateOleObject("MapObjects2.Point");
   IMoPointPtr point22=(IDispatch*)CreateOleObject("MapObjects2.Point");
   IMoPartsPtr partss=(IDispatch*)CreateOleObject("MapObjects2.Parts");
   IMoSymbolPtr symbolss = (IDispatch*)CreateOleObject("MapObjects2.Symbol");
   symbolss->SymbolType = moLineSymbol;
   symbolss->Color = moBlue;
   */


   rect=Map1->Extent;
   //maxDist=rect.Width/20;
   tLayer=Map1->TrackingLayer;

   //nEventCount=tLayer.EventCount;
   //for (iIndex=0; iIndex<nEventCount; iIndex++)
   //{
    //  geoEvnt=(IDispatch*)tLayer->get_Event(iIndex);
    //  Form1->Table2->First();
    //  Form1->Table2->MoveBy(iIndex);
    //  phonenumber = Form1->Table2->Fields->FieldByName("手机号")->Value;
      //geoEvnt.Move(maxDist*((double)random(100)/100-0.5),maxDist*((double)random(100)/100-0.5));
      char buffer[20];
      ZeroMemory(buffer,20);
      //sprintf(buffer,"0571%ld",phonenumber);
      //bool rect= m_info.GetData(buffer,latitude,longitude);

      IMoRectangleDisp rectjinfang = CoRectangle::Create();
      IMoRectangleDisp rectliugongyuan = CoRectangle::Create();
      IMoPointDisp pointnow = CoPoint::Create();


      rectjinfang.set_Left(120.20030);
      rectjinfang.set_Right(120.20230);
      rectjinfang.set_Top(30.27400);
      rectjinfang.set_Bottom(30.27200);

      rectliugongyuan.set_Left(120.15169);
      rectliugongyuan.set_Right(120.15969);
      rectliugongyuan.set_Top(30.26499);
      rectliugongyuan.set_Bottom(30.25699);


      while( m_info.GetData(buffer,latitude,longitude))

      {
       int recordNo;
       Table2->First();
       char temp[20];
       for (int table2No=0;table2No<Table2->RecordCount;table2No++)
         {
            phonenumber = Table2->Fields->FieldByName("手机号")->Value;
            ZeroMemory(temp,20);
            sprintf(temp,"0571%ld",phonenumber);
            if (strcmp(buffer,temp)==0)
             {
              recordNo = Table2->RecNo-1;
              break;
             }
            Table2->Next();
         }
       geoEvnt=(IDispatch*)tLayer->get_Event(recordNo);
       //xx = StrToFloat(AnsiString(latitude));
       //yy = StrToFloat(AnsiString(longitude));
       yy = atof(latitude);
       xx = atof(longitude);

       pointnow.set_X(xx);
       pointnow.set_Y(yy);

       String direction = Form1->Table2->Fields->FieldByName("前进方向")->Value;
       if (direction == "起点站")
         {
         if (rectjinfang->IsPointIn((IDispatch*)pointnow))
             {
             Table2->Edit();
             Table2->Fields->FieldByName("前进方向")->Value = "终点站";
             Table2->Post();
             geoEvnt.MoveTo(120.20130,30.27300);
             }
         else geoEvnt.MoveTo(xx,yy);
         }
       else if (direction == "终点站")
         {
         if (rectliugongyuan->IsPointIn((IDispatch*)pointnow))
             {
             Table2->Edit();
             Table2->Fields->FieldByName("前进方向")->Value = "起点站";
             Table2->Post();
             geoEvnt.MoveTo(120.15569,30.26099);
             }
         else geoEvnt.MoveTo(xx,yy);

         }





       }

   //   xx = StrToFloat(AnsiString(latitude));
  //    yy = StrToFloat(AnsiString(longitude));
     //if (|xxkeep-0|<0.0001)
     /*{
      point11->set_X(120);
      point11->set_Y(31);
      point22->set_X(x);
      point22->set_Y(y);
      pointss->Add(point11);
      pointss->Add(point22);
      partss->Add(pointss);
      drawline->Parts = partss;
      Map1->DrawShape(drawline,symbolss);

     //}
      xxkeep = x;
      yykeep = y;
     */
      //m_info.GetData(phonenumber, (FloatToStr(x)).c_str(),(FloatToStr(y)).c_str());
      //JiaoZheng(x,y);
      //geoEvnt.MoveTo(xx,yy);
      //geoEvnt=NULL;

   //iii++;
   /*IMoTrackingLayerDisp tl = Map1->TrackingLayer;
   int index;
   int GeoEventCount = tl.EventCount;
   */
}
//---------------------------------------------------------------------------


void __fastcall TForm1::Map1MouseDown(TObject *Sender, TMouseButton Button,
      TShiftState Shift, int X, int Y)
{
    if (Zoomin->Down)
   {
     IMoRectanglePtr trackRect = Map1->TrackRectangle();
     Map1->Extent = trackRect;
   }
   if (Zoomout->Down)
   {
     IMoRectanglePtr extRect = Map1->Extent;
     extRect->ScaleRectangle(1.5);
     Map1->Extent = extRect;
   }
   if (Pan->Down)
      Map1->Pan();
   if (Ruler->Down)
   {
     IMoLinePtr line = Map1->TrackLine();
     StatusBar1->Panels->Items[2]->Text = "距离为:" + FloatToStr(line->Length);
   }

   if (Info->Down)
   {
       int j = -1;
       IMoTrackingLayerPtr tl = Map1->TrackingLayer;
       IMoGeoEventDisp evt;
       long GeoEventCount = tl->get_EventCount();
       IMoRectanglePtr rect = Map1->TrackRectangle();
       IMoPointDisp test = CoPoint::Create();
       for (int i=0;i<GeoEventCount;i++ )
       {
         evt =  tl->get_Event(i);
         test.X = evt.X;
         test.Y = evt.Y;
         if (rect->IsPointIn((IDispatch*)test))
          {
            j = i;
          }
        }
        if (j>=0)
        {
          Form1->Table1->Close();
          Form1->Table1->Open();
          Form1->Table1->MoveBy(j);
          String  aa = Form1->Table1->Fields->FieldByName("车号")->Value;
          String  bb = "当前车辆车号为:  "  + aa +"  !!" ;
          ShowMessage(bb);
          AnsiString message;

          Form1->Label7->Caption = Form1->Table1->Fields->FieldByName("车号")->Value;
          Form1->Label8->Caption = Form1->Table1->Fields->FieldByName("手机号")->AsString;
          Form1->Label9->Caption = Form1->Table1->Fields->FieldByName("工号")->Value;
          Form1->Label10->Caption = Form1->Table1->Fields->FieldByName("前进方向")->Value;
          Form1->Memo1->Enabled = true;
          message = Form1->Memo1->Text;
          Form1->Button3->Enabled = true;

        }
        else
            ShowMessage("该范围内无车辆行驶!!");


   }

}
//---------------------------------------------------------------------------

void __fastcall TForm1::FullClick(TObject *Sender)
{
   IMoRectanglePtr fullExt = Map1->FullExtent;
   Map1->Extent = fullExt;
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Map1MouseMove(TObject *Sender, TShiftState Shift,
      int X, int Y)
{
   if (Zoomin->Down)
      Map1->MousePointer = moZoomIn;
   if (Zoomout->Down)
      Map1->MousePointer = moZoomOut;
   if (Pan->Down)
      Map1->MousePointer = moPan;
   if (Ruler->Down)
      Map1->MousePointer = moCross;
   if (Info->Down)
      Map1->MousePointer = moArrow;

   IMoPointPtr mappoint = Map1->ToMapPoint(X,Y);
   double xmap = mappoint->X;
   double ymap = mappoint->Y;
   StatusBar1->Panels->Items[0]->Text = "控制坐标为:X" + FloatToStr(X) + "     Y" + FloatToStr(Y);
   StatusBar1->Panels->Items[1]->Text = "地理坐标为:X" + FloatToStr(xmap) + "     Y" + FloatToStr(ymap);
}
//---------------------------------------------------------------------------





void __fastcall TForm1::Button1Click(TObject *Sender)
{
  Form3->ShowModal();
}
//---------------------------------------------------------------------------

void __fastcall TForm1::N1Click(TObject *Sender)
{
  Form4->ShowModal();
}
//---------------------------------------------------------------------------


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

void __fastcall TForm1::Button2Click(TObject *Sender)
{
   Form2->ShowModal();
}
//---------------------------------------------------------------------------

void __fastcall TForm1::FormClose(TObject *Sender, TCloseAction &Action)
{
   Table1->Close();
   Table1->Open();
   int recordcount = Table1->RecordCount;
   for (int i=0;i<recordcount;i++ )
   {
        Table1->Delete();
        Table1->Next();

   }

   m_info.Release();
   m_smpp.Release();
}
//---------------------------------------------------------------------------


void __fastcall TForm1::Button3Click(TObject *Sender)
{
    AnsiString message = Form1->Memo1->Text;
    char *mess = new char[message.Length() +1];
    strcpy(mess,message.c_str());
    long phonenumber = Form1->Table1->Fields->FieldByName("手机号")->Value;
    char buffer[20];
    ZeroMemory(buffer,20);
    sprintf(buffer,"0571%ld",phonenumber);
    m_smpp.SendSM("",buffer,mess);
    Form1->Memo1->Enabled = false;
    Form1->Memo1->Clear();
    Form1->Button3->Enabled = false;

}
//---------------------------------------------------------------------------


void __fastcall TForm1::N2Click(TObject *Sender)
{
   Form8->ShowModal();
}
//---------------------------------------------------------------------------

void __fastcall TForm1::JiaoZheng(double &x,double &y)
{

   double x1,x2,y1,y2;

   IMoLinePtr line ;

   //得到行车路线
   IMoLayersPtr layers = Map1->Layers;
   IMoMapLayerPtr layer = layers->Item(0);
   IMoRecordsetPtr recs = layer->get_Records();
   IDispatch* shp;
   IMoFieldPtr field;
   IMoFieldsPtr fields = recs->Fields;
   IMoPointsPtr points;
   IMoPointPtr point;
   IMoPartsPtr parts;
   field = (IDispatch*)fields->Item("shape");
   shp = field->get_Value();
   IMoLinePtr linego = (IMoLinePtr)shp;
  // double go = linego->get_Length();
   recs->MoveNext();
   fields = recs->Fields;
   field = (IDispatch*)fields->Item("shape");
   shp = field->get_Value();
   IMoLinePtr lineback = (IMoLinePtr)shp;
   //double back = lineback->get_Length();

   //得到求交的矩形
   IMoRectanglePtr rect = (IDispatch*)CreateOleObject("MapObjects2.Rectangle");
   rect->set_Left(x-0.001);
   rect->Right = x+0.001 ;
   rect->Top = y+0.001 ;
   rect->Bottom = y-0.001 ;

   String direction = Form1->Table2->Fields->FieldByName("前进方向")->Value;
   //求交得到中点坐标

   if (direction == "起点站")
    line = linego->Intersect(rect);
   else if (direction == "终点站")
    line = lineback->Intersect(rect);
   //double aa = rect->get_Left();
   parts = line->Parts;
   points = parts->Item(0);
   point = points->Item(0);
   x1=point->get_X();
   y1=point->get_Y();
   point = points->Item(1);
   x2=point->get_X();
   y2=point->get_Y();

   x = (x1+x2)/2;
   y = (y1+y2)/2;

   //rect->Release();
   //rect->Release
   //layers->Release();
   //layer->Release();
    //rect.ReleaseDispatch();
   //fields->Release();
  // field->Release();
   //points->Release();
   //point->Release();
   //parts->Release();
   //rect->Release();
  // line1->Release();

   //StatusBar1->Panels->Items[2]->Text = "X:" + FloatToStr(x) + "     Y:" + FloatToStr(y);



}

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

void __fastcall TForm1::FormShow(TObject *Sender)
{

   IMoRectanglePtr extent = (IDispatch*)CreateOleObject("MapObjects2.Rectangle");
    extent->Top = 30.287;
    extent->Bottom = 30.242;
    extent->Left = 120.146;
    extent->Right = 120.205;
    Map1->Extent = extent;


}
//---------------------------------------------------------------------------


void __fastcall TForm1::FormCreate(TObject *Sender)
{
    //xxkeep = 0;
    //yykeep = 0;
    //timess = 0;
}
//---------------------------------------------------------------------------


void __fastcall TForm1::N3Click(TObject *Sender)
{
    AboutBox->ShowModal();
}
//---------------------------------------------------------------------------

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -