繁体   English   中英

如何在Android Studio中以字符串形式获取地图标记的位置?

[英]How can I get the location, as a string, of a map marker in Android studio?

我正在尝试从基于位置的服务应用程序中获取标记的当前位置,然后将该位置显示为字符串。 我需要打什么班? 我知道我可以将地图变量作为GoogleMap获取,但是我不确定要调用哪个函数来获取经度和纬度,然后将其转换为字符串。

提前致谢!

到目前为止,这是我的代码。 我试图将其放在displayCurrentLocation中以将当前位置显示为烤面包:

package com.example.lbs;

import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationManager;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.CameraPosition;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

import java.io.IOException;
import java.util.List;
import java.util.Locale;

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {

    private GoogleMap mMap;
    private MarkerOptions mo;

    @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);
    }


    /**
     * Manipulates the map once available.
     * This callback is triggered when the map is ready to be used.
     * This is where we can add markers or lines, add listeners or move the camera. In this case,
     * we just add a marker near Sydney, Australia.
     * If Google Play services is not installed on the device, the user will be prompted to install
     * it inside the SupportMapFragment. This method will only be triggered once the user has
     * installed Google Play services and returned to the app.
     */
    @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.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
            @Override
            public void onMapClick(LatLng point) {
                Log.d("DEBUG","Map clicked [" + point.latitude + " / " + point.longitude + "]");

                Geocoder geoCoder = new Geocoder(getBaseContext(), Locale.getDefault());
                try {
                    List<Address> addresses = geoCoder.getFromLocation(point.latitude,point.longitude,1);
                    String add = "";
                    if (addresses.size() > 0)
                    {
                        for (int i=0; i<addresses.get(0).getMaxAddressLineIndex(); i++)
                            add += addresses.get(0).getAddressLine(i) + "\n";
                    }
                    Toast.makeText(getBaseContext(), add, Toast.LENGTH_SHORT).show();
                }
                catch (IOException e) {
                    e.printStackTrace();
                }

                LatLng p = new LatLng(point.latitude, point.longitude);
                mMap.moveCamera(CameraUpdateFactory.newLatLng(p));
                mMap.clear();
                mMap.addMarker(new MarkerOptions().position(point));
                //mMap.addMarker(mo.position(point));
            }
        });
    }

    public void showMapView(View view){
        mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
    }
    public void showSatView(View view){
        mMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
    }
    public void findLocation(View view){
        Geocoder geoCoder = new Geocoder(getBaseContext(), Locale.getDefault());
        EditText location_et = (EditText) findViewById(R.id.location);
        String location = location_et.getText().toString();
        try {
            List<Address> addresses = geoCoder.getFromLocationName(
                    location, 5);
            if (addresses.size() > 0) {
                LatLng p = new LatLng((int) (addresses.get(0).getLatitude()),
                        (int) (addresses.get(0).getLongitude()));
                mMap.moveCamera(CameraUpdateFactory.newLatLng(p));
            }
        } catch (IOException e) {
            e.printStackTrace();
        }


    }

    public void displayCurrentLocation(View view){
        //Location loc = mMap.getMyLocation();
        //String location = loc.toString();
        //Toast.makeText(getBaseContext(),location, Toast.LENGTH_SHORT);
        //Location currentLocation = LocationServices.FusedLocationApi.getLastLocation(googleApiClient);
    }


}

您可以像这样的字符串来获取标记的纬度和经度:

LatLng pos = yourMarker.getPosition();
String latitude = String.valueOf(pos.latitude);
String longitude = String.valueOf(pos.longitude);

另外,这是Marker类文档

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM