Я создаю простое приложение, которое отображает заданную геолокацию на карте, нажав кнопку «Открыть», и теперь я пытаюсь получить текущие значения широты и долготы, нажав кнопку «locateme», установите их в EditText
Я попытался создать функцию для получения текущей широты и долготы в MainActivity, но не работает, и я попытался сделать то же самое в MapsActivity, но не работает
package com.example.aufgabe2;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.provider.MediaStore;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.InputType;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GoogleApiAvailability;
import com.google.android.gms.maps.LocationSource;
public class MainActivity extends AppCompatActivity implements LocationListener {
Button Open, locateme;
EditText latitude, longitude;
Double longitude_current;
Double latitude_current;
LocationManager locationManager;
String[] items_names = {"Munich", "Tunis", "Barcelone", "Frankfurt", "Wien", "paris", "london", "dubai"};
int[] Images = {R.drawable.munich, R.drawable.tunis, R.drawable.barcelona, R.drawable.frank, R.drawable.wien, R.drawable.paris, R.drawable.london, R.drawable.dubai};
String[] items_lati = {"48.13743", "36.8065", "41.3851", "50.110924", "48.210033", "48.8566", "51.5074", "25.2048"};
String[] items_lng = {"11.57549", "10.1815", "2.1734", "8.682127", "16.363449", "2.3522", "0.1278", "55.2708"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ListView listView = (ListView) findViewById(R.id.listView);
CustomAdapter customAdapter = new CustomAdapter();
listView.setAdapter(customAdapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
latitude.setText(items_lati[position]);
longitude.setText(items_lng[position]);
}
});
if (googleServiceAvailable()) {
Toast.makeText(this, "PERFEKT ", Toast.LENGTH_LONG).show();
init();
}
latitude = (EditText) findViewById(R.id.Latitude);
longitude = (EditText) findViewById(R.id.Longitude);
Open = (Button) findViewById(R.id.button);
locateme = (Button) findViewById(R.id.button_locate_me);
latitude.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
longitude.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
//Open button for displaying the Map
Open.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//condition here
if (longitude.length() == 0) {
longitude.setError("your longitude is empty");
} else if (latitude.length() == 0) {
latitude.setError("your latitude is empty");
} else {
Toast.makeText(MainActivity.this, "values added successfully ", Toast.LENGTH_SHORT).show();
init();
}
}
});
// here is my locateme button
locateme.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
if (ActivityCompat.checkSelfPermission(MainActivity.this, android.Manifest.permission.ACCESS_FINE_LOCATION)
!= PackageManager.PERMISSION_GRANTED
&& ActivityCompat.checkSelfPermission(MainActivity.this, android.Manifest.permission.ACCESS_COARSE_LOCATION)
!= PackageManager.PERMISSION_GRANTED) {
}
Location location = locationManager.getLastKnownLocation(locationManager.NETWORK_PROVIDER);
onLocationChanged(location);
}
});
}
public void init(){
Open=(Button) findViewById(R.id.button);
Open.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, MapsActivity.class);
Double lati =Double.parseDouble(latitude.getText().toString());
Double longi =Double.parseDouble(longitude.getText().toString());
intent.putExtra("longitude",longi);
intent.putExtra("latitude",lati);
startActivity(intent);
}
});
}
//testing the availability of google service
public boolean googleServiceAvailable(){
GoogleApiAvailability api = GoogleApiAvailability.getInstance();
int isavailable = api.isGooglePlayServicesAvailable(this);
if (isavailable == ConnectionResult.SUCCESS){
return true;
}else if (api.isUserResolvableError(isavailable)){
Dialog dialog = api.getErrorDialog(this,isavailable,0);
dialog.show();
}else{
Toast.makeText(this,"Cant connect to play services ", Toast.LENGTH_LONG).show();
}
return false;
}
@Override
public void onLocationChanged(Location location) {
Toast.makeText(this,"Cant connect your current location ", Toast.LENGTH_LONG).show();
latitude_current=location.getLatitude();
longitude_current=location.getLongitude();
latitude.setText(String.valueOf(latitude_current));
longitude.setText(String.valueOf(longitude_current));
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
}
class CustomAdapter extends BaseAdapter {
@Override
public int getCount() {
return Images.length;
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
view = getLayoutInflater().inflate(R.layout.mylist, null);
ImageView imageView = (ImageView) view.findViewById(R.id.imageView);
TextView textView_name = (TextView) view.findViewById(R.id.textView_name);
imageView.setImageResource(Images[i]);
textView_name.setText(items_names[i]);
return view;
}
}
}
Я ожидаю, что кнопка "locateme" получит текущую долготу и широту и отобразит их в EditText