setMyLocation Button will not pop up when application loads - android

I'm trying to display the basic setMyLocation Button when my application loads the map but every time I do so the application crashes and says java.lang.SecurityException: my location requires permission ACCESS_FINE_LOCATION or ACCESS_COARSE_LOCATION
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3288) I have the necessary permissions included in the manifest
public class MainActivity extends AppCompatActivity {
public GoogleMap map;
Location location;
//double lat, longit;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
map = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
map.setMyLocationEnabled(true);
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.cnit425.patrickwalker.assignment2">
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission
android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<!-- GoogleMap API v2 Permission -->
<uses-permission android:name="org.smart_laboratory.laptop.locationmaptest.permission.MAPS_RECEIVE" />
<!-- GoogleMap API v2 Location related Permission -->
<uses-permission android:name="android.permission.INTERNET" />
<!-- GPS & Storage Permission -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<!-- OpenGL Use Setting -->
<uses-feature android:glEsVersion="0x00020000" android:required="true" />
<uses-library android:name="com.google.android.maps"/>
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="AIzaSyCMmSB3RP5MOu7WqZgBBh5jpz-LXy6gQDo"/>
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>

Related

Android-SupportMapFragment is showing a blank map

I want to show supportMapFragment to fill my main activity and by the way I requested location permissions during run time and it was granted successfully.What happens is that I get a blank view of maps and nothing else.
And I gave the api key as well which is expected to be correct
Here's my code for the main activity:
public class MainActivity extends AppCompatActivity {
private static final int REQUEST_ID_MULTIPLE_PERMISSIONS = 1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RequestPermissions requestPermissions = new RequestPermissions(this, this);
requestPermissions.checkAndRequestPermissions();
FragmentManager fmanager = getSupportFragmentManager();
Fragment fragment = fmanager.findFragmentById(R.id.map);
SupportMapFragment supportmapfragment = (SupportMapFragment)fragment;
GoogleMap supportMap = supportmapfragment.getMap();
}
#Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
switch (requestCode) {
case REQUEST_ID_MULTIPLE_PERMISSIONS: {
Map<String, Integer> perms = new HashMap<String, Integer>();
// Initialize the map with both permissions
perms.put(android.Manifest.permission.ACCESS_COARSE_LOCATION, PackageManager.PERMISSION_GRANTED);
perms.put(android.Manifest.permission.ACCESS_FINE_LOCATION, PackageManager.PERMISSION_GRANTED);
// Fill with actual results from user
if (grantResults.length > 0) {
for (int i = 0; i < permissions.length; i++)
perms.put(permissions[i], grantResults[i]);
}
}
}
}
}
and here's my manifest:
<?xml version="1.0" encoding="utf-8"?>
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<uses-feature
android:glEsVersion="0x00020000"
android:required="true" />
<uses-feature android:name="android.hardware.location.gps"
android:required="true"/>
<uses-feature android:name="android.hardware.location.network"
android:required="true"/>
<uses-feature
android:name="android.hardware.location"
android:required="true" />
<permission
android:name="com.truiton.supportmapfragment.permission.MAPS_RECEIVE"
android:protectionLevel="signature" />
<uses-permission android:name="com.truiton.supportmapfragment.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.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" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".MainActivity">
<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="#string/MapsKey" />
</application>
activity xml:
<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"
tools:context="com.example.khairy.fm2apps.MainActivity" >
<fragment
android:id="#+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment" />
Right Click on your Project and go through below Image ;
try below code in your onCreate() method
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RequestPermissions requestPermissions = new RequestPermissions(this, this);
requestPermissions.checkAndRequestPermissions();
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
Make sure you enabled the Maps SDK for Android for your project in the Google Cloud Platform over the APIs & Services section

"network" location provider requires ACCESS_COARSE_LOCATION or ACCESS_FINE_LOCATION permission in GPS Location App

I have a problem with my GPS Location App I have tried by all means and I can't find the solution (I'm developing in Android 6.0) , this is the problem:
"network" location provider requires ACCESS_COARSE_LOCATION or ACCESS_FINE_LOCATION permission
this is my XML FIle:
<?xml version="1.0" encoding="utf-8"?>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.READ_LOGS" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.location.gps" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".chooseEmergency"></activity>
</application>
And this is my LocationManager Code:
LocationManager mlocManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Localizacion Local = new Localizacion();
Local.setMainActivity(this);
mlocManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0,
(LocationListener) Local);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
//use checkSelfPermission()
if (checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
&& checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
} else {
//simply use the required feature
//as the user has already granted permission to them during installation
}
ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 1);
ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.ACCESS_COARSE_LOCATION}, 1);
public class Localizacion implements LocationListener {
MainActivity mainActivity;
public MainActivity getMainActivity() {
return mainActivity;
}
public void setMainActivity(MainActivity mainActivity) {
this.mainActivity = mainActivity;
}
#Override
public void onLocationChanged(Location loc) {
latitud = loc.getLatitude();
longitud = loc.getLongitude();
this.mainActivity.setLocation(loc);
}
#Override
public void onProviderDisabled(String provider) {//pndiente de escribir;
}
#Override
public void onProviderEnabled(String provider) {//pendiente
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
}
Please help, I took 2 days looking at internet solutions and nothing works for me

To obtain the current location

I am using the below code to obtain the location of a user
public class MainLocation extends Application implements LocationListener {
protected LocationManager locationManager;
protected LocationListener locationListener;
protected Context context;
String lat;
String provider;
protected String latitude, longitude;
protected boolean gps_enabled, network_enabled;
#Override
public void onCreate() {
super.onCreate();
Log.e("Location", "here");
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// public void requestPermissions(#NonNull String[] permissions, int requestCode)
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for Activity#requestPermissions for more details.
return;
}
}
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
}
#Override
public void onLocationChanged(Location location) {
Log.e("Location", "" + location.getLatitude());
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {
Log.e("Location Status", "" + provider + " " + status + " " + extras);
}
#Override
public void onProviderEnabled(String provider) {
Log.e("Location", "Enabled");
}
#Override
public void onProviderDisabled(String provider) {
Log.e("Location","Disabled"+provider);
}
}
but here the onLocationChanged function is never getting called, so i m not able fetch the location. So how can i get the location?
In the logcat, the info tab is showing the latitude and longitude under the title onLocationReceived".
This is the manifest,
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.example.krijjj.loginapp" >
<!--
The ACCESS_COARSE/FINE_LOCATION permissions are not required to use
Google Maps Android API v2, but are recommended.
-->
<uses-sdk android:minSdkVersion="15" />
<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.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<application
android:name=".Location.MainLocation"
android:allowBackup="true"
android:icon="#drawable/icon1"
android:label="#string/app_name"
android:theme="#style/AppTheme"
tools:replace="android:label" >
<activity
android:name=".Track"
android:label="Track" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".Security.Lockscreen"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".Second"
android:label="#string/title_activity_second" >
<intent-filter>
<action android:name="com.example.krijjj.loginapp.Second" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<meta-data
android:name="com.google.android.gms.version"
android:value="#integer/google_play_services_version" />
<activity
android:name=".MainActivity"
android:label="#string/title_activity_mapactivity" >
</activity>
<activity
android:name=".Security.MainActivity"
android:label="#string/title_activity_mapactivity" >
</activity>
<!--
The API key for Google Maps-based APIs is defined as a string resource.
(See the file "res/values/google_maps_api.xml").
Note that the API key is linked to the encryption key used to sign the APK.
You need a different API key for each encryption key, including the release key that is used to
sign the APK for publishing.
You can define the keys for the debug and release targets in src/debug/ and src/release/.
-->
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="#string/google_maps_key" />
<activity
android:name=".MapsActivity"
android:label="#string/title_activity_maps" >
</activity>
</application>
</manifest>
The Google Play services location APIs are preferred over the Android framework location APIs (android.location) as a way of adding location awareness to your app.
You can find the example how to use it right here.
P.S. Don't forget to check if permissions granted on your device, if you added permissions into manifest file...

Android ACCESS_FINE_LOCATION permission not being granted or something?

