Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Easy methods of incorporating Google maps for Android

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 533
    Comment on it

    First create a layout for Map Activity. The name of layout is activity_main.xml

     <fragment
                    xmlns:android="http://schemas.android.com/apk/res/android"
                    android:id="@+id/map"
                    class="com.google.android.gms.maps.SupportMapFragment"
                    android:layout&#95;width="match&#95;parent"
                    android:layout&#95;height="match&#95;parent"/>
    

    To add markerr in a specific lat long and move the map focus to this position.
    For this use the following line of code:-

     // Get a handle to the Map Fragment
            GoogleMap map = ((SupportMapFragment) getSupportFragmentManager()
                    .findFragmentById(R.id.map)).getMap();
    
            LatLng dehradun= new LatLng(30.316495, 78.032192);
    
            map.setMyLocationEnabled(true);
            map.moveCamera(CameraUpdateFactory.newLatLngZoom(dehradun, 13));
    
            map.addMarker(new MarkerOptions()
                    .title("Dehradun")
                    .snippet("Capital of Uttarakhand")
                    .position(dehradun));
    

    We can also change the type of Map. Following are the types of map:- 1) Normal 2) Hybrid 3) Terrain 4) None

    To set the map type use following lines of code:-

    GoogleMap map = ((SupportMapFragment) getSupportFragmentManager()
                    .findFragmentById(R.id.map)).getMap();   
    
     map.moveCamera(CameraUpdateFactory.newLatLngZoom(
                new LatLng(30.316495, 78.032192), 2));
    
    // Other supported types include: MAP_TYPE_NORMAL ,
        // MAP_TYPE_TERRAIN, MAP_TYPE_HYBRID and MAP_TYPE_NONE
        map.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
    
    

    To make path and routes on the map we use Polyline
    Following line of code gives the example of how to make paths and routes on the map

     GoogleMap map = ((SupportMapFragment) getSupportFragmentManager()
                    .findFragmentById(R.id.map)).getMap();
    
            map.moveCamera(CameraUpdateFactory.newLatLngZoom(
                    new LatLng(30.316495, 78.032192), 2));
    
            // Polylines are useful for marking paths and routes on the map.
            map.addPolyline(new PolylineOptions().geodesic(true)
                    .add(new LatLng(28.635308, 77.224960))  // New Delhi
                    .add(new LatLng(-18.142, 178.431))  // Dehradun
                    .add(new LatLng(22.572646, 88.363895))  // Kolkata
                    .add(new LatLng(19.075984, 72.877656))  // Mumbai
            );
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: