0

when i run the application it load the map but not my current location.when i clicked on gps after then it zoom in to my current location. i also want to update the location as the user moves from one location to another. this location update code is not working properly. And when i change the orientation of device it reload the map again.Same things happens again.

Xml file

 <?xml version="1.0" encoding="utf-8"?>
 <fragment xmlns:android="http://schemas.android.com/apk/res/android"
       android:id="@+id/map"
      android:layout_width="match_parent"
       android:layout_height="match_parent"
       android:name="com.google.android.gms.maps.SupportMapFragment"/>   

class file

         package com.design.googlemap;

            import android.location.Criteria;
            import android.location.Location;
            import android.location.LocationListener;
            import android.location.LocationManager;
            import android.os.Bundle;
            import android.support.v4.app.FragmentActivity;
            import android.view.Menu;
            import com.google.android.gms.maps.CameraUpdateFactory;
            import com.google.android.gms.maps.GoogleMap;
            import com.google.android.gms.maps.SupportMapFragment;
            import com.google.android.gms.maps.model.LatLng;

            public class MainActivity extends FragmentActivity{
                private GoogleMap googlemap;
                private LocationManager locationManager;
                @Override
                protected void onCreate(Bundle savedInstanceState) {
                    super.onCreate(savedInstanceState);
                    setContentView(R.layout.activity_main);
                    SetMapIfNeeded();   
                        }

                private void SetMapIfNeeded() {
                    // TODO Auto-generated method stub
                    if(googlemap==null){
                        //try to obtain google map form SupportMapFragment;
                        googlemap=((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
                        if(googlemap!= null){
                            SetUpMap();
                        }
                    }
                }

                private void SetUpMap() {
                    // Enable MyLocation Layer of Google Map
                    googlemap.setMyLocationEnabled(true);

                    // Get LocationManager object from System Service Location_SERVICE
                    locationManager =(LocationManager) getSystemService(LOCATION_SERVICE);

                    //Create a criteria object to retrieve provider
                    Criteria criteria= new Criteria();

                    //Get the name of the best provider
                    String provider = locationManager.getBestProvider(criteria,true);

                    //Get Current Location
                    Location myLocation= locationManager.getLastKnownLocation(provider);

                    //Set map type
                    googlemap.setMapType(GoogleMap.MAP_TYPE_NORMAL);

                    //Get Latitude object of current location
                    double latitude=myLocation.getLatitude();

                    //Get Latitude object of current location
                    double longitude=myLocation.getLatitude();

                    //Create a LatLng object for current location
                    LatLng latLng = new LatLng(latitude,longitude);

                    //show the current location of Google Map
                    googlemap.moveCamera(CameraUpdateFactory.newLatLng(latLng));

                    LocationListener listner = new LocationListener() {

                        public void onStatusChanged(String provider, int status, Bundle extras) {
                            // TODO Auto-generated method stub

                        }

                        public void onProviderEnabled(String provider) {
                            // TODO Auto-generated method stub

                        }

                        public void onProviderDisabled(String provider) {
                            // TODO Auto-generated method stub

                        }

                        public void onLocationChanged(Location location) {
                            // TODO Auto-generated method stub

                            locationManager =(LocationManager) getSystemService(LOCATION_SERVICE);
                        }
                    };
                    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, (LocationListener) listner);


                }

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

            }

ManifestFile

        <?xml version="1.0" encoding="utf-8"?>
        <manifest xmlns:android="http://schemas.android.com/apk/res/android"
            package="com.design.googlemap"
            android:versionCode="1"
            android:versionName="1.0" >

            <uses-sdk
                android:minSdkVersion="12"
                android:targetSdkVersion="17" />

            <permission
                android:name="com.design.googlemap.permission.MAPS_RECEIVE"
                android:protectionLevel="signature" />

             <uses-permission android:name="android.permission.INTERNET" />
            <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
            <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
            <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
            <!--
             The following two permissions are not required to use
             Google Maps Android API v2, but are recommended.
            -->
            <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
            <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
            <uses-permission android:name="com.design.googlemap.permission.MAPS_RECEIVE" />
            <uses-feature
                android:glEsVersion="0x00020000"
                android:required="true" />

            <application
                android:allowBackup="true"
                android:icon="@drawable/ic_launcher"
                android:label="@string/app_name"
                android:theme="@style/AppTheme" >

                <activity
                    android:name="com.design.googlemap.MainActivity"
                    android:label="@string/app_name" >
                    <intent-filter>
                        <action android:name="android.intent.action.MAIN" />

                        <category android:name="android.intent.category.LAUNCHER" />
                    </intent-filter>
                </activity>
                <meta-data
                    android:name="com.google.android.gms.version"
                    android:value="@integer/google_play_services_version" />
                <meta-data
                    android:name="com.google.android.maps.v2.API_KEY"
                    android:value="XXXXXXXXXXXXXXXXXXXXXXXXXXXX" />
            </application>

        </manifest>
DroidDev
  • 1,527
  • 4
  • 20
  • 42
Android Learner
  • 319
  • 2
  • 5
  • 19
  • The map will be reloaded on orientation change.. [put this lines in ur manifest to avoid this](http://stackoverflow.com/a/20607162/2591002) – SweetWisher ツ Dec 19 '13 at 08:36
  • Check this link.. [How to detect Location Provider ? GPS or Network Provider][1] [1]: http://stackoverflow.com/questions/20350665/how-to-detect-location-provider-gps-or-network-provider – Rajith Rajan Dec 19 '13 at 09:51

2 Answers2

1

1.

If you only want to show your own location, than a MyLocationOverlay is probably easiest.

Quick information here (rest of that article might be helpful too):

http://www.vogella.com/articles/AndroidLocationAPI/article.html#maps_mylocation

Use MyLocationOverlay, as that is what it is there for. Here is a sample application that uses MyLocationOverlay and a custom overlay; you can always get rid of the custom one if you do not need it.

2.

To avoid the common behavior of reloading, you have to handle configuration change for your app .

Add this line to your AndroidManifest.xml. This tells the system what configuration changes you are going to handle yourself - in this case by doing nothing.

android:configChanges="keyboardHidden|orientation|screenSize"
SweetWisher ツ
  • 7,296
  • 2
  • 30
  • 74
1
//Get the name of the best provider
String provider = locationManager.getBestProvider(criteria,false);

Even if Gps if Off. Your approx location will be pointed.

And for locationUpdates on your GoogleMaps go through this link

Also for zooming to a particular position add this code:

CameraPosition cameraPosition = new CameraPosition.Builder()
    .target(YOUR_LATLNG).zoom(ZOOM_LEVEL).build();     // int ZOOM_LEVEL=12            
    map.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));

