在android中加载谷歌地图的问题

时间:2013-10-19 05:37:44

标签: android google-maps

我正在进行一项主要活动,当按下此按钮时,我有一个按钮,它显示了Map.This工作完美。

但是当我按下后退键时,我再次进入我的主要活动,现在又一次按下地图按钮,显示全世界地图。

我的意思是说它从头开始。

如何从Back Stack中完全删除我的地图活动并再次创建以再次显示我想要的地图?

或其他任何解决方案吗?

修改:1

btn_find_atm.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            Intent intent = new Intent(NearByATM.this, Map.class);
            startActivityForResult(intent, request_map);

        }
    });

修改:2

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    if (isGooglePlayAvailable()) {

        expld = new ExpandablePlaceListData();

        criteria = new Criteria();
        criteria.setAccuracy(Criteria.ACCURACY_FINE);

        // Getting the service from context and giving to location_manager
        location_manager = (LocationManager) getSystemService(LOCATION_SERVICE);

        setContentView(R.layout.mapdemo);

        placesTask = new PlacesTask();

        getGoogleMap();

        getUserLocation();

        google_map.clear();

        Toast.makeText(this, "Latitude:" + lat + " Longitude:" + lang,
                Toast.LENGTH_LONG).show();

        drawMarker();

        sb = createUrl();

        placesTask.execute(sb);

    }

}

@Override
public void onBackPressed() {
    super.onBackPressed();
    Intent returnIntent = new Intent();
    setResult(RESULT_OK, returnIntent);
    this.finish();
}

修改:3

private void getUserLocation() {

    if (location_manager != null) {
        provider = location_manager.getBestProvider(criteria, true);
        location = location_manager.getLastKnownLocation(provider);
        location_manager.requestLocationUpdates(provider,
                Map.MIN_TIME_BW_UPDATES,
                Map.MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
        if (location != null) {
            lat = location.getLatitude();
            lang = location.getLongitude();

        } else {

            Toast.makeText(Map.this, "Unable to idntify location",
                    Toast.LENGTH_LONG).show();

            lat = -22.00000;
            lang = 33.0000;

        }
    }
}

修改:4

当我再次按下按钮加载地图时,LogCat会显示类似这些内容。

10-19 16:47:06.048: W/Trace(4887): Unexpected value from nativeGetEnabledTags: 0
10-19 16:47:06.086: I/SurfaceTexture(4887): [unnamed-4887-1](this:0x5fa6d608,api:0) detachFromContext
10-19 16:47:06.087: I/BufferQueue(4887): [unnamed-4887-1](this:0x610cd580,api:0) consumerDisconnect
10-19 16:47:06.088: I/BufferQueue(4887): [unnamed-4887-1](this:0x610cd580,api:0) ~BufferQueue
10-19 16:47:06.090: D/dalvikvm(4887): threadid=22: exiting
10-19 16:47:06.096: W/Trace(4887): Unexpected value from nativeGetEnabledTags: 0
10-19 16:47:06.097: D/dalvikvm(4887): threadid=22: bye!
10-19 16:47:08.782: W/Trace(4887): Unexpected value from nativeGetEnabledTags: 0
10-19 16:47:08.783: W/Trace(4887): Unexpected value from nativeGetEnabledTags: 0
10-19 16:47:08.785: W/Trace(4887): Unexpected value from nativeGetEnabledTags: 0
10-19 16:47:08.853: W/Trace(4887): Unexpected value from nativeGetEnabledTags: 0
10-19 16:47:08.853: W/Trace(4887): Unexpected value from nativeGetEnabledTags: 0
10-19 16:47:08.854: W/Trace(4887): Unexpected value from nativeGetEnabledTags: 0
10-19 16:47:08.864: W/Trace(4887): Unexpected value from nativeGetEnabledTags: 0
10-19 16:47:08.864: W/Trace(4887): Unexpected value from nativeGetEnabledTags: 0
10-19 16:47:08.865: W/Trace(4887): Unexpected value from nativeGetEnabledTags: 0
10-19 16:47:08.866: W/Trace(4887): Unexpected value from nativeGetEnabledTags: 0
10-19 16:47:08.866: W/Trace(4887): Unexpected value from nativeGetEnabledTags: 0
10-19 16:47:08.867: W/Trace(4887): Unexpected value from nativeGetEnabledTags: 0
10-19 16:47:08.874: W/Trace(4887): Unexpected value from nativeGetEnabledTags: 0
10-19 16:47:08.882: W/Trace(4887): Unexpected value from nativeGetEnabledTags: 0

2 个答案:

答案 0 :(得分:2)

最后我所做的是,

@Override
public void onBackPressed() {
    super.onBackPressed();
    google_map.clear();
    location_manager.removeUpdates(this);
}

并在活动结果中删除了后堆栈中的所有活动。

Intent intent = new Intent(NearByATM.this, NearByATM.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(intent);

以及我从历史记录中删除的主要活动中的Manifiest文件,

android:excludeFromRecents="true"
                android:noHistory="true"

答案 1 :(得分:1)

您应该使用LocationClient和位置管理器,并且**您不必担心从后台堆栈**或任何其他东西中删除活动。让android系统为你处理。

在您的活动中实施GooglePlayServicesClient.ConnectionCallbacksGooglePlayServicesClient.OnConnectionFailedListener ..

实施所有必要的方法。

LocationClient lc = new LocationClient(this, this, this);

public void onStart()
{
     super.onStart();
     lc.connect();
}

..... 在实现的方法中的同一类.... 做你必要的工作 public void onConnected(Bundle b) {}

public void ondisConnected(Bundle arg0) {}

这是我看到你的代码时能告诉你的最好的。由于您调用的方法太多,我无法看到方法的实现。 如果实现此机制,则不必处理 onBackPressed()或任何其他句柄。

一些代码......

public class Example extends Activity implements GooglePlayServicesClient.ConnectionCallbacks, GooglePlayServicesClient.OnConnectionFailedListener {

public void onCreate(Bundle arg0)
{
// Initialize LocationClient here. and do necessary things.
}

public void onStart()
{
    super.onStart();
    lc.connect();
}

public void onPause()
{
   // Remove object references and reset the map components if you want
}
public void onConnected(Bundle arg0)
{
     // This will be called when you use lc.connect();  and the connection is successful otherwise onConnectionfailed() if connection fails.
     // If connected then do necessary things on GOOGLE MAPS here.
     // Whatever your code is, put here what you have shown in the sample code.
     gmap.animateCamera(CameraUpdateFactory.newLatLngZoom(YOUR_LATLNG_HERE, 15.00f));
     // The above statement will move your camera to the location with zooming and animation. 15.00f is the zoom level. you can change it.
}

public void onDisconnected(Bundle arg0)
{
     // Do on disconnect  
}

public void onConnectionFailed(ConnectionResult arg0)
{
    // Do something or leave blank if on connection failed.
}
}

此示例假定您的活动布局中有mapfragment。
希望这会有所帮助。