Click here to Skip to main content
15,883,901 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I am trying to add google map in android app. But import com.google.android.gms.maps.GoogleMap is not working I have googled very much not found solution. Can any one help me plz.
Below is my code:



Java
package in.wptrafficanalyzer.locationingooglemap;

import android.app.Dialog;

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.widget.TextView;


import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;
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;
import com.makemyapp.ss.R;


public class MainActivity extends FragmentActivity implements LocationListener 


{
	
        GoogleMap googleMap;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.locateme);
		
		// Getting Google Play availability status
        int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getBaseContext());

        // Showing status
        if(status!=ConnectionResult.SUCCESS){ // Google Play Services are not available
        	
        	int requestCode = 10;
            Dialog dialog = GooglePlayServicesUtil.getErrorDialog(status, this, requestCode);
            dialog.show();
            
        }
        
        else 
        
        {	
        	
        	// Google Play Services are available	
		
			// Getting reference to the SupportMapFragment of activity_main.xml
			
        	SupportMapFragment fm = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
			
			// Getting GoogleMap object from the fragment
			
			googleMap = fm.getMap();
			
			// Enabling MyLocation Layer of Google Map
			
			googleMap.setMyLocationEnabled(true);				
					
			
			 // Getting LocationManager object from System Service LOCATION_SERVICE
	        LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
	
	        // Creating a criteria object to retrieve provider
	        Criteria criteria = new Criteria();
	
	        // Getting the name of the best provider
	        String provider = locationManager.getBestProvider(criteria, true);
	
	        // Getting Current Location
	        Location location = locationManager.getLastKnownLocation(provider);
	
	        if(location!=null){
	                onLocationChanged(location);
	        }
	
	        locationManager.requestLocationUpdates(provider, 20000, 0, this);
        }
		
	}
	

	@Override
	public void onLocationChanged(Location location) {
		
		TextView tvLocation = (TextView) findViewById(R.id.tv_location);
		
		// Getting latitude of the current location
		double latitude = location.getLatitude();
		
		// Getting longitude of the current location
		double longitude = location.getLongitude();		
		
		// Creating a LatLng object for the current location
		LatLng latLng = new LatLng(latitude, longitude);
		
		// Showing the current location in Google Map
		googleMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
		
		// Zoom in the Google Map
		googleMap.animateCamera(CameraUpdateFactory.zoomTo(15));		
		
		
		// Setting latitude and longitude in the TextView tv_location
	//	tvLocation.setText("Latitude:" +  latitude  + ", Longitude:"+ longitude );		
				
	}

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

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

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

}
Posted
Updated 6-Nov-13 16:48pm
v2
Comments
Richard MacCutchan 6-Nov-13 16:49pm    
What does "not working" mean?
Sumit Kumar Singh India 6-Nov-13 16:56pm    
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;
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;

The above lines showing error "com.google.android.gms cannot be resolved"
Richard MacCutchan 6-Nov-13 16:58pm    
Then you are missing a .jar file that contains those classes.
Sumit Kumar Singh India 6-Nov-13 17:04pm    
I am not able to understand which .jar file I have missing. and where I need to add it. As I have already added Google Play Service.
Can you suggest me plz?
Richard MacCutchan 7-Nov-13 4:50am    
I have no idea, you need to check the documentation for the product you are using and ensure that all .jar files are in the Java class directories.

http://www.java2s.com/Code/Jar/g/Downloadgoogleplayservicesjar.htm

Download the package from here! It Works!
 
Share this answer
 
sdk->extra->google->google-play service
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900