go through this link for more on zooming.

To hide the WebView call the Visibility functions in your Activity[OnClick of your Button]

WebView wv=(WebView)findViewById(R.id.YOUR_WEB_VIEW);
    wv.setVisibility(View.GONE);// when you want to hide[shrink]
    wv.setVisibility(View.VISIBLE);// when you want to Show[Zoom]
Community
  • 1
  • 1
AabidMulani
  • 2,325
  • 1
  • 28
  • 47
  • it is pointed to my current location but i want to show that when user open my app it automattically zoom in to dat pointed location – Android Learner Dec 23 '13 at 06:01
  • i have updated my answer. Hope this is what you want! – AabidMulani Dec 23 '13 at 06:46
  • If my answer worked for you..then you can Up for my answer and close your question. Or else your question will be as unanswered – AabidMulani Dec 23 '13 at 13:20
  • sir one more thing i added a button on map when user click on that button the map will shifted to the 3/4 portion of screen and rest portion is filled contain webview. if user click on map will zoom to certain level and same in webview case. sir for this should i have to declare map again or i can include previous one into next activity. please guide me how to do it. – Android Learner Dec 24 '13 at 08:22
  • This shrinking and Expanding can be done in the same Activity. I dont find any need to create any other activity. Just use animation and do the shrinking and Zooming. And YES YOU CAN USE THE SAME MAP VIEW AGAIN – AabidMulani Dec 24 '13 at 09:17
  • i want to add webview but on startup i want only map and when user click on button then it shrink and map appear.how can i hide webview at startup. – Android Learner Dec 24 '13 at 10:00
  • Thanku very much sir :) – Android Learner Dec 27 '13 at 20:43