简体   繁体   中英

how to show map of specific city in google map when app start

below is my code which display map of all world i want when application start is show specific city not world map i follow this tutorial http://android-er.blogspot.com/2013/01/google-maps-android-api-v2-example-draw.html every thing work fine but i want when app start first time is display specific city map not all global map i dont want show globap map like this image http://1.bp.blogspot.com/-dwjqTWCdONg/UObUk1syAFI/AAAAAAAAGy8/S43YMKuXZRc/s1600/screen_MapsAPIv2_Polygon.png on app start i want specific city map like newyork for example.

       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;
}

}

add this code to your 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);
}

After the onClikListener are setted (into onCreate), put this code

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);

It will animate your map and move the camera to NY

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM