Android从标记标题中的当前位置设置地址(位置名称)

时间:2016-01-06 18:34:20

标签: android google-maps

我想从当前位置获取地址并输入标记的标题。现在我只有.title(“Tuubicación”))。showInfoWindow();但我想改变地址

这是我的代码:

public class Request extends Fragment implements LocationListener {
private FragmentActivity myContext;

public static final String ARG_REQUEST = "arg_request";

public Request() {
    // Empty constructor required for fragment subclasses
}
MapView mapView;
GoogleMap map;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.request, container, false);
    // Gets the MapView from the XML layout and creates it

    switch (GooglePlayServicesUtil.isGooglePlayServicesAvailable(getActivity()) )
    {
        case ConnectionResult.SUCCESS:
            mapView = (MapView) v.findViewById(R.id.googleMap);
            mapView.onCreate(savedInstanceState);
            // Gets to GoogleMap from the MapView and does initialization stuff
            if(mapView!=null)
            {
                map = mapView.getMap();
                map.getUiSettings().setMyLocationButtonEnabled(false);
                map.setMyLocationEnabled(true);
                LocationManager locationManager = (LocationManager) myContext.getSystemService(myContext.LOCATION_SERVICE);
                Criteria criteria = new Criteria();
                String bestProvider = locationManager.getBestProvider(criteria, true);
                Location location = locationManager.getLastKnownLocation(bestProvider);
                if (location != null) {
                    onLocationChanged(location);
                }
                locationManager.requestLocationUpdates(bestProvider, 20000, 0, this);
            }
            break;
        case ConnectionResult.SERVICE_MISSING:
            break;
        case ConnectionResult.SERVICE_VERSION_UPDATE_REQUIRED:
            break;
        default:
    }
    // Updates the location and zoom of the MapView

    return v;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setHasOptionsMenu(true);
}

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    // TODO Add your menu entries here
    inflater.inflate(R.menu.main, menu);
    super.onCreateOptionsMenu(menu, inflater);
}

@Override
public void onResume() {
    mapView.onResume();
    super.onResume();
}
@Override
public void onDestroy() {
    super.onDestroy();
    mapView.onDestroy();
}
@Override
public void onLowMemory() {
    super.onLowMemory();
    mapView.onLowMemory();
}

@Override
public void onLocationChanged(Location location) {
    map.clear();
    double latitude = location.getLatitude();
    double longitude = location.getLongitude();
    LatLng latLng = new LatLng(latitude, longitude);
    map.addMarker(new MarkerOptions()
            .position(latLng)
            .icon(BitmapDescriptorFactory.fromResource(R.drawable.car))
            .title("Tu ubicación")).showInfoWindow();
    map.moveCamera(CameraUpdateFactory.newLatLng(latLng));
    map.animateCamera(CameraUpdateFactory.zoomTo(15));
}

和xml:

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="150dp"
    android:id="@+id/relativeLayout">

    <com.google.android.gms.maps.MapView
        android:id="@+id/googleMap"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true" />
</RelativeLayout>

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/imageViewPerfilPersona"
    android:src="@drawable/perfil_persona_b"
    android:layout_alignParentTop="true"
    android:layout_alignRight="@+id/textViewClientName"
    android:layout_alignEnd="@+id/textViewClientName"
    android:layout_marginTop="112dp" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceSmall"
    android:text="@string/ClientName"
    android:id="@+id/textViewClientName"
    android:layout_below="@+id/imageViewPerfilPersona"
    android:layout_centerHorizontal="true" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceSmall"
    android:text="@string/TimeLeftRequest"
    android:id="@+id/textViewTimeLeft"
    android:layout_below="@+id/textViewClientName"
    android:layout_centerHorizontal="true" />

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/imageViewCar"
    android:src="@drawable/car"
    android:paddingRight="5dp"
    android:paddingTop="0dp"
    android:paddingBottom="0dp"
    android:layout_alignTop="@+id/textViewTypeCar"
    android:layout_toStartOf="@+id/textViewTypeCar"
    android:layout_alignBottom="@+id/textViewTypeCar"
    android:layout_toLeftOf="@+id/textViewTypeCar" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:text="@string/TypeCar"
    android:id="@+id/textViewTypeCar"
    android:paddingLeft="20dp"
    android:layout_marginTop="30dp"
    android:layout_below="@+id/textViewTimeLeft"
    android:layout_centerHorizontal="true"
    android:paddingTop="0dp"
    android:paddingRight="10dp"
    android:paddingStart="20dp"
    android:paddingEnd="10dp" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:text="@string/I_need"
    android:id="@+id/textViewI_need"
    android:layout_below="@+id/textViewTypeCar"
    android:layout_alignLeft="@+id/textViewTypeCar"
    android:layout_alignStart="@+id/textViewTypeCar"
    android:layout_marginTop="15dp"
    android:paddingLeft="20dp"
    android:paddingRight="10dp"
    android:paddingStart="20dp"
    android:paddingEnd="10dp" />

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/imageViewTire"
    android:layout_below="@+id/imageViewCar"
    android:layout_alignLeft="@+id/imageViewCar"
    android:layout_alignStart="@+id/imageViewCar"
    android:src="@drawable/tire"
    android:layout_alignBottom="@+id/textViewI_need"
    android:layout_alignRight="@+id/imageViewCar"
    android:layout_alignEnd="@+id/imageViewCar"
    android:layout_alignTop="@+id/textViewI_need" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:text="@string/ClientLocation"
    android:id="@+id/textViewLocation"
    android:layout_below="@+id/textViewI_need"
    android:layout_alignLeft="@+id/textViewI_need"
    android:layout_alignStart="@+id/textViewI_need"
    android:layout_marginTop="15dp"
    android:paddingLeft="20dp"
    android:paddingRight="10dp"
    android:paddingStart="20dp"
    android:paddingEnd="10dp"/>

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/imageViewLocation"
    android:layout_alignLeft="@+id/imageViewTire"
    android:layout_alignStart="@+id/imageViewTire"
    android:src="@drawable/map"
    android:layout_alignBottom="@+id/textViewLocation"
    android:layout_alignRight="@+id/imageViewTire"
    android:layout_alignEnd="@+id/imageViewTire"
    android:layout_alignTop="@+id/textViewLocation" />

