如何在MapView上缩放接收的坐标(经度和纬度)

时间:2018-05-10 10:36:19

标签: android

如何在MapView上显示经度和纬度坐标?

这是我的xml布局文件:

<TextView
    android:layout_marginTop="25dp"
    android:id="@+id/addresse"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="20sp"
    android:layout_centerHorizontal="true"
    android:text="my full address"
    />



<TextView
    android:id="@+id/textView"
    android:layout_width="match_parent"
    android:gravity="center"
    android:layout_height="wrap_content"
    android:text="Coordinates: "
    android:textSize="30sp"
    android:layout_below="@+id/button"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_marginTop="20dp"/>





<Button
    android:id="@+id/button"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Request Location"
    android:background="@color/colorAccent"
    android:textColor="#ffffff"
    android:layout_below="@+id/addresse"
    android:layout_alignParentStart="true"
    android:layout_marginTop="20dp"/>



<com.google.android.gms.maps.MapView
    android:id="@+id/mapView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_below="@id/textView"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="50dp"
    android:layout_marginTop="30dp" />

这是我的java代码,我在onlocationchanged方法上接收gps坐标。

我在textview上显示它们之后我使用地理编码反向获取地址并将其显示在上面的textview上:

公共类MainActivity扩展了AppCompatActivity {

private Button b;
private TextView t;
private LocationManager locationManager;
private LocationListener listener;
TextView addresse;
Geocoder geocoder;
List<Address> addresses;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    t = (TextView) findViewById(R.id.textView);
    b = (Button) findViewById(R.id.button);
    addresse = (TextView)findViewById(R.id.addresse);

    locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);

    listener = new LocationListener() {
        @Override
        public void onLocationChanged(Location location) {
            t.setText("\n " + location.getLongitude() + " " + location.getLatitude());
            geocoder = new Geocoder(MainActivity.this, Locale.getDefault());
            try {
                addresses = geocoder.getFromLocation(location.getLatitude(),location.getLongitude(),1);
                String address = addresses.get(0).getAddressLine(0);
                String area = addresses.get(0).getLocality();
                String city = addresses.get(0).getAdminArea();
                String country = addresses.get(0).getCountryName();

                String fulladdress = address + " " + area + " " + city + " " + country;
                addresse.setText(fulladdress);
            } catch (IOException e) {
                e.printStackTrace();
                Toast.makeText(MainActivity.this, "error while geocoding reverse", Toast.LENGTH_SHORT).show();
            }



        }

        @Override
        public void onStatusChanged(String s, int i, Bundle bundle) {

        }

        @Override
        public void onProviderEnabled(String s) {

        }

        @Override
        public void onProviderDisabled(String s) {
            Intent i = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
            startActivity(i);
        }
    };

    configure_button();

}


@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    switch (requestCode) {
        case 10:
            configure_button();

            break;
        default:
            break;
    }
}


void configure_button() {
    // first check for permissions
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            requestPermissions(new String[]{Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.INTERNET}
                    , 10);
        }
        return;
    }

    // this code won't execute IF permissions are not allowed, because in the line above there is return statement.

    b.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            //noinspection MissingPermission
            if (ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                return;
            }


            locationManager.requestLocationUpdates("gps", 5000, 0, listener);

        }
    });
}

}

1 个答案:

答案 0 :(得分:0)

我认为,这会奏效。

1)将收到的纬度和经度存储在LatLng对象中。

2)您可以使用

设置缩放比例

mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latlng, 16));

其中16是缩放比例。 3)您可以使用

直接添加缩放工具

UiSettings settings=mMap.getUiSettings(); settings.setZoomControlsEnabled(true);

首先,在您的活动中使用地图设施, 让您的活动扩展FragmentActivity并使其实现OnMapReadyCallback,并实施所需的方法。

现在,您的活动将如下所示。

    public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {

            @Override
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_maps);

                // Obtain the SupportMapFragment and get notified when the map is ready to be used.
                SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                    .findFragmentById(R.id.map);
                mapFragment.getMapAsync(this);

            }
            @Override
            public void onMapReady(GoogleMap googleMap) {
                mMap = googleMap;

                // Add a marker in Sydney and move the camera
                LatLng sydney = new LatLng(-34, 151);
                mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
                mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
                mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL | GoogleMap.MAP_TYPE_SATELLITE);
                mMap.setMyLocationEnabled(true);
                UiSettings settings = mMap.getUiSettings();
                settings.setZoomControlsEnabled(true);
            }
    }

,您的布局文件如下所示:

    <fragment xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:map="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/map"
        android:name="com.google.android.gms.maps.SupportMapFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context="demomap.app.com.MapsActivity" />

我希望这会有所帮助。如果这不是答案,请忽略这个答案。 谢谢。