![package com.example.mytt.cardguide;
import java.util.List;
import java.util.Locale;
import android.app.Activity;
import android.location.Address;
import android.location.Geocoder;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import com.example.mytt.R;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
public class CardGuide_MainActivity extends Activity {
Button btnShowLocation;
// GPSTracker class
GPSTracker gps;
GoogleMap map;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.cardguide);
// Get a handle to the Map Fragment
final GoogleMap map = ((MapFragment) getFragmentManager()
.findFragmentById(R.id.map)).getMap();
Button btnShowLocation = (Button) findViewById(R.id.showlocation_btn);
// show location button click event
btnShowLocation.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// create class object
gps = new GPSTracker(CardGuide_MainActivity.this);
// check if GPS enabled
if(gps.canGetLocation()){
double latitude = gps.getLatitude();
double longitude = gps.getLongitude();
LatLng mylocation = new LatLng(latitude,longitude);
String strAdd = "";
Geocoder geocoder = new Geocoder(CardGuide_MainActivity.this, Locale.getDefault());
try {
List<Address> addresses = geocoder.getFromLocation(latitude, longitude, 1);
if (addresses != null) {
Address returnedAddress = addresses.get(0);
StringBuilder strReturnedAddress = new StringBuilder("");
for (int i = 0; i < returnedAddress.getMaxAddressLineIndex(); i++) {
strReturnedAddress.append(returnedAddress.getAddressLine(i)).append("\n");
}
strAdd = strReturnedAddress.toString();
Log.w("My Current loction address", "" + strReturnedAddress.toString());
} else {
Log.w("My Current loction address", "No Address returned!");
}
} catch (Exception e) {
e.printStackTrace();
Log.w("My Current loction address", "Canont get Address!");
}
map.moveCamera(CameraUpdateFactory.newLatLngZoom(mylocation, 13));
Marker marker = map.addMarker(new MarkerOptions()
.position(mylocation)
.title(""+strAdd+""));
marker.showInfoWindow();
// \n is for new line
//Toast.makeText(getApplicationContext(), "Your Location is - \nLat: " + latitude + "\nLong: " + longitude, Toast.LENGTH_LONG).show();
}else{
// can't get location
// GPS or Network is not enabled
// Ask user to enable GPS/network in settings
gps.showSettingsAlert();
}
}
});
}
}][3]
当我点击图片视图时,地图会按原样加载,但点击“显示我的位置”按钮后,地图会消失,但当前位置的地址会按原样显示。
我不知道为什么地图会在点击按钮后消失。
我甚至检查了也不为空的地图对象......
答案 0 :(得分:0)
Google API控制台上的API密钥和软件包名称不匹配。