Android:Google将API v1映射到v2
我有一个android应用程序(源代码),它使用谷歌地图api v1。不幸的是,我不能使用它,因为我不能为v1api生成映射键。正因为如此,地图没有显示出来 有没有一种简单的方法可以更改源代码以与v2 google maps api兼容?我尝试过这个教程:但没有更大的成功(我是android开发的新手)Android:Google将API v1映射到v2,android,api,google-maps,Android,Api,Google Maps,我有一个android应用程序(源代码),它使用谷歌地图api v1。不幸的是,我不能使用它,因为我不能为v1api生成映射键。正因为如此,地图没有显示出来 有没有一种简单的方法可以更改源代码以与v2 google maps api兼容?我尝试过这个教程:但没有更大的成功(我是android开发的新手) 公共类MapviewGeolocation扩展MapActivity实现LocationListener{ 私有地图视图; 专用地图控制器; 公共静态浮点currentLatitude=Reso
公共类MapviewGeolocation扩展MapActivity实现LocationListener{
私有地图视图;
专用地图控制器;
公共静态浮点currentLatitude=Resources.lat;
公共静态浮点currentLength=Resources.lon;
//私人我的位置高于私人我的位置;
私人清单项目;
共享参考sp;
地点经理地点经理;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//严格模式
StrictMode.ThreadPolicy policy=新建StrictMode.ThreadPolicy.Builder()
.permitAll().build();
StrictMode.setThreadPolicy(策略);
sp=this.getPreferences(Context.MODE\u PRIVATE);
items=newarraylist();
最终位置myCurrentLocation=this.getLastBestLocation();
if(myCurrentLocation!=null){
if(Resources.isByGps){
currentLatitude=(float)myCurrentLocation.getLatitude();
currentLongitude=(float)myCurrentLocation.getLongitude();
}
}否则{
currentLatitude=Resources.lat;
currentLength=Resources.lon;
}
mapView=(mapView)findViewById(R.id.mapView);
mapView.SetBuilTinZoomControl(真);
mc=mapView.getController();
mc.setZoom(13);
地质点geo=新的地质点((int)(当前纬度*1e6),
(int)(当前经度*1e6));
司令官(土力工程处);
locationManager=(locationManager)此
.getSystemService(Context.LOCATION\u服务);
MyLocationOverlay MyLocationOverlay=新建MyLocationOverlay(此,
地图视图);
mylocationOverlay.enableMyLocation();
mapView.getOverlays().add(mylocationOverlay);
InitMapTask init_map_task=新建InitMapTask();
init_map_task.execute();
}
@凌驾
受保护的void onResume(){
super.onResume();
字符串adres=sp.getString(“adres”和“”);
如果(地址长度()<1){
Resources.isByGps=true;
}否则{
Resources.isByGps=false;
}
如果(!Resources.isByGps){
currentLatitude=sp.getFloat(“lat”,Resources.lat);
Current经度=sp.getFloat(“lon”,Resources.lon);
}否则{
locationManager.RequestLocationUpdate(
LocationManager.GPS_提供商,5000,200,此);
}
mapView.refreshDrawableState();
mapView.invalidate();
}
@凌驾
受保护的void onPause(){
//TODO自动生成的方法存根
super.onPause();
if(Resources.isByGps){
locationManager.RemoveUpdate(此);
}
}
私有void addOverlays(){
项目=资源。事件;
}
公共可提取getDrawable(整数人口){
Drawable Drawable=null;
如果(人口<300)
drawable=this.getResources().getDrawable(R.drawable.pins_-rose);
否则,如果((300人))
drawable=this.getResources().getDrawable(R.drawable.pins_bleu);
否则,如果((500人))
drawable=this.getResources().getDrawable(R.drawable.pins\u vert);
否则,如果((800人))
drawable=this.getResources().getDrawable(R.drawable.pins_jaune);
其他的
drawable=this.getResources().getDrawable(R.drawable.pins_blanc);
回拉;
}
专用void addOverlay(MapItemizedOverlay itemizedOverlay){
Event ev=itemizedOverlay.getLocation();
地质点位置=新的地质点((int)(ev.getLat()*1E6),
(int)(ev.getLon()*1E6));
OverlayItem OverlayItem=新的OverlayItem(位置,ev.getTitle(),
ev.getCity());
itemizedOverlay.addOverlay(overlayitem);
mapView.getOverlays().add(ItemizeOverlay);
}
@凌驾
受保护的布尔值isRouteDisplayed(){
返回false;
}
@凌驾
已更改位置上的公共无效(位置){
if(Resources.istest){
Toast.makeText(
getBaseContext(),
“本地化已更改-当前:纬度=”
+currentLatitude+“经度=”
+当前经度,Toast.LENGTH_LONG).show();
}
//if(Resources.isByGps){
如果(位置!=null){
currentLatitude=(浮点)位置。getLatitude();
currentLongitude=(float)location.getLongitude();
地质点geo=新的地质点((int)(当前纬度*1e6),
(int)(当前经度*1e6));
司令官(土力工程处);
}
// }
mapView.invalidate();
}
@凌驾
公共无效onProviderDisabled(字符串提供程序){
if(Resources.istest)
Toast.makeText(此“GPS关闭…”,Toast.LENGTH_LONG.show();
}
@凌驾
公共无效onProviderEnabled(字符串提供程序){
if(Resources.istest)
Toast.makeText(此“GPS开启…”,Toast.LENGTH_LONG.show();
}
@凌驾
public void onStatusChanged(字符串提供程序、int状态、Bundle extra){
if(Resources.istest)
Toast.makeText(此“GPS状态已更改…”,Toast.LENGTH\u LONG)
.show();
}
公共类InitMapTask扩展了AsyncTask{
私人进展对话进展;
@凌驾
赞成的意见
public class MapviewGeolocation extends MapActivity implements LocationListener {
private MapView mapView;
private MapController mc;
public static float currentLatitude = Resources.lat;
public static float currentLongitude = Resources.lon;
// private MyLocationOverlay myLocation;
private List<Event> items;
SharedPreferences sp;
LocationManager locationManager;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// STRICTMODE
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
.permitAll().build();
StrictMode.setThreadPolicy(policy);
sp = this.getPreferences(Context.MODE_PRIVATE);
items = new ArrayList<Event>();
final Location myCurrentLocation = this.getLastBestLocation();
if (myCurrentLocation != null) {
if (Resources.isByGps) {
currentLatitude = (float) myCurrentLocation.getLatitude();
currentLongitude = (float) myCurrentLocation.getLongitude();
}
} else {
currentLatitude = Resources.lat;
currentLongitude = Resources.lon;
}
mapView = (MapView) findViewById(R.id.mapview);
mapView.setBuiltInZoomControls(true);
mc = mapView.getController();
mc.setZoom(13);
GeoPoint geo = new GeoPoint((int) (currentLatitude * 1e6),
(int) (currentLongitude * 1e6));
mc.animateTo(geo);
locationManager = (LocationManager) this
.getSystemService(Context.LOCATION_SERVICE);
MyLocationOverlay mylocationOverlay = new MyLocationOverlay(this,
mapView);
mylocationOverlay.enableMyLocation();
mapView.getOverlays().add(mylocationOverlay);
InitMapTask init_map_task = new InitMapTask();
init_map_task.execute();
}
@Override
protected void onResume() {
super.onResume();
String adres = sp.getString("adres", "");
if (adres.length() < 1) {
Resources.isByGps = true;
} else {
Resources.isByGps = false;
}
if (!Resources.isByGps) {
currentLatitude = sp.getFloat("lat", Resources.lat);
currentLongitude = sp.getFloat("lon", Resources.lon);
} else {
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER, 5000, 200, this);
}
mapView.refreshDrawableState();
mapView.invalidate();
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
if (Resources.isByGps) {
locationManager.removeUpdates(this);
}
}
private void addOverlays() {
items = Resources.events;
}
public Drawable getDrawable(int population) {
Drawable drawable = null;
if (population < 300)
drawable = this.getResources().getDrawable(R.drawable.pins_rose);
else if ((300 <= population) && (500 > population))
drawable = this.getResources().getDrawable(R.drawable.pins_bleu);
else if ((500 <= population) && (800 > population))
drawable = this.getResources().getDrawable(R.drawable.pins_vert);
else if ((800 <= population) && (1000 > population))
drawable = this.getResources().getDrawable(R.drawable.pins_jaune);
else
drawable = this.getResources().getDrawable(R.drawable.pins_blanc);
return drawable;
}
private void addOverlay(MapItemizedOverlay itemizedOverlay) {
Event ev = itemizedOverlay.getLocation();
GeoPoint location = new GeoPoint((int) (ev.getLat() * 1E6),
(int) (ev.getLon() * 1E6));
OverlayItem overlayitem = new OverlayItem(location, ev.getTitle(),
ev.getCity());
itemizedOverlay.addOverlay(overlayitem);
mapView.getOverlays().add(itemizedOverlay);
}
@Override
protected boolean isRouteDisplayed() {
return false;
}
@Override
public void onLocationChanged(Location location) {
if (Resources.isTesting) {
Toast.makeText(
getBaseContext(),
"Localization changed - current: Latitude = "
+ currentLatitude + " Longitude = "
+ currentLongitude, Toast.LENGTH_LONG).show();
}
// if (Resources.isByGps) {
if (location != null) {
currentLatitude = (float) location.getLatitude();
currentLongitude = (float) location.getLongitude();
GeoPoint geo = new GeoPoint((int) (currentLatitude * 1e6),
(int) (currentLongitude * 1e6));
mc.animateTo(geo);
}
// }
mapView.invalidate();
}
@Override
public void onProviderDisabled(String provider) {
if (Resources.isTesting)
Toast.makeText(this, "GPS is off...", Toast.LENGTH_LONG).show();
}
@Override
public void onProviderEnabled(String provider) {
if (Resources.isTesting)
Toast.makeText(this, "GPS is on...", Toast.LENGTH_LONG).show();
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
if (Resources.isTesting)
Toast.makeText(this, "GPS status changed...", Toast.LENGTH_LONG)
.show();
}
public class InitMapTask extends AsyncTask<Void, Void, Void> {
private ProgressDialog progress;
@Override
protected void onPreExecute() {
super.onPreExecute();
progress = new ProgressDialog(MapviewGeolocation.this);
progress.setMessage("Loading...");
progress.show();
}
@Override
protected Void doInBackground(Void... params) {
addOverlays();
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
for (int i = 0; i < items.size(); i++) {
int color = 0;
Drawable drawable = getDrawable(400);
MapItemizedOverlay itemizedOverlay = new MapItemizedOverlay(
drawable, mapView, MapviewGeolocation.this, color,
items.get(i), currentLatitude, currentLongitude);
addOverlay(itemizedOverlay);
}
mapView.invalidate();
progress.dismiss();
}
}
/**
* @return the last know best location
*/
private Location getLastBestLocation() {
LocationManager mLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Location locationGPS = mLocationManager
.getLastKnownLocation(LocationManager.GPS_PROVIDER);
Location locationNet = mLocationManager
.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
long GPSLocationTime = 0;
if (null != locationGPS) {
GPSLocationTime = locationGPS.getTime();
}
long NetLocationTime = 0;
if (null != locationNet) {
NetLocationTime = locationNet.getTime();
}
if (0 < GPSLocationTime - NetLocationTime) {
return locationGPS;
} else {
return locationNet;
}
}
// action for bottom menu
public void actionLista(View v) {
Intent i = new Intent(this, ListviewActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
i.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(i);
}
public void actionMapa(View v) {
Intent i = new Intent(this, MapviewGeolocation.class);
i.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
i.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(i);
}
public void actionSettings(View v) {
Intent i = new Intent(this, Settings.class);
i.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
i.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(i);
}
}