NPE when adding GoogleMap in fragment - android

Here's my my_location.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:animateLayoutChanges="true"
android:orientation="vertical">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/etName" />
<fragment
android:id="#+id/map"
android:name="com.google.android.gms.maps.MapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
Here's my code:
public class MyLocation extends Fragment {
private GoogleMap googleMap;
Context mContext;
public MyLocation (){ mContext = getActivity(); };
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
View root = inflater.inflate(R.layout.my_location, container, false);
googleMap = ((SupportMapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
}
}
Unfortunately, it displays NPE at the line
googleMap = ((SupportMapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
What did I do wrong?
In case, parts of my manifest
<permission
android:name="com.imincode.meniti.permission.MAPS_RECEIVE"
android:protectionLevel="signature" />
<uses-feature
android:glEsVersion="0x00020000"
android:required="true" />
<uses-permission android:name="com.imincode.meniti.maps.permission.MAPS_RECEIVE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Solved my problem with the link given in the accepted answer! Here's what I did:
I just changed
googleMap = ((SupportMapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
to
googleMap = ((SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.map)).getMap();

Your issue is same with link1 link2 link3
Please do some search before ask a question.

The issue is because it takes time for google map to load ( if you're using inside a fragment, you can expect this load time to be huge) and when you try to access map before it is being loaded, you'll get NullPointerException saying map reference went null. You should wait for the map to load first and then you should access the map views. You have pre-defined callback for that and here is how you should use it.
supportMapFragment.getMapAsync(new OnMapReadyCallback() {
#Override
public void onMapReady(GoogleMap map) {
googleMap = map; }
});

Related

Android : Google Maps API V2 displays black screen ECLIPSE

I am trying to show my current location on google maps but it displays black screen in the map fragment.
my manifest file
<?xml version="1.1" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.edraky.tasksheet"
android:versionCode="13"
android:versionName="6.7" >
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="26" />
<uses-feature
android:glEsVersion="0x00020000"
android:required="true" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="com.edraky.tasksheet.MAPS_RECEIVE" />
<permission
android:name="com.edraky.tasksheet.permission.MAPS_RECEIVE"
android:protectionLevel="signature" />
<uses-permission android:name="com.edraky.tasksheet.permission.MAPS_RECEIVE" />
<application
android:allowBackup="true"
android:icon="#drawable/transparentlogo"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".SplashScreenActivity"
android:label="#string/app_name" >
</activity>
<activity
android:name=".LoginActivity"
android:label="#string/title_activity_login" >
</activity>
<activity
android:name=".ContactUs_Activity"
android:label="#string/title_activity_contact_us" >
</activity>
<activity
android:name=".AboutUs_Activity"
android:label="#string/title_activity_about_us_"
android:noHistory="false" >
</activity>
<activity
android:name=".WelcomePage"
android:label="#string/title_activity_welcome_page" >
<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.maps.v2.API_KEY"
android:value="AIzaSyDpP-HMGNQCnFPnzODgTiJFXs00yirfhHE" />
<meta-data
android:name="com.google.android.gms.version"
android:value="#integer/google_play_services_version" />
</application>
</manifest>
ContactUs Layout ( layout displaying the map )
Contact us layout
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.edraky.tasksheet.ContactUsActivity">
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_marginTop="40dp"
android:layout_height="260dp"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:textStyle="bold|italic"
android:textSize="11dp"
android:textIsSelectable="true" />
<fragment
android:id="#+id/map"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView1"
android:layout_alignParentBottom="true"
android:layout_below="#+id/textView1"
android:layout_marginTop="24dp"
class="com.google.android.gms.maps.SupportMapFragment"
android:scrollbars="vertical" />
<Button
android:id="#+id/homebutton"
android:layout_width="100dp"
android:layout_height="40dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:background="#drawable/contactusicon" />
</RelativeLayout>
contact us activity class
public class ContactUs_Activity extends FragmentActivity {
GoogleMap mMap;
GPSTracker gps;
Double currLat;
Double currLong;
GMapV2Direction gmap;
Button homebutton;
TextView view1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_contact_us);
homebutton = (Button) findViewById(R.id.homebutton);
view1 = (TextView) findViewById(R.id.textView1);
view1.setText("Text Shown");
homebutton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
onBackPressed();
}
});
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
mMap = mapFragment.getMap();
gps = new GPSTracker(this);
currLat = gps.getLatitude();
currLong = gps.getLongitude();
LatLng sydney = new LatLng(currLat, currLong);
mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
}
}
i included all the libraries and generated the API key
output is shown below
no error message is logged. what could possibly go wrong in displaying the map ?
Note : same code is working on another application with different API key ofcourse and map is displayed correctly.
IMPORTANT NOTE:The app was already published on google play store before adding the Maps SDK API,so is there any modifications that I should do considering that or its irrelative?
Go to console.google.com
And generate a new API key with package name(com.package.app) same as in your mobile app and SHA1.
Get the new API key, replace in the code. Uninstall the app and re-run.
Okey , so i am not using FragmentActivity, and this may not directly tell the solution.
But this code definitely works and shows map using android.support.v4.app.Fragment.
import android.support.v4.app.Fragment;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapView;
import com.google.android.gms.maps.MapsInitializer;
import com.google.android.gms.maps.OnMapReadyCallback;
public class OrderMapFrag extends Fragment implements OnMapReadyCallback{
private GoogleMap mGoogleMap;
private MapView mMapView;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_order_map, container, false);
........
........
mMapView.onCreate(savedInstanceState);
mMapView.onResume();
try {
MapsInitializer.initialize(getActivity().getApplicationContext());
} catch (Exception e) {
e.printStackTrace();
}
mMapView.getMapAsync(this);
return view;
}
#Override
public void onMapReady(GoogleMap googleMap) {
mGoogleMap = googleMap;
//setting initial elements in map if required after this , as map got initialized properly
}
}
But I am not using exact same elements and permissions you use in Manifest. For me these are the used ones.
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<meta-data
android:name="com.google.android.gms.version"
android:value="#integer/google_play_services_version" />
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="MY API KEY"/>

getMap() is null in SupportMapFragment in google map in android fragment

I am using google map in a fragment and getting null when calling getMap(), this is my code:
In my MapViewFragment
private GoogleMap mGoogleMap;
I am getting null at this line, in onCreate() method...
mGoogleMap = ((SupportMapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
In fragment_map_view file
<fragment
android:id="#+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
The getMap method is deprecated. You should use getMapAsync:
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
#Override
protected void onCreate(Bundle savedInstanceState) {
// ...
((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMapAsync(this);
}
#Override
public void onMapReady(final GoogleMap googleMap) {
mGoogleMap = googleMap;
// Do something with your map
}
}
did you add the permissions and google API key ?
here is my code from project call this function in OnCreate()
private void CheckGoogleMap()
{
try {
if (googleMap == null) {
googleMap = ((MapFragment) getFragmentManager().
findFragmentById(R.id.map)).getMap();
}
googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
}
catch (Exception e) {
e.printStackTrace();
}
}
in XML file
<fragment
android:id="#+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.MapFragment"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
in Manifest file
<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.example.googlemaps.permission.MAPS_RECEIVE" />
<uses-permission android:name="android.permission.INTERNET"/>
<permission android:name="com.example.googlemaps.permission.MAPS_RECEIVE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-feature android:glEsVersion="0x00020000" android:required="true" />
before closing of application TAG in Manifest file add this
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="YOUR API KEY" />
in Gradle add
compile 'com.google.android.gms:play-services:8.1.0'
I got the reason for it, I think I should post for future readers......
Actually as I GoogleMap was called in a fragment so, in the line..
mGoogleMap = ((SupportMapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
instead of
getFragmentManager(), getSupportFragmentManager() should be used in fragments.
Corrected one-
mGoogleMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();

Can't setup Google map v2

I am trying over a week to setup my Google Map v2. but its not working for me.I tried answers from questions related to it but no use.i couldn't track the exact problem.please help.
Here is my xml file
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<fragment
android:id="#+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment"/>
</RelativeLayout>
In my map.class I have
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.maps.*;
import android.support.v4.app.FragmentActivity;
import com.google.android.gms.maps.SupportMapFragment;
import android.app.Activity;
import android.app.Dialog;
import android.support.v4.app.Fragment;
import android.widget.Toast;
import android.os.Bundle;
public class Map extends Activity {
private GoogleMap googleMap;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.map);
if (googleMap == null) {
googleMap = ((MapFragment) getFragmentManager().findFragmentById(
R.id.map)).getMap();
// check if map is created successfully or not
if (googleMap == null) {
Toast.makeText(getApplicationContext(),
"Sorry! unable to create maps", Toast.LENGTH_SHORT)
.show();
}
}
}
}
My API key is correct and I imported google play services library as per the directions.In my manifest file
<permission
android:name="com.example.sellatease.permission.MAPS_RECEIVE"
android:protectionLevel="signature" />
<uses-permission android:name="com.example.sellatease.permission.MAPS_RECEIVE" />
<uses-sdk
android:minSdkVersion="21"
android:targetSdkVersion="21" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-feature
android:glEsVersion="0x00020000"
android:required="true" />
and
<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="AIzaSyCwHLxfMRBnD_MJDQDV9IYBZTxbkCVlZsA" />
This is my error log
And in console I got can not find googleplayserviceslib.apk error too.
new error log
you need to extend FragmentActivity/AppCompatActivity if you are using SupportMapFragment.
public class Map extends FragmentActivity/AppCompatActivity {
And
FragmentManager myFragmentManager = getSupportFragmentManager();
SupportMapFragment mySupportMapFragment = (SupportMapFragment)myFragmentManager.findFragmentById(R.id.map);
googleMap = mySupportMapFragment.getMap();
You can check Android Map V2
Try this
googleMap = ((SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
Try this...
activity_map.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<fragment
android:id="#+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment"/>
</RelativeLayout>
Map.java
public class Map extends android.support.v4.app.FragmentActivity {
private GoogleMap mMap;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_map);
setUpMapIfNeeded();
}
#Override
protected void onResume() {
super.onResume();
setUpMapIfNeeded();
}
private void setUpMapIfNeeded() {
if (mMap == null) {
mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
.getMap();
if (mMap != null) {
setUpMap();
}
}
}
AndroidManifest.xml
....
<permission
android:name="com.example.sellatease.permission.MAPS_RECEIVE"
android:protectionLevel="signature" />
<uses-sdk
android:minSdkVersion="21"
android:targetSdkVersion="21" />
<uses-permission
android:name="com.example.sellatease.permission.MAPS_RECEIVE"/>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<!-- Maps API needs OpenGL ES 2.0. -->
<uses-feature
android:glEsVersion="0x00020000"
android:required="true" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<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="AIzaSyCwHLxfMRBnD_MJDQDV9IYBZTxbkCVlZsA" />
<activity
android:name=".Map"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
....
UPDATE
Create Google map programatically.
activity_map.xml
<FrameLayout
android:gravity="top"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:id="#+id/mapContainer"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</FrameLayout>
Map.java
public class Map extends AppCompatActivity{
private GoogleMap mMap;
private SupportMapFragment mMapFragment;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_map);
mMapFragment = SupportMapFragment.newInstance();
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.add(R.id.mapContainer, mMapFragment, "map");
fragmentTransaction.commit();
mMapFragment.getMapAsync(mapReadyCallback);
}
private OnMapReadyCallback mapReadyCallback = new OnMapReadyCallback() {
#Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
mMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
mMap.setMyLocationEnabled(true);
mMap.setTrafficEnabled(true);
mMap.setIndoorEnabled(true);
mMap.setBuildingsEnabled(true);
mMap.getUiSettings().setZoomControlsEnabled(true);
mMap.getUiSettings().setCompassEnabled(true);
}
};

Google Maps V2 Not Woking

I m using google maps v2 and getting this error.
Could not find class 'maps.af.k', referenced from method maps.ag.an.a
I m following all the rules correctly. I have created SHA1 finger print and added it to ANdroid Key for apikey with projct name
1F:41:29:EC:E8:07:3C:A9:F8:6E:EB:D2:7B:42:41:62:3A:36:CA:F2;com.example.routes
Then in mainest file i m using
<fragment
android:name="com.google.android.gms.maps.SupportMapFragment"
android:id="#+id/map"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
i have also imported the googleplayservices and correctly added it to the project
My code is
public class MainActivity extends FragmentActivity {
GoogleMap googleMap;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SupportMapFragment fragment = new SupportMapFragment();
getSupportFragmentManager().beginTransaction()
.add(android.R.id.content, fragment).commit();
;
googleMap = ((SupportMapFragment)
getSupportFragmentManager().findFragmentById(
R.id.map)).getMap();
}
}
but still i m getting that error.
Try to extend Activity rather then FragmentActivity:
public class MainActivity extends Activity implements LocationListener{
LocationManager locationManager;
private GoogleMap googleMap;
double lat=17.385044;
double long=78.486671;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LocationManager service=(LocationManager)getSystemService(LOCATION_SERVICE);
locationManager=(LocationManager)getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
provider = locationManager.getBestProvider(criteria, false);
Location location = locationManager.getLastKnownLocation(provider);
private void initilizeMap(){
if(googleMap==null){
googleMap=((MapFragment)getFragmentManager().findFragmentById(R.id.map)).getMap();
//check if map is created succesfully or not
if(googleMap==null){
Toast.makeText(getApplicationContext(),"sorry unable to create map",1).show();
}
}
MarkerOptions markerOptions=new MarkerOptions().position(new LatLng(lat,long));
markerOptions.icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_launcher));
CameraPosition cameraPosition=new CameraPosition.Builder().target(new LatLng(17.385044, 78.486671)).zoom(12).build();
googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
googleMap.setMyLocationEnabled(true);
googleMap.setOnInfoWindowClickListener(null);
}
#Override
protected void onResume(){
super.onResume();
initilizeMap();
}
I had the same problem and my solution was to remove and add the googleplayservices again.
When you are using SupportMapFragment no need of extending FragmentActivity just extend the Activity See the below example code:
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
public class MainActivity extends Activity {
private GoogleMap map;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
}
#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;
}
}
Note: You have to add google play services library project as a reference to your project, your package name must be com.example.routes you have to add the map fragment in your xml layout not in the manifest.
Make following changes to your Manifest.xml
Add following permissions.
<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"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
Add the OpenGL
<uses-feature
android:glEsVersion="0x00020000"
android:required="true"/>
Add the API key
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="API_KEY"/>
Example manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.package"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<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"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<used-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.example.package.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.maps.v2.API_KEY"
android:value="YOUR_API_KEY_HERE"/>
</application>

