I'm trying to make application where user has an option to click on button "Find on Map".
If user clicks "Find on Map" button, I would like to start new activity that contains google map.
I did this tutorial and map shows.
http://mobiforge.com/developing/story/using-google-maps-android
However this tutorial shows map in the main activity. That is why I made ViewMap activity which is exactly the same as the main activity in the tutorial showed above.
This is how I start ViewMap activity:
findOnMapButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// create an Intent to launch the ViewMap Activity
Intent viewMap = new Intent(ViewCourse.this, ViewMap.class);
startActivity(viewMap); // start the ViewContact Activity
}
});
Here is ViewMap.java:
package com.ijankovic.exammanager;
import android.os.Bundle;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
public class ViewMap extends MapActivity {
private MapView mapView;
private MapController mc;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.view_map);
mapView = (MapView) findViewById(R.id.mapview);
mc = mapView.getController();
String coordinates[] = {"31.567615", "74.360962"};
double lat = Double.parseDouble(coordinates[0]);
double lng = Double.parseDouble(coordinates[1]);
GeoPoint p = new GeoPoint(
(int) (lat * 1E6),
(int) (lng * 1E6));
mc.animateTo(p);
mc.setZoom(7);
mapView.invalidate();
}
#Override
protected boolean isRouteDisplayed() {
return false; // we are not displaying route information
}
}
Here is view_map.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/mapview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:apiKey="0jHJ3aWhSfOMZ1gjFMGc8xTf7mASBEtQcynZPOQ"
android:clickable="true"
android:enabled="true" />
</RelativeLayout>
I know my API key works for sure since map displays if I put ViewMap as the main activity.
However if I try to put it in activity that gets called on button clicked, map opens but everything is grey and tiles do not load. There is Google logo in the bottom left corner.
Here is my manifest file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.ijankovic.exammanager"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="10" />
<uses-permission android:name="android.premission.INTERNET" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#android:style/Theme.NoTitleBar" >
<activity
android:name="com.ijankovic.exammanager.MainActivity"
android:configChanges="keyboardHidden|orientation|screenSize"
android:label="#string/app_name"
android:screenOrientation="portrait" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".ViewCourse"
android:label="#string/app_name"></activity>
<activity android:name=".ViewMap"
android:label="#string/app_name"></activity>
<uses-library android:name="com.google.android.maps" />
</application>
</manifest>
Problem:
Is something wrong with my Intent and ViewMap activity call?
Any suggestions are welcome. Thanks in advance!
Edit:
I managed to get google maps with this call:
Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse("google.navigation:q=New+York+NY));
startActivity(i);
However this seems to start google maps app. It is not google maps within my app like the tutorial showed.
Add these 2 permissions in your manifest file Before the Internet Permission:
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
And make sure your API key is correct
Add this permission in your manifest file.
<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" />
In your map activity have you defined the MapController ?
There may be possibility that you might have missed the MapView or MapController
You can use the following code in your mapactivity class
to confirm
private MapView mapView;
private MapController mc;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mapView = (MapView) findViewById(R.id.mapview1);
mc = mapView.getController();
String coordinates[] = {"31.567615", "74.360962"};
double lat = Double.parseDouble(coordinates[0]);
double lng = Double.parseDouble(coordinates[1]);
GeoPoint p = new GeoPoint(
(int) (lat * 1E6),
(int) (lng * 1E6));
mc.animateTo(p);
mc.setZoom(7);
mapView.invalidate();
}
Simply Copy and Paste this class in your project.I have tested in my Demo Project and it worked for me...
Hope it will work for you also .. :)
import com.google.android.maps.MapActivity;
import android.os.Bundle;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
public class MyMapActivity extends MapActivity
{
private MapView mapView;
private MapController mc;
private GeoPoint geoPoint;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.view_map);
mapView = (MapView) findViewById(R.id.mapview);
mc = mapView.getController();
mc.setZoom(16);
geoPoint = new GeoPoint((int)(31.567615 * 1E6),(int)(74.360962 * 1E6));
mc= mapView.getController();
mapView.setBuiltInZoomControls(true);
mapView.setStreetView(true);
mc.animateTo(geoPoint);
}
#Override
protected boolean isRouteDisplayed()
{
return false;
}
}
I figured out what the problem was!
What I had:
<uses-permission android:name="android.premission.INTERNET" />
What it should be:
<uses-permission android:name="android.permission.INTERNET" />
So I typed premission instead of permission.
I'm surprised that eclipse does not point syntax errors in AndroidManifest.xml file.
Related
I want to display google map in Android ,i have map API even map is not showing and also not throwing any error.
I have use following
Java code
public class GoogleMapActivity extends MapActivity {
private Location myLocation;
protected MapView myMapView = null;
protected LocationManager myLocationManager = null;
protected MapController mapController;
List<Overlay> mapOverlays;
protected boolean isRouteDisplayed() {
return false;
}
protected void onCreate(Bundle icicle) {
super.onCreate(icicle);
this.myMapView = new MapView(this, "0jzIB6m5R1kLa_rGte-DS9PhF3KlSgqYHUZognA");
this.setContentView(myMapView);
myMapView.setBuiltInZoomControls(true);
myMapView.setSatellite(true);
mapController = myMapView.getController();
mapOverlays = myMapView.getOverlays();
this.myLocation = new Location("gps");
this.myLocation.setLongitude(77.52436144125092);
this.myLocation.setLatitude(13.05096452223662);
updateView();
}
private void updateView() {
Double lat = myLocation.getLatitude();
Double lng = myLocation.getLongitude();
GeoPoint point = new GeoPoint(lat.intValue(), lng.intValue());
mapController.setCenter(point);
}
}
Manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.googlemap"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_GPS" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:name=".GoogleMapActivity"
android:label="#string/app_name" >
<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" />
</application>
</manifest>
Finally i get output blank. please have a look at below screen
on please suggest me where i'm wrong
I think you have not added myMapView in current layout ...........
you created in java code
this.myMapView = new MapView(this,
"0jzIB6m5R1kLa_rGte-DS9PhF3KlSgqYHUZognA");
but looks not added in any layout.. as per link
If you dynamically create the MapView via its constructor, you can
supply the API key that way from Java code, but then you will need to
dynamically add it to your layout.
You cannot both have the widget in your layout and set the API key
in Java.
Solutions :
option 1 - Create a Linear layout in for XML and add the myMapView
in that.
option 2 -Create Map view in XML it self and get that by
findViewById.
Try placing the mapview on the layout xml with its api key there as well and then on the activity just get a reference of the map and continue normally.
main.xml with mapView:
<com.google.android.maps.MapView android:id="#+id/mapView"
android:layout_width="fill_parent" android:layout_height="fill_parent" android:enabled="true"
android:layout_centerInParent="true" android:clickable="true" android:apiKey="YOUR MAP KEY"/>
Activity:
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mapView = (MapView)findViewById(R.id.mapView);
Below code which has working fine for me -
public class MapTabView extends MapActivity
{
MapView map;
String api = "02gY92RWvQgV-k5CUQvGPowJkw8HkN4Tmm-HDIQ";
#Override
protected void onCreate(Bundle icicle)
{
super.onCreate(icicle);
map = new MapView(this, api);
map.setClickable(true);
map.setEnabled(true);
setContentView(map);
}
#Override
protected boolean isRouteDisplayed() {
return false;
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem menu_item)
{
switch (menu_item.getItemId()) {
case R.id.satelite:
map.setSatellite(true);
map.setTraffic(false);
break;
case R.id.traffic:
map.setTraffic(true);
map.setSatellite(false);
break;
default:
break;
}
return true;
}
}
Have a look at this TabMapsExample
I am creating an app that uses the google API. I would like to know if i install that app on a mobile phone, will it require a gooogle map app pre-installed in the device to be able to use the map or it uses the internet to get the map from google or any other methods?
This my main.java:
package com.map.testing;
import android.os.Bundle;
import com.google.android.maps.GeoPoint; import
com.google.android.maps.MapActivity; import
com.google.android.maps.MapController; import
com.google.android.maps.MapView;
public class main extends MapActivity {
/** Called when the activity is first created. */
MapController mControl;
GeoPoint GeoP;
MapView mapV; #Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mapV = (MapView) findViewById(R.id.mapView);
mapV.displayZoomControls(true);
mapV.setBuiltInZoomControls(true);
double lat = 23.1;
double longi = 113.3;
GeoP = new GeoPoint((int)(lat*1E6), (int)(longi*1E6));
mControl = mapV.getController();
mControl.animateTo(GeoP);
mControl.setZoom(12);
}
#Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
}
This my main.xml:
<com.google.android.maps.MapView
android:id="#+id/mapView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:enabled="true"
android:clickable="true"
android:apiKey="00_sJY0o9guZejQwsJOr8PPDHtMpnEB6LWFspjA"
/>
</LinearLayout>
my manifest:
<uses-sdk android:minSdkVersion="8" />
<uses-permission android:name="android.permission.INTERNET" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<uses-library android:name="com.google.android.maps" />
<activity
android:name=".main"
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>
But you can handle id the api is not on phone by
android:required="false" in
How to detect an android device whether it supports google maps API
I implemented the app that displays the map. and it shows the particular location using latitude and longitude. but it shows an empty map.how to see the map. please can anybody help me.
code
public class mapsdemo extends MapActivity
{
MapView mapView;
GeoPoint gp;
MapController mc;
/** Called when the activity is first created. */
#SuppressWarnings({ "unchecked", "rawtypes" })
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mapView = (MapView) findViewById(R.id.mapView);
mapView.setBuiltInZoomControls(true);
mapView.displayZoomControls(true);
List mapOverlays = mapView.getOverlays();
Drawable drawable = this.getResources().getDrawable(R.drawable.icon);
HelloItemizedOverlay itemizedOverlay = new HelloItemizedOverlay(drawable, this);
double lat = 17.385044;
double longi = 78.486671;
gp = new GeoPoint((int) (lat *1E6),(int) (longi *1E6));
OverlayItem overlayitem = new OverlayItem(gp, "Hello", "I'm in Hyd");
itemizedOverlay.addOverlay(overlayitem);
mapOverlays.add(itemizedOverlay);
mc = mapView.getController();
mc.animateTo(gp);
mc.setZoom(13);
}
#Override
protected boolean isRouteDisplayed() {
return false;
}
}
main.xml
< ?xml version="1.0" encoding="utf-8"?>
< LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
< com.google.android.maps.MapView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:apiKey="00YCOT71Vu0btHPPlbIR9uvF0-l4mcAVT9_6HMw"
android:id="#+id/mapView"
android:enabled="true"
android:clickable="true"
/>
< /LinearLayout>
manifeast
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.maps.demo"
android:versionCode="1"
android:versionName="1.0">
< uses-sdk android:minSdkVersion="7" />
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<application android:icon="#drawable/icon" android:label="#string/app_name">
<activity android:name=".mapsdemo"
android:label="#string/app_name"
android:theme="#android:style/Theme.NoTitleBar">
<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" />
</application>
</manifest>
This is most likely caused by a signature mismatch. Make sure that you registered your private key from the keystore used to package your application with Google at the maps API signup page and ensure the key that is issued is in your layout file for the MapView.
I have an activity which extends MapActivity. After running the activity, the Google Map should be shown on the emulator.
How can I do that?
http://www.vogella.de/articles/AndroidLocationAPI/article.html#overview_maps
This link got what u need.
i did it. and it works for me as well..
Just move to the 4th topic from the Index. (Google Maps)
Thanks
UPDATED:
Menifest File:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="de.vogella.android.locationapi.maps" android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="10" />
<application android:icon="#drawable/icon" android:label="#string/app_name">
<activity android:name=".ShowMap" android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<uses-library android:required="true" android:name="com.google.android.maps"></uses-library>
</application>
XML LayoutFile:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/mainlayout"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<com.google.android.maps.MapView
android:id="#+id/mapview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:clickable="true"
android:apiKey="Your Maps API Key"
/>
</RelativeLayout>
ACtivity:
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
public class ShowMap extends MapActivity {
private MapController mapController;
private MapView mapView;
private LocationManager locationManager;
public void onCreate(Bundle bundle) {
super.onCreate(bundle);
setContentView(R.layout.main); // bind the layout to the activity
// create a map view
RelativeLayout linearLayout = (RelativeLayout) findViewById(R.id.mainlayout);
mapView = (MapView) findViewById(R.id.mapview);
mapView.setBuiltInZoomControls(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());
}
#Override
protected boolean isRouteDisplayed() {
return false;
}
public class GeoUpdateHandler implements LocationListener {
#Override
public void onLocationChanged(Location location) {
int lat = (int) (location.getLatitude() * 1E6);
int lng = (int) (location.getLongitude() * 1E6);
GeoPoint point = new GeoPoint(lat, lng);
mapController.animateTo(point); // mapController.setCenter(point);
}
#Override
public void onProviderDisabled(String provider) {
}
#Override
public void onProviderEnabled(String provider) {
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
}
}
for starting mapview then follow this mapview's link
You have to signup here and then insert the code provided in your layout like this:
<com.google.android.maps.MapView
android:id="#+id/map_map"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:apiKey="***************************************"
/>
You can refer this link, http://developer.android.com/resources/tutorials/views/hello-mapview.html
Check if you have provided your Map API key properly. This link has helped me to display the map with my present location.
I have one google maps application it does not show the maps it only shows the lines
It does not show the mapview,and i also have the internet Connection
And it also genrate the following error
09-30 12:01:12.934: WARN/Resources(587): Converting to string:
TypedValue{t=0x12/d=0x0 a=2 r=0x7f050002}
09-30 12:01:13.094: WARN/GpsLocationProvider(59): Duplicate add listener for uid
10042
09-30 12:01:13.334: INFO/MapActivity(587): Handling network change
notification:CONNECTED
09-30 12:01:13.334: ERROR/MapActivity(587): Couldn't get connection factory client
Showmap.java
package de.vogella.android.locationapi.maps;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.RelativeLayout;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
public class ShowMap extends MapActivity {
private MapController mapController;
private MapView mapView;
private LocationManager locationManager;
public void onCreate(Bundle bundle) {
super.onCreate(bundle);
setContentView(R.layout.main); // bind the layout to the activity
// create a map view
RelativeLayout linearLayout = (RelativeLayout) findViewById(R.id.mainlayout);
mapView = (MapView) findViewById(R.id.mapview);
mapView.setBuiltInZoomControls(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, 13,
14, new GeoUpdateHandler());
}
#Override
protected boolean isRouteDisplayed() {
return false;
}
public class GeoUpdateHandler implements LocationListener {
#Override
public void onLocationChanged(Location location) {
int lat = (int) (location.getLatitude() * 1E6);
int lng = (int) (location.getLongitude() * 1E6);
GeoPoint point = new GeoPoint(lat, lng);
mapController.animateTo(point); // mapController.setCenter(point);
}
#Override
public void onProviderDisabled(String provider) {
}
#Override
public void onProviderEnabled(String provider) {
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
}
}
This is My mainfest
?xml version="1.0" encoding="utf-8"?>
<application android:icon="#drawable/icon" android:label="#string/app_name">
<uses-library android:name="com.google.android.maps" />
<activity android:name=".ShowMap"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<uses-library android:required="true" android:name="com.google.android.maps"></uses-library>
And main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/mainlayout"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<com.google.android.maps.MapView
android:id="#+id/mapview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:enabled="true"
android:clickable="true"
android:apiKey="0cHF-o_cvW1DEz3ocWAyybGqUsg8aUlwKMoyr4A"
/>
Do you have the required permissions in place in the AndroidManifest.xml? In an Android app that I've made for GPS + Maps I have the following:
<uses-permission android:name="android.permission.ACCESS_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_GPS" />
This is a problem with an API-Key.
Here you can do it.
You need to generate different one for each emulator, device etc. and for final release, and you need to merge them with an keystore you're using, what sux, but there is no different way :/.
Good luck!