0

我有一个 android 代码,它将使用 NETWORK PROVIDER 查找纬度和经度值,并使用静态 IP 将其(纬度和经度值)存储在 SERVER 以及 MYSQL 数据库中。这工作正常..

我现在需要编写一个客户端应用程序,它从 MYSQL 数据库中检索纬度和经度值,并将其显示在 android 的谷歌地图中。怎么做。在过去的 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 回答