我是android的新手。 我一直在寻找,但我找不到我的问题的答案。
我制作了一个显示图像的应用程序。
我当前位置的地图非常清楚。
但是将imapge映射到其他位置真的不清楚。
有人可以帮助我吗?
请找到附上我拍摄的图片,如下所示
我的代码如下,
package org.androidtown.burningfriday;
import android.Manifest;
import android.app.Dialog;
import android.content.pm.PackageManager;
import android.location.Geocoder;
import android.location.Location;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.TextView;
import android.widget.Toast;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.location.LocationListener;
import com.google.android.gms.location.LocationServices;
import com.google.android.gms.maps.CameraUpdate;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
import java.io.IOException;
import java.util.List;
public class MainActivity extends AppCompatActivity implements GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener {
GoogleMap mMap;
private static final int ERROR_DIALOG_REQUEST = 9001;
private GoogleApiClient mLocationClient;
private LocationListener mListenter;
private Marker marker;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (servicesOK()) {
setContentView(R.layout.activity_map);
if (initMap()) {
gotoLocation(37, 128, 5);
mLocationClient = new GoogleApiClient.Builder(this)
.addApi(LocationServices.API)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.build();
mLocationClient.connect();
} else {
Toast.makeText(this, "맵핑서비스에 연결못함", Toast.LENGTH_SHORT).show();
}
Toast.makeText(this, "Ready to map", Toast.LENGTH_SHORT).show();
} else {
setContentView(R.layout.activity_main);
}
}
public boolean servicesOK() {
int isAvailable = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
if (isAvailable == ConnectionResult.SUCCESS) {
return true;
} else if (GooglePlayServicesUtil.isUserRecoverableError((isAvailable))) {
Dialog dialog = GooglePlayServicesUtil.getErrorDialog(isAvailable, this, ERROR_DIALOG_REQUEST);
dialog.show();
} else {
Toast.makeText(this, "맵핑서비스에 연결못함", Toast.LENGTH_SHORT).show();
}
return false;
}
private boolean initMap() {
if (mMap == null) {
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
mMap = mapFragment.getMap();
mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
if (mMap != null) {
mMap.setInfoWindowAdapter(new GoogleMap.InfoWindowAdapter() {
@Override
public View getInfoWindow(Marker marker) {
return null;
}
@Override
public View getInfoContents(Marker marker) {
View v = getLayoutInflater().inflate(R.layout.info_window, null);
TextView tvLocality = (TextView) v.findViewById(R.id.tvLocality);
TextView tvLat = (TextView) v.findViewById(R.id.tvLat);
TextView tvLng = (TextView) v.findViewById(R.id.tvLng);
TextView tvSnippet = (TextView) v.findViewById(R.id.tvSnippet);
LatLng latLng = marker.getPosition();
tvLocality.setText(marker.getTitle());
tvLat.setText("Latitude:" + latLng.latitude);
tvLng.setText("Longitude:" + latLng.longitude);
tvSnippet.setText(marker.getSnippet());
return v;
}
});
mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
@Override
public boolean onMarkerClick(Marker marker) {
String msg = marker.getTitle() + "("+marker.getPosition().latitude+","+marker.getPosition().longitude+")";
Toast.makeText(MainActivity.this, msg, Toast.LENGTH_SHORT).show();
return false;
}
});
}
}
return (mMap != null);
}
private void gotoLocation(double lat, double lng, float zoom) {
LatLng latLng = new LatLng(lat, lng);
CameraUpdate update = CameraUpdateFactory.newLatLngZoom(latLng, zoom);
mMap.moveCamera(update);
}
private void hideSoftKeyboard(View v) {
InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
}
public void geoLocate(View v) throws IOException {
hideSoftKeyboard(v);
TextView tv = (TextView) findViewById(R.id.editText1);
String searchString = tv.getText().toString();
Toast.makeText(this, "Searching for : " + searchString, Toast.LENGTH_SHORT).show();
Geocoder gc = new Geocoder(this);
List<android.location.Address> list = gc.getFromLocationName(searchString, 1);
if (list.size() > 0) {
android.location.Address add = list.get(0);
String locality = add.getLocality();
Toast.makeText(this, "Found : " + locality, Toast.LENGTH_SHORT).show();
double lat = add.getLatitude();
double lng = add.getLongitude();
gotoLocation(lat, lng, 10);
if (marker != null) {
marker.remove();
}
MarkerOptions options = new MarkerOptions()
.title(add.getLocality())
.position(new LatLng(lat, lng))
.icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_backward));
String country = add.getCountryName();
if (country.length() > 0) {
options.snippet(country);
}
marker = mMap.addMarker(options);
}
}
public void showCurrentLocation(View v) {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
Location currentLocation = LocationServices.FusedLocationApi
.getLastLocation(mLocationClient);
if (currentLocation == null) {
Toast.makeText(this, "위치 설정을 켜주시기 바랍니다.", Toast.LENGTH_SHORT).show();
} else {
LatLng latLng = new LatLng(currentLocation.getLatitude(), currentLocation.getLongitude());
CameraUpdate update = CameraUpdateFactory.newLatLngZoom(latLng, 15);
mMap.animateCamera(update);
}
}
@Override
public void onConnected(@Nullable Bundle bundle) {
}
@Override
public void onConnectionSuspended(int i) {
}
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
}
@Override
protected void onPause() {
super.onPause();
LocationServices.FusedLocationApi.removeLocationUpdates(
mLocationClient,mListenter
);
}
}
三江源