In the previous post i showed you how to successfully integrate Google maps with your android application. Now after integration what next!? in this post am going to show you how to add a Google map marker on the touched location on the map.
If your are new to Google maps for android then you can follow the previous tutorial Google Maps for Android to set up the map in your application.
If your are new to Google maps for android then you can follow the previous tutorial Google Maps for Android to set up the map in your application.
Now, Fire your eclipse and go to File>New>Android Application Project to create a new android project.
Ensure that you add the google play services to your project asn explained in this post Google maps for android.
Edit your main xml file to look like below
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="wrap_content"
android:layout_height="wrap_content"
class="com.google.android.gms.maps.SupportMapFragment" />
</RelativeLayout>
Copy the code below into your MainActivity.java class, so you should have your main activity as below
MainActivity.java
package com.mojas.googlemapsforandoid;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.GoogleMap.OnMapClickListener;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
public class MainActivity extends FragmentActivity {
GoogleMap mGoogleMap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SupportMapFragment supportMapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mGoogleMap = supportMapFragment.getMap();
mGoogleMap.setOnMapClickListener(new OnMapClickListener() {
@Override
public void onMapClick(LatLng latlng) {
MarkerOptions marker = new MarkerOptions();
marker.position(latlng);
mGoogleMap.clear();
mGoogleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(latlng,14));
mGoogleMap.addMarker(marker);
}
});
;
}
}
Note that com.mojas.googlemapsforandoid is the package name for my project and therefore this should be replaced with your package name which you created the Google API key with. So ensure that your manifest file is edited appropriatetly
Manifestfile.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mojas.googlemapsforandoid"
android:versionCode="1"
android:versionName="1.0" >
<!-- Replace the package name below with your project pacakge name -->
<permission
android:name="com.mojas.googlemapsforandoid.permission.MAPS_RECEIVE"
android:protectionLevel="signature" />
<uses-permission android:name="com.mojas.googlemapsforandoid.permission.MAPS_RECEIVE" />
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<!-- Required to show current location -->
<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/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.mojas.googlemapsforandoid.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>
<!-- Replace the API KEY with your API key obtained form google console -->
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="Your AIPI KEY" />
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
</application>
</manifest>
Run the application and then touch any part of the map and you will see a red marker placed on the location touched as in the screenshot below.
Coding is fun, Enjoy it........
0 comments:
Post a Comment