[英]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);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.