How update user's current location in google map

Hi Friends
I want to display user's current location in Google map and add marker of his location in the map and how can i Update their location in google map after every second ..???
myLocationOverlay = new MyLocationOverlay(this, mapView);
mapView.getOverlays().add(myLocationOverlay);
myLocationOverlay.enableMyLocation();
myLocationOverlay.runOnFirstFix(addfirststandort);
java file
public class MYGIRNE extends FragmentActivity {
GoogleMap map;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mygirne);
SupportMapFragment fm = (SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.map);
// Getting Map for the SupportMapFragment
map = fm.getMap();
if(map!=null){
// Enable MyLocation Button in the Map
map.setMyLocationEnabled(true);
LocationManager manager=(LocationManager) getSystemService(LOCATION_SERVICE);
Criteria criteria=new Criteria();
String provider =manager.getBestProvider(criteria, true);
Location cloc=manager.getLastKnownLocation(provider);
double lat=0;
double lng =0;
lat =cloc.getLatitude();
lng= cloc.getLongitude();
LatLng latlng = new LatLng (lat,lng);
map.moveCamera(CameraUpdateFactory.newLatLng(latlng));
map.animateCamera(CameraUpdateFactory.zoomTo(15));
map.addMarker(new MarkerOptions()
.title("Your Current Location")
.position(latlng));
.icon(BitmapDescriptorFactory.defaultMarker(
BitmapDescriptorFactory.HUE_AZURE)));
}
}
xml file
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.firsttry.cyptour.MYGIRNE" >
<fragment
android:id="#+id/map"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
class="com.google.android.gms.maps.SupportMapFragment" />
</RelativeLayout>
also include the following in your manifest file
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="enter your APIKEY" />
<meta-data
android:name="com.google.android.gms.version"
android:value="#integer/google_play_services_version" />
this code should be an element of application
then include this under the uses permission of your manifest file
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<!-- External storage for caching. -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<!-- My Location -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<!-- Maps API needs OpenGL ES 2.0. -->
<uses-feature
android:glEsVersion="0x00020000"
android:required="true" />
i hope this helps

Categories

Resources