0

NETWORK PROVIDERを使用して緯度と経度の値を検索し、静的IPを使用してSERVERとMYSQLDATABASEに保存するAndroidコードがあります。これは正常に機能します。

次に、MYSQLデータベースから緯度と経度の値を取得し、AndroidのGoogleマップに表示するクライアントアプリケーションを作成する必要があります。どうやってするの。私は過去4日間これに夢中になっています。解決策を見つけるのを手伝ってください

以下は私のコードです

public class MainActivity extends Activity {

Location location;
protected LocationManager locationManager;
private LocationListener ll ;
private TextView tv ;
EditText msgTextField;
int delay = 1000;
int period=10000;


/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {

    boolean gpsEnabled = true;
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
   // AlarmManager manager=(AlarmManager)getSystemService(Context.ALARM_SERVICE);
    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
    tv= new TextView (this);
    try {
    gpsEnabled =locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
  //  Toast.makeText(MainActivity.this,"ggg ",Toast.LENGTH_LONG).show();
    }
    catch (Exception e)
    {

    }
    locationManager. requestLocationUpdates( LocationManager.NETWORK_PROVIDER, 20000, 5, new MyLocationListener()); 
    Log.d("gps","Gps supports altitude"+ locationManager.getProvider(LocationManager.NETWORK_PROVIDER).supportsAltitude());
    List<String> providers=locationManager.getAllProviders();
    for(String provider : providers)
    {
       Toast.makeText(MainActivity.this,provider,Toast.LENGTH_LONG).show();
    }
    for(int w=0;w <providers.size();w++)
    {
    Log.d("Location Test ","provider "+w +":"+providers.get(w));

    }
    providers =locationManager.getProviders(true);

    final Button send = (Button) this.findViewById(R.id.sendButton);

    send.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {

            showCurrentLocation();


        }
    });

   Button send1 = (Button) this.findViewById(R.id.sendButton1);

    send1.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {

            Intent viewIntent =new Intent(MainActivity.this,Mapview.class);
            startActivity(viewIntent);

        }
    });


  Button start=(Button) this.findViewById(R.id.startSercice1);


start.setOnClickListener(new View.OnClickListener() {

    public void onClick(View arg0) {
        startService(new Intent(MainActivity.this,SimpleService.class));    

    }
});
  Button stop=(Button) this.findViewById(R.id.stopService1);
  stop.setOnClickListener(new View.OnClickListener() {

public void onClick(View v) {
    stopService(new Intent(MainActivity.this,SimpleService.class));

}
 });
 } 

protected void showCurrentLocation() 
    { 

    Location location = locationManager.getLastKnownLocation( LocationManager.NETWORK_PROVIDER);

    if (location != null) 
    {

   // double dd = location.getAltitude();
    String message = String.format( "Current Location \n Longitude: %1$s \n Latitude: %2$s  ",location.getLongitude(), location.getLatitude() );
    Toast.makeText(MainActivity.this, message,Toast.LENGTH_LONG).show();
    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost("http://xx.xx.xx.xx/YourPhpScript1.php");
    try {
       List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
       nameValuePairs.add(new BasicNameValuePair("id", "12345"));
       nameValuePairs.add(new BasicNameValuePair("message", message));
       httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
       httpclient.execute(httppost);

     }
 catch (ClientProtocolException e)
    {

    } 
 catch (IOException e)
   {

   }


    }

    }

    private class MyLocationListener implements LocationListener 
    {

    public void onLocationChanged(final Location location)
    { 

        final String message = String.format( "New Location \n Longitude: %1$s \n Latitude: %2$s  ",location.getLongitude(), location.getLatitude());
        Toast.makeText(MainActivity.this, message, Toast.LENGTH_LONG).show();

         HttpClient httpclient = new DefaultHttpClient();
         HttpPost httppost = new HttpPost("http://xx.xx.xx.xx/YourPhpScript1.php");
        double LONGITUDE = location.getLongitude();
        double LATITUDE = location.getLatitude();
        TextView myAddress = (TextView)findViewById(R.id.myaddress);

        try
        {
          Geocoder geocoder = new Geocoder(MainActivity.this, Locale.ENGLISH);
          List<Address> addresses = geocoder.getFromLocation(LATITUDE, LONGITUDE, 1);

          if(addresses != null) {
           Address returnedAddress = addresses.get(0);
           StringBuilder strReturnedAddress = new StringBuilder("Address:\n");
           for(int i=0; i<returnedAddress.getMaxAddressLineIndex(); i++) {
            strReturnedAddress.append(returnedAddress.getAddressLine(i)).append("\n");
            String add = strReturnedAddress.toString();
            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
           nameValuePairs.add(new BasicNameValuePair("id", "12345"));
           nameValuePairs.add(new BasicNameValuePair("message", add));
           httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
           httpclient.execute(httppost);
           }
          myAddress.setText(strReturnedAddress.toString());
          }

          else
          {
           myAddress.setText("No Address returned!");
          }



        }
     catch (IOException e) 
     {
          // TODO Auto-generated catch block
          e.printStackTrace();
          myAddress.setText("Canont get Address!");
     }

  //  double ddd =location.getAltitude();


//   pendingIntent =PendingIntent.getService(MainActivity.this,1,intent,1);
        final String message1 = String.format( "New Location \n Longitude: %1$s \n Latitude: %2$s  ",location.getLongitude(), location.getLatitude());
        Toast.makeText(MainActivity.this, message1, Toast.LENGTH_LONG).show();

    new Timer().schedule(new TimerTask()
    {

    public void run()
    { 

    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost("http://xx.xx.xx.xx/YourPhpScript1.php");
 try { 

       List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
       nameValuePairs.add(new BasicNameValuePair("id", "12345"));
       nameValuePairs.add(new BasicNameValuePair("message", message));
       httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

  //manager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()+200000, pendingIntent);
       httpclient.execute(httppost);

     }

 catch (ClientProtocolException e)
    {

    } 
 catch (IOException e)
   {

   }

  }
  },60000);



  }

    public void onStatusChanged(String s, int i, Bundle b) 
    { 
    Toast.makeText( MainActivity.this, "Provider status changed",Toast.LENGTH_LONG).show(); 
    } 
    public void onProviderDisabled(String s)
    { 
    Toast.makeText( MainActivity.this,"Provider disabled by the user. GPS turned off",Toast.LENGTH_LONG).show();

    }

    public void onProviderEnabled(String s)
    { 
    Toast.makeText( MainActivity.this,"Provider enabled by the user",Toast.LENGTH_LONG).show();
    }

    }




}
4

1 に答える 1

0

AsyncTaskを使用してデータベースから緯度と経度を取得する必要があります。その後、webViewを使用して地図とマーカーを表示できます。

https://developers.google.com/maps/articles/android_v3

于 2012-12-13T16:25:44.703 に答える