현재 위치에서 마커를 만들고 이전 마커도 표시하려는 Android Map App을 만듭니다. 내 현재 위치에서 마커를 만들 수 있지만 다른 위치에서 앱을 다시 시작할 때 내 앱은 이전 위치 마커 만 표시합니다. 도와주세요. 여기에 내가 뭘하려 :안드로이드지도 앱에 마커 저장
이public class Main extends FragmentActivity implements OnMapReadyCallback, GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener {
private GoogleMap mMap;
private GoogleApiClient mGoogleApiClient;
public Location mLastLocation;
ArrayList<LatLng> listOfPoints = new ArrayList<>();
public boolean isMapReady = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
if (mGoogleApiClient == null) {
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
}
}
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
isMapReady = true;
}
public boolean onMarkerClick(final Marker marker) {
return false;
}
public void onConnected(Bundle connectionHint) {
if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
mLastLocation = LocationServices.FusedLocationApi.getLastLocation(
mGoogleApiClient);
MarkerOptions mp = new MarkerOptions();
mp.position(new LatLng(mLastLocation.getLatitude(), mLastLocation.getLongitude()));
mp.title("my position");
mMap.addMarker(mp);
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(
new LatLng(mLastLocation.getLatitude(), mLastLocation.getLongitude()), 16));
LatLng newLatLng = new LatLng(mLastLocation.getLatitude(), mLastLocation.getLongitude());
listOfPoints.add(newLatLng);
}
public void onConnectionSuspended(int i) {
}
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
}
protected void onStart() {
super.onStart();
mGoogleApiClient.connect();
}
protected void onStop() {
super.onStop();
mGoogleApiClient.disconnect();
}
protected void onPause() {
super.onPause();
try {
// Modes: MODE_PRIVATE, MODE_WORLD_READABLE, MODE_WORLD_WRITABLE
FileOutputStream output = openFileOutput("latlngpoints.txt",
Context.MODE_PRIVATE);
DataOutputStream dout = new DataOutputStream(output);
dout.writeInt(listOfPoints.size()); // Save line count
for (LatLng point : listOfPoints) {
dout.writeUTF(point.latitude + "," + point.longitude);
Log.v("write", point.latitude + "," + point.longitude);
}
dout.flush(); // Flush stream ...
dout.close(); // ... and close.
} catch (IOException exc) {
exc.printStackTrace();
}
}
protected void onResume(){
super.onResume();
if (isMapReady==true){
try {
FileInputStream input = openFileInput("latlngpoints.txt");
DataInputStream din = new DataInputStream(input);
int sz = din.readInt(); // Read line count
for (int i = 0; i < sz; i++) {
String str = din.readUTF();
Log.v("read", str);
String[] stringArray = str.split(",");
double latitude = Double.parseDouble(stringArray[0]);
double longitude = Double.parseDouble(stringArray[1]);
listOfPoints.add(new LatLng(latitude, longitude));
}
din.close();
loadMarkers(listOfPoints);
} catch (IOException exc) {
exc.printStackTrace();
}
}
}
protected void onSaveInstanceState(Bundle outState){
super.onSaveInstanceState(outState);
outState.putParcelableArrayList("places", listOfPoints);
}
private void restore(Bundle outState){
if (outState != null) {
listOfPoints =(ArrayList<LatLng>)outState.getSerializable("places");
}
}
protected void onRestoreInstanceState(Bundle outState) {
super.onRestoreInstanceState(outState);
restore(outState);
}
private void loadMarkers(List<LatLng> listOfPoints) {
int i=listOfPoints.size();
while(i>0){
i--;
double Lat=listOfPoints.get(i).latitude;
double Lon=listOfPoints.get(i).longitude;
MarkerOptions mp = new MarkerOptions();
mp.position(new LatLng(Lat, Lon));
mp.title("my previous position");
mMap.addMarker(mp);
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(
new LatLng(Lat, Lon), 16));
}
}
}
정말로이 파일을 가져 오는 것과 비슷합니다. – danny117
답을 확인하십시오 http://stackoverflow.com/questions/33430559/how-to-make-a-search-on-google-maps-for-finding-hospitals-etc/33431118#33431118 – AndroidHacker
코드를 보며 외모를 보입니다. 마치 마커를 저장하고로드하는 데 필요한 모든 일을하고있는 것처럼 말입니다. 흥미로운 점은 if (isMapReady == true) 입니다. 이제는 onResume이 발생할 때마다이 조건이 참이되지 않습니까? onResume이 일어날 때까지는지도가 준비되지 않은 것일 수 있습니다. 이것을 확인할 수 있습니까? – CrashOverride