如何在应用启动时在谷歌地图中显示特定城市的地图

时间:2013-11-15 20:02:34

标签: android google-maps

下面是我的代码,显示我想要的所有世界的地图,当应用程序开始显示特定城市而不是世界地图我按照本教程http://android-er.blogspot.com/2013/01/google-maps-android-api-v2-example-draw.html每件事情都很好但我想第一次app开始时显示特定城市地图不是所有的全球地图我不想要在应用程序开始时像这个图像http://1.bp.blogspot.com/-dwjqTWCdONg/UObUk1syAFI/AAAAAAAAGy8/S43YMKuXZRc/s1600/screen_MapsAPIv2_Polygon.png一样显示globap地图我想要像newyork这样的特定城市地图。

       public class MainActivity extends Activity 
implements OnMapClickListener, OnMapLongClickListener, OnMarkerClickListener{

final int RQS_GooglePlayServices = 1;
private GoogleMap myMap;

Location myLocation;
TextView tvLocInfo;

boolean markerClicked;
PolygonOptions polygonOptions;
Polygon polygon;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    tvLocInfo = (TextView)findViewById(R.id.locinfo);

    FragmentManager myFragmentManager = getFragmentManager();
    MapFragment myMapFragment 
        = (MapFragment)myFragmentManager.findFragmentById(R.id.map);
    myMap = myMapFragment.getMap();

    myMap.setMyLocationEnabled(true);

    myMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);

    myMap.setOnMapClickListener(this);
    myMap.setOnMapLongClickListener(this);
    myMap.setOnMarkerClickListener(this);

    markerClicked = false;
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}



@Override
protected void onResume() {

    super.onResume();

    int resultCode = 
  GooglePlayServicesUtil.isGooglePlayServicesAvailable(getApplicationContext());

    if (resultCode == ConnectionResult.SUCCESS){
        Toast.makeText(getApplicationContext(), 
                "isGooglePlayServicesAvailable SUCCESS", 
                Toast.LENGTH_LONG).show();
    }else{
        GooglePlayServicesUtil.getErrorDialog(resultCode, this, 
   RQS_GooglePlayServices);
    }

}

@Override
public void onMapClick(LatLng point) {
    tvLocInfo.setText(point.toString());
    myMap.animateCamera(CameraUpdateFactory.newLatLng(point));

    markerClicked = false;
}

@Override
public void onMapLongClick(LatLng point) {
    tvLocInfo.setText("New marker added@" + point.toString());
    myMap.addMarker(new 
   MarkerOptions().position(point).title(point.toString()));

    markerClicked = false;
}

@Override
public boolean onMarkerClick(Marker marker) {

    if(markerClicked){

        if(polygon != null){
            polygon.remove();
            polygon = null;
        }

        polygonOptions.add(marker.getPosition());
        polygonOptions.strokeColor(Color.RED);
        polygonOptions.fillColor(Color.BLUE);
        polygon = myMap.addPolygon(polygonOptions);
    }else{
        if(polygon != null){
            polygon.remove();
            polygon = null;
        }

        polygonOptions = new PolygonOptions().add(marker.getPosition());
        markerClicked = true;
    }

    return true;
}

}

2 个答案:

答案 0 :(得分:2)

将此代码添加到您的onCreate:

@Override
protected void onCreate(Bundle savedInstanceState) {
.
.
.
   // define point to center on
   LatLng origin = new LatLng(40.67, -73.94);
   CameraUpdate panToOrigin = CameraUpdateFactory.newLatLng(origin);
   myMap.moveCamera(panToOrigin);

   // set zoom level with animation
   myMap.animateCamera(CameraUpdateFactory.zoomTo(14), 400, null);
}

答案 1 :(得分:1)

设置onClikListener后(进入onCreate),输入此代码

LatLng NewYork= new LatLng(40.714623,-74.006605);
CameraPosition camPos = new CameraPosition.Builder().target(NewYork).zoom(14).build();
CameraUpdate cam = CameraUpdateFactory.newCameraPosition(camPos);
myMap.animateCamera(cam);

它会动画地图并将相机移动到纽约