通过搜索EditText并单击地图,获取相同位置的不同纬度和经度值

时间:2015-08-10 11:24:15

标签: android google-maps

    public class AddNewLocationActivity extends Activity {
    GoogleMap googleMap;
    EditText edtLocation;
    Marker marker;
    List<Address> addressList = null;
    VibRIngDatabase vibRIngDatabase;
    double latitude, longitude;
    Geocoder geocoder;
    LatLng latLng;
    String locationAddress, name, mode;
    AlertDialog.Builder dialogBuilder;
    double lati1, longi1;
    TextView lat, longi, location_name;
    RadioButton selectedModeRadioButton;
    RadioGroup rg;
    Button setMode;
    int selectedId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_addnewlocation);
        createMapView();// Rendering the Google Map in the fragment
        addMarkerAtCurrent(); // Adding marker at the current location of the device
        mapClick(); // On clicking the map
        vibRIngDatabase = new VibRIngDatabase(this);
    }

    //Display the Google Map inside the fragment
    private void createMapView() {
           try {
            if (googleMap == null) {
                googleMap = ((MapFragment) getFragmentManager().findFragmentById(
                        R.id.map)).getMap();
                googleMap.setMyLocationEnabled(true); //To See my current location

                /**
                 * If the map is still null after attempted initialisation,
                 * show an error to the user
                 */
                if (googleMap == null) {
                    Toast.makeText(getApplicationContext(),
                            "Error creating map", Toast.LENGTH_SHORT).show();
                }
            }
        } catch (NullPointerException exception) {
            Log.e("mapApp", exception.toString());
        }
    }

    // Adding marker to the current location
    public void addMarkerAtCurrent() {
        LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);    
        // Create a criteria object to retrieve provider
        Criteria criteria = new Criteria();    
        // Get the name of the best provider
        String provider = locationManager.getBestProvider(criteria, true);    
        // Get Current Location
        Location myLocation = locationManager.getLastKnownLocation(provider);    
        // set map type
        googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);    
        // Get latitude of the current location
        latitude = myLocation.getLatitude();    
        // Get longitude of the current location
        longitude = myLocation.getLongitude();    
        // Create a LatLng object for the current location
        LatLng latLng = new LatLng(latitude, longitude);
        String address = getLocationAddress(latitude, longitude);
        marker = googleMap.addMarker(    
                new MarkerOptions()
                        .position(new LatLng(latitude, longitude))
                        .title(address));
        // Show the current location in Google Map
        googleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
        // Zoom in the Google Map
        googleMap.animateCamera(CameraUpdateFactory.zoomTo(14));
    }      
    public LatLng search(View v) {
        edtLocation = (EditText) findViewById(R.id.edtLocation);
        String location = edtLocation.getText().toString();
          if (location != null) {
         Geocoder geocoder = new Geocoder(this);
            try {
                addressList = geocoder.getFromLocationName(location, 1);
            } catch (IOException e) {
                e.printStackTrace();
            }
            // To get address from list<Address>
            Address address = addressList.get(0);
            String locality = address.getLocality();
            String adminArea = address.getAdminArea();
            String locationAddress = locality + " " + adminArea;
             latitude = address.getLatitude();
            longitude = address.getLongitude();
            latLng = new LatLng(latitude, longitude);
            googleMap.clear();
              marker = googleMap.addMarker(new MarkerOptions().position(latLng).title(locationAddress));
           googleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
            googleMap.animateCamera(CameraUpdateFactory.zoomTo(14));
        }
        return latLng;
    }

    // Clicking the GoogleMap
    public LatLng mapClick() {
        googleMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
            @Override
            public void onMapClick(LatLng arg) {
                latLng = arg;
                latitude = latLng.latitude;
                longitude = latLng.longitude;
                String name = getLocationAddress(latitude, longitude);
                 googleMap.clear();
                marker = googleMap.addMarker(new MarkerOptions().position(latLng).title(locationAddress));
                googleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
                 googleMap.animateCamera(CameraUpdateFactory.zoomTo(15));
            }
        });
        return latLng;
    }

    public String getLocationAddress(double lat, double longi) {
        latitude = lat;
        longitude = longi;
        geocoder = new Geocoder(getApplicationContext());
        try {
            addressList = geocoder.getFromLocation(latitude, longitude, 1);
        } catch (IOException e) {
            e.printStackTrace();
        }
        Address address = addressList.get(0);
        String addressLine = address.getAddressLine(0);
        String locality = address.getLocality();
        String adminArea = address.getAdminArea();
        locationAddress = addressLine + " " + locality + " " + adminArea;
        return locationAddress;
    }

    // On clicking add button openDialog method will be called
    public void openDialog(View v) {
        dialogBuilder = new AlertDialog.Builder(this);
        LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = inflater.inflate(R.layout.custom_dialog, null, false);
        dialogBuilder.setTitle("Set Mode");
        dialogBuilder.setView(view);
        AlertDialog dialog = dialogBuilder.create();
        dialog.show();
        lat = (TextView) view.findViewById(R.id.lblLatitude);
        longi = (TextView) view.findViewById(R.id.lblLongitude);
        location_name = (TextView) view.findViewById(R.id.lblLocationName);
        setMode = (Button) view.findViewById(R.id.btnSetMode);
        latLng = marker.getPosition();
        lati1 = latLng.latitude;
        longi1 = latLng.longitude;
        name = getLocationAddress(lati1, longi1);
         lat.setText(Double.toString(lati1));
        longi.setText(Double.toString(longi1));
        location_name.setText(name);
        rg = (RadioGroup) view.findViewById(R.id.modesRadioGroup);
        rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.vibrationRadioButton:
                        Toast.makeText(getApplicationContext(), "Vibration clicked", Toast.LENGTH_LONG).show();    
                        break;
                    case R.id.muteRadioButton:
                        Toast.makeText(getApplicationContext(), "Mute clicked", Toast.LENGTH_LONG).show();
                        break;
                    case R.id.ringingRadioButton:
                        Toast.makeText(getApplicationContext(), "Ringing clicked", Toast.LENGTH_LONG).show();
                        break;
                    default:
                        break;
                }
            }
        });

    }
}

在此代码中,我使用的是Google Map的概念。我们可以通过EditText输入位置名称,然后点击调用search()方法的搜索按钮来搜索位置。这将是我的意思得到输入位置的纬度和经度。我也可以点击地图,这将调用onMapClick()方法。这也是我可以获得点击位置的纬度和经度。执行此代码时,我得到不同的纬度和使用这两种方法在同一位置的经度值。只关注search()onMapClick()方法。请帮我解决问题。

0 个答案:

没有答案