<ImageButton
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/imageButtonCheck"
    android:src="@drawable/check"
    android:background="@null"
    android:onClick="requestCheckButton"
    android:layout_alignParentBottom="true"
    android:layout_alignRight="@+id/textViewTypeCar"
    android:layout_alignEnd="@+id/textViewTypeCar"
    android:layout_marginBottom="38dp" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:text="@string/WaitResponse"
    android:id="@+id/textViewResponse"
    android:layout_below="@+id/textViewLocation"
    android:layout_alignLeft="@+id/textViewLocation"
    android:layout_alignStart="@+id/textViewLocation"
    android:layout_marginTop="15dp"
    android:paddingLeft="20dp"
    android:paddingRight="10dp"
    android:paddingStart="20dp"
    android:paddingEnd="10dp"
    android:visibility="invisible"
    android:onClick="requestSent"/>

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/imageViewResponse"
    android:layout_alignTop="@+id/textViewResponse"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:src="@drawable/wait_response"
    android:layout_alignBottom="@+id/textViewResponse"
    android:layout_alignRight="@+id/imageViewLocation"
    android:layout_alignEnd="@+id/imageViewLocation"
    android:visibility="invisible" />

<Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="@string/BeginService"
    android:id="@+id/buttonBeginService"
    android:layout_alignTop="@+id/imageButtonCheck"
    android:layout_centerHorizontal="true"
    android:visibility="invisible"
    android:onClick="beginService"/>

Here's a image from the layout

2 个答案:

答案 0 :(得分:1)

试试这个:

String street = null;
Geocoder geocoder = new Geocoder(getActivity(), Locale.getDefault());
try {
     List<Address> addresses = geocoder.getFromLocation(location.getLatitude(), location.getLongitude(), 1);
     if (addresses != null) {
                Address returnedAddress = addresses.get(0);
                StringBuilder strReturnedAddress = new StringBuilder();
                for (int j = 0; j < returnedAddress.getMaxAddressLineIndex(); j++) {
                    strReturnedAddress.append(returnedAddress.getAddressLine(j)).append("");
                }
                street = strReturnedAddress.toString();
            }
  } catch (IOException e) {
}

现在处理street字符串。

答案 1 :(得分:0)

@覆盖     public void onLocationChanged(位置位置)     {

    mLastLocation = location;
    if (mCurrLocationMarker != null) {
        mCurrLocationMarker.remove();
    }
    //Place current location marker
    LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
    MarkerOptions markerOptions = new MarkerOptions();
    markerOptions.position(latLng);
    markerOptions.title(street);
    markerOptions.snippet("your Location");
    markerOptions.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_MAGENTA));
    mGoogleMap.addMarker(markerOptions).showInfoWindow();
    //move map camera
    mGoogleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng,13));

    Geocoder geocoder = new Geocoder(this, Locale.getDefault());
    try {
        List<android.location.Address> addresses = geocoder.getFromLocation(location.getLatitude(), location.getLongitude(), 1);
        if (addresses != null) {
            android.location.Address returnedAddress = addresses.get(0);
            StringBuilder strReturnedAddress = new StringBuilder();
            for (int j = 0; j < returnedAddress.getMaxAddressLineIndex(); j++) {
                strReturnedAddress.append(returnedAddress.getAddressLine(j)).append("");
            }
            street = strReturnedAddress.toString();
            Log.e("byeee",""+street);
        }
    } catch (IOException e) {
        e.printStackTrace();
    }


}

这对我有用......试试..