Here is my manifest file:
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="8" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<uses-library android:name="com.google.android.maps"/>
<activity
android:name="com.docuart.maps.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>
</application>
</manifest>
And Here is my layout:
<?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:orientation="vertical" >
<com.google.android.maps.MapView
android:id="#+id/mapGoogle"
android:enabled="true"
android:clickable="true"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:apiKey="my api key" />
</LinearLayout>
Here is my code:
public class MainActivity extends MapActivity {
private static final int MINIMUM_DISTANCE_CHANGE_FOR_UPDATES = 1;//metre
private static final int MINIMUM_TIME_BETWEEN_UPDATES = 1000;//milisaniye
protected LocationManager locationManager;
MapView mView;
MapController mapController;
GeoPoint gPoint;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mView = (MapView) findViewById(R.id.mapGoogle);
mView.displayZoomControls(true);
mView.setBuiltInZoomControls(true);
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, MINIMUM_TIME_BETWEEN_UPDATES , MINIMUM_DISTANCE_CHANGE_FOR_UPDATES, new MyLocationListener());
Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if(location != null){
gPoint = new GeoPoint((int)(location.getLatitude()*1000000),(int)(location.getLongitude()*1000000));
mapController = mView.getController();
mapController.animateTo(gPoint);
mapController.setZoom(14);
}
}
#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_main, menu);
return true;
}
public class MyLocationListener implements LocationListener{
#Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
gPoint = new GeoPoint((int)(location.getLatitude()*1000000),(int)(location.getLongitude()*1000000));
mapController = mView.getController();
mapController.animateTo(gPoint);
mapController.setZoom(14);
}
#Override
public void onProviderDisabled(String provider) {
}
#Override
public void onProviderEnabled(String provider) {
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}}
protected boolean isRouteDisplayed() {
return false;
}
}
Project running but i cant see my location. I cant find where is may fault. To see Screenshot: http://sdrv.ms/Wyf1Z1 Thank u for helping.
in your layout add your actual 2.0 auth api key in android:apiKey="my api key" />, which you must be reated from api key console .
Related
I'm doing a university project, and i've to show user location in the OSM map, and markers with data from Retrofit API.
I'm not worried about api data right now, but with my map that isn't showing.
All i got is this:MainMap
The coordinates here are random.
My map activity:
public class MapaPrincipal extends AppCompatActivity implements LocationListener {
private MapView osm;
private MapController mc;
private LocationManager locationManager;
#RequiresApi(api = Build.VERSION_CODES.M)
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mapa_principal);
osm = (MapView) findViewById(R.id.mapView);
osm.setTileSource(TileSourceFactory.MAPNIK);
osm.setBuiltInZoomControls(true);
osm.setMultiTouchControls(true);
mc = (MapController) osm.getController();
GeoPoint center = new GeoPoint(14.1, 19.2);
mc.setZoom(12);
mc.animateTo(center);
addMarker(center);
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
if (checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// Activity#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 Activity#requestPermissions for more details.
return;
}
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
}
public void addMarker(GeoPoint center){
Marker marker = new Marker(osm);
marker.setPosition(center);
marker.setAnchor(Marker.ANCHOR_CENTER, Marker.ANCHOR_BOTTOM);
osm.getOverlays().clear();
osm.getOverlays().add(marker);
osm.invalidate();
}
#Override
public void onLocationChanged(Location location) {
GeoPoint center = new GeoPoint(location.getLatitude(),location.getLongitude());
mc.animateTo(center);
addMarker(center);
}
#Override
public void onStatusChanged(String s, int i, Bundle bundle) {
}
#Override
public void onProviderEnabled(String s) {
}
#Override
public void onProviderDisabled(String s) {
}
#Override
protected void onDestroy() {
super.onDestroy();
if(locationManager!=null){
locationManager.removeUpdates(this);
}
}
}
My map layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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:orientation="vertical">
<org.osmdroid.views.MapView
android:id="#+id/mapView"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
</LinearLayout>
Android Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="pt.ipp.estg.osm">
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<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="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<application
android:usesCleartextTraffic="true"
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>
<activity android:name=".SignUpActivity" />
<activity android:name=".MapaPrincipal" />
</application>
</manifest>
Can someone help me?
Hi I am trying to getting location(latitude and longitude) from Gps and set marker on google map but it is not working.In this code I am trying to get latitude and longitude from gps Location Listener method onLocationChanged but this method never calling not showing any toast.
public class MapsFragment extends Fragment implements OnMapReadyCallback, GoogleMap.OnMapLoadedCallback {
private static View view;
private SupportMapFragment mMap;
private static Double latitude = 28.6538100, longitude = 77.2289700;
GoogleMap gMap;
private static final int PERMISSION_REQUEST_CODE = 1;
private MinDisLocationListener locationListener;
private LocationManager lm;
public MapsFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
FragmentManager fm = getActivity().getSupportFragmentManager();
SupportMapFragment mMapFragment = (SupportMapFragment) getActivity()
.getSupportFragmentManager().findFragmentById(R.id.map);
locationListener = new MinDisLocationListener();
lm = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE);
if (ContextCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION)
== PackageManager.PERMISSION_GRANTED) {
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5000, 2, this.locationListener);
} else {
requestPermission();
}
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5000, 2, this.locationListener);
if (mMapFragment == null) {
mMapFragment = SupportMapFragment.newInstance();
fm.beginTransaction().replace(R.id.map, mMapFragment).commit();
mMapFragment.getMapAsync(this);
}
view = inflater.inflate(R.layout.fragment_map, container, false);
return view;
}
#Override
public void onMapReady(GoogleMap map) {
gMap = map;
gMap.setOnMapLoadedCallback(this);
// drawMarker(latitude, longitude);
gMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new
LatLng(49.39, -124.83), 20));
gMap.addMarker(new MarkerOptions()
.position(new LatLng(37.7750, 122.4183))
.title("San Francisco")
.snippet("Population: 776733"));
gMap.getUiSettings().setZoomGesturesEnabled(true);
if (ContextCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION)
== PackageManager.PERMISSION_GRANTED) {
gMap.setMyLocationEnabled(true);
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5000, 2, this.locationListener);
} else {
requestPermission();
}
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
FragmentManager fm = getChildFragmentManager();
mMap = (SupportMapFragment) fm.findFragmentById(R.id.map);
if (mMap != null) {
mMap = SupportMapFragment.newInstance();
fm.beginTransaction().replace(R.id.map, mMap).commit();
mMap.getMapAsync(this);
}
}
private void requestPermission() {
if (ActivityCompat.shouldShowRequestPermissionRationale(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION)) {
Toast.makeText(getActivity(), "GPS permission allows us to access location data. Please allow in App Settings for additional functionality.", Toast.LENGTH_LONG).show();
} else {
ActivityCompat.requestPermissions(getActivity(), new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, PERMISSION_REQUEST_CODE);
}
}
public void drawMarker(double lat, double lon) {
if (gMap != null) {
MarkerOptions marker = new MarkerOptions().position(new LatLng(lat, lon)).title(" Maps Tutorial").snippet("Android Ruler");
marker.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_GREEN));
CameraPosition cameraPosition = new CameraPosition.Builder().target(new LatLng(latitude, longitude)).zoom(12).build();
gMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
gMap.addMarker(marker);
}
}
#Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
switch (requestCode) {
case PERMISSION_REQUEST_CODE:
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
gMap.setMyLocationEnabled(true);
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER,5000, 2, this.locationListener);
}
gMap.setMyLocationEnabled(true);
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5000, 2, this.locationListener);
} else {
}
break;
}
}
#Override
public void onResume() {
super.onResume();
FragmentManager fm = getChildFragmentManager();
mMap = (SupportMapFragment) fm.findFragmentById(R.id.map);
if (mMap != null) {
mMap = SupportMapFragment.newInstance();
fm.beginTransaction().replace(R.id.map, mMap).commit();
mMap.getMapAsync(this);
}
}
#Override
public void onDestroyView() {
super.onDestroyView();
if (mMap != null) {
mMap = null;
}
}
#Override
public void onMapLoaded() {
}
public class MinDisLocationListener implements LocationListener {
#Override
public void onLocationChanged(Location location) {
Log.d("location", "onLocationChanged");
drawMarker(location.getLatitude(),location.getLongitude());
Toast.makeText(getActivity(), "onLocationChanged", Toast.LENGTH_SHORT).show();
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {
Log.d("location", "onStatusChanged");
Toast.makeText(getActivity(), "onStatusChanged", Toast.LENGTH_SHORT).show();
}
#Override
public void onProviderEnabled(String provider) {
Log.d("location", "onProviderEnabled");
Toast.makeText(getActivity(), "onProviderEnabled", Toast.LENGTH_SHORT).show();
}
#Override
public void onProviderDisabled(String provider) {
Log.d("location", "onProviderEnabled");
Toast.makeText(getActivity(), "onProviderEnabled", Toast.LENGTH_SHORT).show();
}
}
}
Msnifist File Is this
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.deltastar.catchme" >
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permisssion.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
- See more at:
http://www.theappguruz.com/blog/android-take-photo-camera-gallery-code-sample#sthash.PtrAvZrk.dpuf
<application
android:allowBackup="true"
android:icon="#drawable/ic_logo"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme" >
<activity android:name=".LoginActivity" />
<activity
android:name=".RegisterActivity"
android:label="#string/register" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".OtpVerifyActivity"
android:label="#string/otp_verify" />
<activity android:name=".CreateProfileActivity" />
<activity android:name=".ChatMemberActivity" />
<activity android:name=".CreateGroup" />
<activity android:name=".AddMemberGroup" />
<activity android:name=".MeatPointName" />
<activity android:name=".MyMeeting" />
<activity android:name=".ChatActivity" />
<activity android:name=".GroupInfo" />
<activity
android:name=".MapChat"
android:label="#string/title_activity_map_chat" />
<activity android:name=".MyMeatingReq" />
<activity android:name=".UserProfile" />
<activity android:name=".MyProfile" />
<activity android:name=".SplashScreen" />
<activity android:name=".Settings" />
<activity
android:name=".DrawerDemo"
android:label="#string/title_activity_drawer_demo"
android:theme="#style/AppTheme" />
<!--
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=".MeetingPointLocation"
android:label="#string/title_activity_create_meating_point" />
<!--
ATTENTION: This was auto-generated to add Google Play services to your project for
App Indexing. See https://g.co/AppIndexing/AndroidStudio for more information.
-->
<meta-data
android:name="com.google.android.gms.version"
android:value="#integer/google_play_services_version" />
<activity
android:name=".HomePage"
android:label="#string/title_activity_home_page"
android:theme="#style/AppTheme" />
<activity android:name=".MyLocation" />
<activity android:name="com.services.LocDemo" >
</activity>
</application>
</manifest>
fragment_map.xml
<FrameLayout 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.fragment.MapsFragment">
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>
Your code works perfect. You just have to move your device in the place where gps is available, because the place where you are testing your app may not have exposure to GPS satellite.
onLocationChanged() will be called once GPS is detected by the device
Check your project from there
http://www.androidhive.info/2012/07/android-gps-location-manager-tutorial
But i think problem with manifest
Have you this
<permission android:name="tj.tajdev.mrking.whatisonindushanbe.permission.MAPS_RECEIVE" android:protectionLevel="signature" /> <uses-feature android:glEsVersion="0x00020000" android:required="true" /> <uses-permission android:name="android.permission.VIBRATE" /> <uses-permission android:name="tj.tajdev.mrking.whatisonindushanbe.permission.MAPS_RECEIVE" /> <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.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.READ_PHONE_STATE" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.RECORD_AUDIO" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
And this
<meta-data android:name="com.google.android.gms.version" android:value="#integer/google_play_services_version" />
I am having a small issue when starting my app. The tiles which should show up are only loaded after tapping on the map... afterwards all seems to work fine!
In logcat I see the following error:
E/EnterpriseContainerManager﹕ ContainerPolicy Service is not yet
ready!!!
public class MainActivity extends FragmentActivity {
private static final String TAG = "MAIN_ACTIVITY";
private MyLocationListener locListener;
private SupportMapFragment supportMapFragment;
private GoogleMap gmap;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.setUpMap();
list = new ArrayList<GPSCoordinates>();
new Object();
}
#Override
protected void onStart() {
super.onStart();
if (gmap == null) {
gmap = supportMapFragment.getMap();
Log.i(TAG, "gmap set!");
}
if(locListener == null) {
this.initLocationManager();
}
Log.i(TAG, "gmap start!");
}
#Override
protected void onStop() {
super.onStop();
Log.i(TAG, "gmap stop!");
}
private void setUpMap() {
GoogleMapOptions options = new GoogleMapOptions();
options.mapType(GoogleMap.MAP_TYPE_NORMAL)
.camera(CameraPosition.fromLatLngZoom(new LatLng(47.384906, 15.093149), 25));
//supportMapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
if(supportMapFragment == null) {
supportMapFragment = SupportMapFragment.newInstance(options);
getSupportFragmentManager().beginTransaction().replace(R.id.map, supportMapFragment).commit();
}
}
}
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="at.ac.unileoben.infotech.paapp.MainActivity"
tools:ignore="MissingPrefix">
<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" />
</FrameLayout>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="at.ac.unileoben.infotech.paapp">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="at.ac.unileoben.infotech.paapp.permission.MAPS_RECEIVE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<permission android:name="at.ac.unileoben.infotech.paapp.permission.MAPS_RECEIVE" android:protectionLevel="signature"/>
<uses-feature android:glEsVersion="0x00020000" android:required="true"/>
<application
android:allowBackup="true"
android:icon="#drawable/icon"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="at.ac.unileoben.infotech.paapp.MainActivity"
android:label="#string/app_name"
android:configChanges="orientation|screenSize">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="at.ac.unileoben.infotech.paapp.SettingsActivity"
android:label="#string/action_settings" />
<meta-data android:name="com.google.android.maps.v2.API_KEY" android:value="XXX" />
<meta-data android:name="com.google.android.gms.version" android:value="#integer/google_play_services_version"/>
</application>
</manifest>
edit:
i changed the code of my main_activity to the following..
public class MainActivity extends FragmentActivity {
private ArrayList<GPSCoordinates> list;
private static final String TAG = "MAIN_ACTIVITY";
private static Context context;
private MyLocationListener locListener;
private SupportMapFragment supportMapFragment;
private GoogleMap gmap;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.setUpMap();
list = new ArrayList<GPSCoordinates>();
// access context global
MainActivity.context = getApplicationContext();
new Object();
}
#Override
protected void onStart() {
super.onStart();
if (gmap == null) {
gmap = supportMapFragment.getMap();
Log.i(TAG, "gmap set!");
}
if(locListener == null) {
this.initLocationManager();
}
Log.i(TAG, "gmap start!");
}
#Override
protected void onResume() {
super.onResume();
Log.i(TAG, "gmap resume!");
}
private void setUpMap() {
if(supportMapFragment == null) {
supportMapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
}
}
later on im calling my simulatelocationmanager with:
private void initLocationManager() {
locListener = new MyLocationListener(gmap);
// Define a listener that responds to location updates
// Register the listener with the Location Manager to receive location updates
SimulateLocationManager simulatedLocationManager = new SimulateLocationManager();
//LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
simulatedLocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1, 0, locListener);
Log.i(TAG, "locListener");
}
in the locationmanager i call the function viewlocation onlocationchanged
public void viewLocation(double latitude, double longitude)
{
float bearing = this.bearing();
Log.i(TAG2, "bearing" + bearing);
gmap.animateCamera(CameraUpdateFactory.newCameraPosition(new CameraPosition.Builder()
.target(new LatLng(latitude, longitude))
.bearing(bearing)
.tilt(55f)
.zoom(gmap.getCameraPosition().zoom).build()));
}
as i allready said before the problem is that the tiles arent shown till i tap on the map..
all the other things seem to work because i can see the camera moving. but why are the tiles not loaded?
thank you for your reply!
Already accepted answer is there about your issue :Find here
If you set up the SupportMapFragment via the <fragment> element in the layout, you can call getMap() successfully in onCreate(). But, if you create the SupportMapFragment via the constructor, that's too soon -- the GoogleMap does not yet exist. You can extend SupportMapFragment and override onActivityCreated(), as getMap() is ready by then.
However, getMap() can also return null for a bigger problem, such as Google Play Services not being installed. You would need to use something like GooglePlayServicesUtil.isGooglePlayServicesAvailable() to detect this condition and deal with it however you wish.
I implemented a map method using the Google Maps API. Yesterday, it was working fine. Since then, I've made absolutely no changes whatsoever to eclipse, any map related method (including views and the MapActivity class) or anything in the its corresponding entry in the manifest - the only thing I changed was to add a splash screen, thereby changing the launcher activity from MyLITactivity to SplashActivity.
My API key is in the manifest, and I've included the uses-library entry in the manifest.
When I run the app, logcat shows this:
05-06 16:12:04.855: I/dalvikvm(753): Failed resolving Lcom/mad/mylit/MapActivity; interface 486 'Lcom/google/android/gms/maps/GoogleMap$OnMapClickListener;'
05-06 16:12:04.855: W/dalvikvm(753): Link of class 'Lcom/mad/mylit/MapActivity;' failed
05-06 16:12:04.855: E/dalvikvm(753): Could not find class 'com.mad.mylit.MapActivity', referenced from method com.mad.mylit.MyLITactivity.startMaps
05-06 16:12:04.855: W/dalvikvm(753): VFY: unable to resolve const-class 495 (Lcom/mad/mylit/MapActivity;) in Lcom/mad/mylit/MyLITactivity;
05-06 16:12:04.855: D/dalvikvm(753): VFY: replacing opcode 0x1c at 0x0002
My manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mad.mylit"
android:installLocation="auto"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="10"
android:targetSdkVersion="17" />
<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" />
<uses-feature
android:glEsVersion="0x00020000"
android:required="true" />
<application
android:allowBackup="true"
android:icon="#drawable/lit_logo"
android:label="#string/app_name"
android:theme="#style/Theme.litac" >
<activity
android:name="com.mad.mylit.MyLITactivity"
android:label="#string/app_name"
android:theme="#style/Theme.litac" >
</activity>
<activity
android:name="com.mad.mylit.ItemListActivity"
android:label="#string/title_item_list" >
</activity>
<activity
android:name="com.mad.mylit.ItemDetailActivity"
android:label="#string/title_item_detail"
android:parentActivityName=".ItemListActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".ItemListActivity" />
</activity>
<activity
android:name="com.mad.mylit.NewsDetailFragment"
android:label="#string/title_activity_news_detail_fragment" >
</activity>
<activity
android:name="com.mad.mylit.NewsListFragment"
android:label="#string/title_activity_news_list_fragment" >
</activity>
<activity
android:name="com.mad.mylit.NewsActivity"
android:label="#string/title_activity_news" >
</activity>
<activity
android:name="com.mad.mylit.DetailActivity"
android:label="SU News" >
</activity>
<activity
android:name="com.mad.mylit.TimetableActivity"
android:label="#string/title_activity_timetable" >
</activity>
<activity
android:name="com.mad.mylit.MoodleActivity"
android:label="#string/title_activity_moodle" >
</activity>
<activity
android:name="com.mad.mylit.SplashActivity"
android:configChanges="orientation|keyboardHidden|screenSize"
android:label="#string/app_name"
android:theme="#style/FullscreenTheme" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<uses-library android:name="com.google.android.maps" />
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="xxxxxxxxxxxxxxxxxxxxx" />
<activity
android:name="com.mad.mylit.MapActivity"
android:label="#string/title_activity_map"
android:parentActivityName="com.mad.mylit.MyLITactivity" >
android:theme="#style/Theme.litac"
android:uiOptions="splitActionBarWhenNarrow" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.mad.mylit.MyLITactivity" />
</activity>
</application>
</manifest>
MapActivity:
public class MapActivity extends FragmentActivity implements OnMapClickListener, OnMapLongClickListener{
final int RQS_GooglePlayServices = 1;
private GoogleMap myMap;
Location myLocation;
LocationManager locationManager;
String provider;
OnLocationChangedListener myLocationListener = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
setupActionBar();
android.support.v4.app.FragmentManager myFragmentManager = getSupportFragmentManager();
SupportMapFragment mySupportMapFragment = (SupportMapFragment)myFragmentManager.findFragmentById(R.id.map);
myMap = mySupportMapFragment.getMap();
myMap.setMyLocationEnabled(true);
myMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
myMap.setOnMapClickListener(this);
myMap.setOnMapLongClickListener(this);
LocationManager service = (LocationManager) getSystemService(LOCATION_SERVICE);
boolean enabled = service.isProviderEnabled(LocationManager.GPS_PROVIDER);
if (!enabled) {
startActivity(new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS));
}
}
public void activate(OnLocationChangedListener listener) {
myLocationListener = listener;
}
public void deactivate() {
myLocationListener = null;
}
private void setupActionBar() {
getActionBar().setDisplayHomeAsUpEnabled(true);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.maps, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
return true;
case R.id.menu_legalnotices:
String LicenseInfo = GooglePlayServicesUtil.getOpenSourceSoftwareLicenseInfo(
getApplicationContext());
AlertDialog.Builder LicenseDialog = new AlertDialog.Builder(MapActivity.this);
LicenseDialog.setTitle("Legal Notices");
LicenseDialog.setMessage(LicenseInfo);
LicenseDialog.show();
return true;
case R.id.itemid_1:
Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse("google.navigation:q=Limerick Institute of Technology, Limerick"));
startActivity(i);
return true;
case R.id.itemid_2:
//TODO change to local map of LIT
}
return super.onOptionsItemSelected(item);
}
#Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
}
public void onLocationChanged(Location location) {
myLocationListener.onLocationChanged(location);
LatLng latlng = new LatLng(location.getLatitude(),location.getLongitude());
myMap.animateCamera(CameraUpdateFactory.newLatLng(latlng));
}
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
#Override
public void onMapLongClick(LatLng point) {
//myMap.addMarker(new MarkerOptions().position(point).title(point.toString()));
}
#Override
public void onMapClick(LatLng point) {
//myMap.animateCamera(CameraUpdateFactory.newLatLng(point));
}
}
If I comment out the OnMapClickListener and OnMapLongClickListener implements (and their corresponding methods) the error disappears.
Solved: I removed and re-imported all libraries, fixed project properties and did a clean-build.
Still have no idea why it worked yesterday and not today...
I am new at Android. I have been trying from many days to make very
basic google map application but unable to complete it yet... :(
There are no errors in code, emulator running fine from terminal, Map
key also fine but still I am unable to see the map. When I run my
app only grid appears and map is not displayed. Here is the code, can
any body please help me.
public class HelloGoogleMaps extends MapActivity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
MapView mapView =(MapView)findViewById(R.id.mapview);
mapView.setBuiltInZoomControls(true);
}
protected boolean isRouteDisplayed(){
return false;
}
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<com.google.android.maps.MapView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/mapview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:clickable="true"
android:apiKey="0fyF-qSuCtdQinoUGoFbLxZoTx10Tm-YV6m6A8g"
/>
manifest file
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.android.GoogleMaps"
android:versionCode="1"
android:versionName="1.0">
<uses-permission android:name="android.permission.INTERNET"/
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<application android:icon="#drawable/icon" android:label="#string/app_name">
<uses-library android:name="com.google.android.maps" />
<uses-permission android:name="android.permission.INTERNET"/>
<activity android:name=".HelloGoogleMaps"
android:label="#string/app_name"
android:theme="#android:style/Theme.NoTitleBar">
<!--<activity android:name=".HelloGoogleMaps"
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>
<uses-sdk android:minSdkVersion="4" />
</manifest>
Don't know where is anything wrong. I am using eclipse and android 1.6
try setting internet permission outside the application tag
**// Activty**
public class MapsActivity extends MapActivity {
private MapController mapController;
private MapView mapView;
private LocationManager locationManager;
private MyOverlays itemizedoverlay;
private MyLocationOverlay myLocationOverlay;
public void onCreate(Bundle bundle) {
super.onCreate(bundle);
setContentView(R.layout.main); // bind the layout to the activity
// Configure the Map
mapView = (MapView) findViewById(R.id.map_container);
mapView.setBuiltInZoomControls(true);
// mapView.setSatellite(true);
mapView.setStreetView(true);
mapController = mapView.getController();
mapController.setZoom(14); // Zoon 1 is world view
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0,
0, new GeoUpdateHandler());
myLocationOverlay = new MyLocationOverlay(this, mapView);
mapView.getOverlays().add(myLocationOverlay);
myLocationOverlay.runOnFirstFix(new Runnable() {
public void run() {
mapView.getController().animateTo(myLocationOverlay.getMyLocation());
}
});
Drawable drawable = this.getResources().getDrawable(R.drawable.map);
itemizedoverlay = new MyOverlays(this, drawable);
createMarker();
}
protected boolean isRouteDisplayed() {
return false;
}
public class GeoUpdateHandler implements LocationListener {
public void onLocationChanged(Location location) {
int lat = (int) (location.getLatitude() * 1E6);
int lng = (int) (location.getLongitude() * 1E6);
GeoPoint point = new GeoPoint(lat, lng);
createMarker();
mapController.animateTo(point); // mapController.setCenter(point);
}
public void onProviderDisabled(String provider) {
}
public void onProviderEnabled(String provider) {
}
public void onStatusChanged(String provider, int status, Bundle extras) {
}
}
private void createMarker() {
GeoPoint p = mapView.getMapCenter();
OverlayItem overlayitem = new OverlayItem(p, "", "");
itemizedoverlay.addOverlay(overlayitem);
if (itemizedoverlay.size() > 0) {
mapView.getOverlays().add(itemizedoverlay);
}
}
protected void onResume() {
super.onResume();
myLocationOverlay.enableMyLocation();
myLocationOverlay.enableCompass();
}
protected void onPause() {
super.onPause();
myLocationOverlay.disableMyLocation();
myLocationOverlay.disableCompass();
}
}
**//Class MyOvelays**
public class MyOverlays extends ItemizedOverlay<OverlayItem> {
private static int maxNum = 5;
private OverlayItem overlays[] = new OverlayItem[maxNum];
private int index = 0;
private boolean full = false;
private Context context;
private OverlayItem previousoverlay;
public MyOverlays(Context context, Drawable defaultMarker) {
super(boundCenterBottom(defaultMarker));
this.context = context;
}
#Override
protected OverlayItem createItem(int i) {
return overlays[i];
}
#Override
public int size() {
if (full) {
return overlays.length;
} else {
return index;
}
}
public void addOverlay(OverlayItem overlay) {
if (previousoverlay != null) {
if (index < maxNum) {
overlays[index] = previousoverlay;
} else {
index = 0;
full = true;
overlays[index] = previousoverlay;
}
index++;
populate();
}
this.previousoverlay = overlay;
}
protected boolean onTap(int index) {
OverlayItem overlayItem = overlays[index];
Builder builder = new AlertDialog.Builder(context);
builder.setMessage("This will end the activity");
builder.setCancelable(true);
builder.setPositiveButton("I agree", new OkOnClickListener());
builder.setNegativeButton("No, no", new CancelOnClickListener());
AlertDialog dialog = builder.create();
dialog.show();
return true;
};
private final class CancelOnClickListener implements
DialogInterface.OnClickListener {
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(context, "You clicked yes", Toast.LENGTH_LONG)
.show();
}
}
private final class OkOnClickListener implements
DialogInterface.OnClickListener {
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(context, "You clicked no", Toast.LENGTH_LONG).show();
}
}
}
//Main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<com.google.android.maps.MapView
android:id="#+id/map_container"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:apiKey="0PYAmXmindXBuvCvIFhCUC3y0GNjJKuFJHclkVw"
android:clickable="true"
android:focusable="true"
android:keepScreenOn="true"
/>
</RelativeLayout>
//Android.mainifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.map"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="10" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<uses-library android:name="com.google.android.maps" />
<activity
android:name=".MapsActivity"
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>
</manifest>
Are you using the correct Google Maps key - if you use a debug key, the maps seem not to work.
You might have to regenerate the key without the --debug flag.
maybe this will help: Android MapView - tiles not loading with Debug API key
Common issues are that you're using the wrong emulator (ie not the Google APIs one), have the wrong imports, have the wrong API key, don't have permissions in manifest, don't have uses library etc
I wrote up a newbies guide on this, have a look
http://www.jameselsey.co.uk/blogs/techblog/android-how-to-display-a-map-the-easy-way/
I have recently been through all of these. I as well tried everything mentioned but all that worked is -
Make sure you are not a behind a proxy and remove any proxy settings you made on the emulator. There's a bug that Google Maps doesn't work on Android Emulators behind a proxy.
I think the problem is that you are using a wrong apk. You must use the apk generated by the emulator (eclipse in my case) and not the apk generated using the option "export". I think the problem is related to the key used to export the apk is not the same as the google maps api key. Try it.
PS: sorry for my bad English.