Tuesday, April 19, 2016

Google Map API V2 Part-II (Add Marker with Google Map)

" Jai Saraswati Maa"

Please follow

Google Map API V2 Part-I (Prepare yourself to do fun with Google Map)


Extra add on from previous example ....
1. implements OnMapReadyCallback interface
2. Add fragment with Async
3. Implements callback method
4. Add marker 

A. Output will be like  - You have to Zoom Out 
   




B. xml layout file is same as in Part-I

C. Java file MapsActivity.java is as 


package com.exam.ravi.googlemapex1;

import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.CameraPosition;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

public class MapsActivity extends AppCompatActivity implements OnMapReadyCallback
 {

    private GoogleMap mMap;

    @Override    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_maps);
  SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
     .findFragmentById(R.id.map);
     mapFragment.getMapAsync(this);
}

@Overridepublic void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;
LatLng jaipur = new LatLng(26.923952,75.826743);
MarkerOptions mo = new MarkerOptions();
mo.position(jaipur).title("HawaMahal Jaipur").snippet("Padahro Mhare Desh...");
mMap.addMarker(mo);
mMap.moveCamera(CameraUpdateFactory.newLatLng(jaipur));
   }
}


D.....   Just change inside the onMapReady method like you will get already zoomed out output

Like    ---  

@Overridepublic void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;

    LatLng jaipur = new LatLng(26.923952,75.826743);
    CameraPosition cameraPosition = CameraPosition.builder()
      .zoom(17).tilt(20).bearing(90).target(jaipur).build();
    MarkerOptions mo = new MarkerOptions();
    mo.position(jaipur).title("HawaMahal Jaipur").snippet("Padahro Mhare Desh...");
    mMap.addMarker(mo);
    mMap.moveCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
}

E.  If you will change the body of onMapReady function you will get output like 
          
            



No comments:

Post a Comment