Android: how to open a Google Map? - android

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.

Related

Need to place an icon on my google map in an android application using GPS coordinates

Thanks in advance, I'm having dificulties trying to display an icon on my current location in a mobile application using google maps. At the moment I'm able to retrieve the maps from Google maps android api v2 libraries using the using API key etc., and I am able to retrieve my own location on GPS coordinates, "latitude" and "longitude" as they can be displayed on the screen in two textViews in my mobile device. My problem now is how to use those coordinates to display with an icon my own location on the map. Obviously I'm doing something wrong when I try to implement it. I tried to implement it using overlays but I'm having plenty of errors. I don't know why or what way to take.
Here is my code. Any help I would really appreciate it.
thanks
This is my MainActivity.java
package com.example.location3;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.widget.TextView;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
public class MainActivity extends FragmentActivity {
TextView textLat;
TextView textLong;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Create reference
textLat = (TextView) findViewById(R.id.textLat);
textLong = (TextView) findViewById(R.id.textLong);
// create locationManager object
LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
// create LocationListener object (listen for changes in the location)
LocationListener ll = new myLocationListener();
// use lm location manager to get update
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, ll);
}
// create a LocationListener to do what we want
class myLocationListener implements LocationListener {
GoogleMap map = ((SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map)).getMap();
// implemented method
#Override
public void onLocationChanged(Location location) {
if (location != null) {
// variables to get and store longitude and latitude
double pLong = location.getLongitude();
double pLat = location.getLatitude();
// set text to display values from longitude and latitude
textLat.setText(Double.toString(pLat));
textLong.setText(Double.toString(pLong));
}
}
// implemented method
#Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
// implemented method
#Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
// implemented method
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
}
}
This is my Activity_main.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=".MainActivity" >
<fragment
android:id="#+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Latitud"
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="#+id/textLat"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/textView1"
android:text=""
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/textLong"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/textView3"
android:text=""
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/textLat"
android:text="Longitud"
android:textAppearance="?android:attr/textAppearanceSmall" />
</RelativeLayout>
And this is my Manifest file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.location3"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<permission
android:name="com.example.location3.permissions.MAPS_RECEIVE"
android:protectionLevel="signature" />
<uses-permission android:name="com.example.location3.permission.MAP_RECEIVE" />
<!-- to be able to use it for mobiles -->
<uses-feature
android:glEsVersion="0x00020000"
android:required="true" />
<!-- Permision to use Internet to get the maps -->
<uses-permission android:name="android.permission.INTERNET" />
<!-- Permision for the cache which is done in the external memory -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<!-- Permision to use google services -->
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<!-- Permision to be able to point in the map where the user is -->
<!-- Permision to use wifi net -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<!-- Permision to use GPS -->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<uses-library
android:required="true"
android:name="com.google.android.maps" />
<activity
android:name="com.example.location3.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>
<!-- API provided from Google -->
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="AIzaSyCx00iNUdhMi9uyuCO9tTPwX4-nbi4wg6wx" />
</application>
</manifest>
you need to create a class which will handle the overlay wherever you want to pinup on the map. refer this tutorial here
try this :
class CurrentLocationOverlay extends ItemizedOverlay<OverlayItem>{
private ArrayList<OverlayItem> mapOverlays = new ArrayList<OverlayItem>();
public CurrentLocationOverlay(Drawable defaultMarker) {
super(boundCenterBottom(defaultMarker));
}
public CurrentLocationOverlay(Drawable defaultMarker , Context context) {
this(boundCenterBottom(defaultMarker));
}
#Override
protected OverlayItem createItem(int i) {
return mapOverlays.get(i);
}
#Override
public int size() {
return mapOverlays.size();
}
public void addOverlay(GeoPoint gp , String title , String msg) {
OverlayItem overlay = new OverlayItem(gp, title, msg);
mapOverlays.add(overlay);
this.populate();
}
}
add icon :
Drawable mDrawable = getResources().getDrawable(R.drawable.pinpoint);
GeoPoint point = new GeoPoint((int)(lat* 1E6) , (int)(lng* 1E6));
CurrentLocationOverlay currentLocationOverlay = new CurrentLocationOverlay(mDrawable);
currentLocationOverlay.addOverlay(point ,"" , "");
mapView.getOverlays().add(currentLocationOverlay);

Google Maps empty tiles if not in the Main Activity

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.

Android map not displaying

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

android- How to see the map using maps lib

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.

Android google maps showing greed lines

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!

Categories

Resources