AI have made a simple app that displays a marker at user's current location and I have used all permissions in the manifest. Here is the code:
public class LocationActivity extends FragmentActivity implements LocationListener {
SupportMapFragment map;
GoogleMap googleMap;
LocationManager loc;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_location);
map=(SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
onMapReady(googleMap);
//onLocationChanged(location);
}
public void onMapReady(GoogleMap googleMap){
try {
googleMap = map.getMap();
if (googleMap != null) {
googleMap.setMyLocationEnabled(true);
loc = (LocationManager) getSystemService(LOCATION_SERVICE);
Criteria crit = new Criteria();
String best = loc.getBestProvider(crit,true);
Location location = loc.getLastKnownLocation(best);
LatLng myLocation = new LatLng(location.getLatitude(), location.getLongitude());
googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(myLocation, 15));
googleMap.addMarker(new MarkerOptions().title("You're here").position(myLocation));
} else {
Toast.makeText(this, "Fragment has a problem", Toast.LENGTH_SHORT).show();
}
}
catch (Exception e){
Toast.makeText(this,"we can't do it",Toast.LENGTH_SHORT).show();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_location, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
#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
}
#Override
public void onLocationChanged(Location location) {
//something will go on here
}}
But it shows a runtime error:
ACCESS_FINE_LOCATION is required for PRIORITY_HIGH_LOCATION_REQUEST
What is the probable reason here? Although the code compiles well if I remove this line -
googleMap.setMyLocationEnabled(true);
But the location is not accurate.`
Here is the manifest file
<?xml version="1.0" encoding="utf-8"?>
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="API_Key"/>
<meta-data
android:name="com.google.android.gms.version"
android:value="#integer/google_play_services_version" />
<android:uses-permission android:name="android.permission.READ_PHONE_STATE" />
<android:uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<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" />
<permission android:name="com.example.permission.MAPS_RECEIVE" android:protectionLevel="signature" />
<uses-permission android:name="com.example.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"/>
<activity
android:name=".LockScreenActivity"
android:label="#string/title_activity_lock_screen" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".LocationActivity"
android:label="#string/title_activity_location" >
</activity>
</application>
Two problems:
All of your <uses-permission>, <permission>, and <uses-feature> elements need to move outside of <application>. They should be immediate children of the root <manifest> element.
You do not use the android: prefix on element names, as you have with READ_PHONE_STATE and READ_EXTERNAL_STORAGE. You do not need READ_EXTERNAL_STORAGE anyway, as you are requesting WRITE_EXTERNAL_STORAGE.
put All below snippet outside application tag
<android:uses-permission android:name="android.permission.READ_PHONE_STATE" />
<android:uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<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" />
<permission android:name="com.example.permission.MAPS_RECEIVE" android:protectionLevel="signature" />
<uses-permission android:name="com.example.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"/>
And also add your package name at the time of adding permission
<permission android:name="yourpackagename.permission.MAPS_RECEIVE" android:protectionLevel="signature" />
<uses-permission android:name="yourpackagename.permission.MAPS_RECEIVE" />

Google Map view shows only grey screen with zoom button

I am Using android Google map API-2 for my application. When clicking map view it shows only a screen with zoom button.
my AndroidManifest.xml file
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.burusoth.advertise"
android:versionCode="1"
android:versionName="1.0" >
<permission
android:name="com.burusoth.advertise.permission.MAPS_RECEIVE"
android:protectionLevel="signature" />
<uses-permission android:name="com.burusoth.advertise.permission.MAPS_RECEIVE" />
<uses-permission android:name="android.permission.CALL_PHONE" >
</uses-permission>
<uses-permission android:name="android.permission.INTERNET" >
</uses-permission>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<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" />
<!-- Required to show current location -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="17" />
<!-- Required OpenGL ES 2.0. for Maps V2 -->
<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.burusoth.advertise.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>
<activity
android:name="com.burusoth.advertise.View_Advertisements"
android:label="#string/title_activity_view__advertisements" >
</activity>
<activity
android:name="com.burusoth.advertise.Gadget_view"
android:label="#string/title_activity_gadget_view"
android:windowSoftInputMode="stateHidden" >
</activity>
<activity
android:name="com.burusoth.advertise.Activity_post_advertisements"
android:label="#string/title_activity_activity_post_advertisements" >
</activity>
<activity
android:name="com.burusoth.advertise.Post_Advertisement"
android:label="#string/title_activity_post__advertisement" >
</activity>
<activity
android:name="com.burusoth.advertise.Popup"
android:label="#string/title_activity_popup" >
</activity>
<activity
android:name="com.burusoth.advertise.Google_Map"
android:label="#string/title_activity_google__map" >
</activity>
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="AIzaSyAKCvpyR3v4YStDJDoibNgoblkdch0F254" />
</application>
my activity xml file
<RelativeLayoutxmlns: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"
tools:context=".Google_Map" >
<fragment
android:id="#+id/map"
android:name="com.google.android.gms.maps.MapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>
my Activity .java Activity file for calling map
public class Google_Map extends Activity {
private GoogleMap googleMap=null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_google__map);
try {
// Loading map
initilizeMap();
} catch (Exception e) {
e.printStackTrace();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_google__map, menu);
return true;
}
private void initilizeMap() {
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();
}
}
}
#Override
protected void onResume() {
super.onResume();
initilizeMap();
}
}
Here with I would like to share one example which run under Google Map API v2.
You can go through that steps or else you can download full example from below link.
This is working example. Please test this in real device.
Google Maps API v2 using Android
Please provide your comment if you have any question.
You need to put API_KEY in android manifest
Get from :
Google Map API_KEY

Categories

Resources