Thursday, 15 August 2013

How do I center my Google Map Android application to provided coordinates?

How do I center my Google Map Android application to provided coordinates?

I need to find out how can I move my android googlemap programmatically. I
got the map working, but now i need to add the functionality of centering
the map to specific coordinates given to me by the google geocoder
httprequest. I got the coordinates out of the response from google, but I
have not found any way to actually move my map to said point.
I'm mostly getting in trouble with the reference assemblies. I'm using
Xamarin C# and it seems that all the tutorials and examples are in Java
and the calls for stuff are totally different and need stuff that are not
here in C#.
How do I get my map to move to a desired location on the map?
And how do I place a marker on that point?
Also please add the names of any necessary reference assemblies on your
answer so I'll know what to add/use.
Thank you
..
Here's my relevant code:
MapsActivity.cs
public class MapsActivity : FragmentActivity {
Query q;
int latE6, lonE6;
LatLng coordinates;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView (Resource.Layout.Map);
EditText sText = FindViewById<EditText> (Resource.Id.et_location);
Button fButton = FindViewById<Button> (Resource.Id.btn_find);
fButton.Click += (sender, e) => {
q = new Query(sText.Text);
q.CreateQuery();
var result = q.ResponseFromGoogle.results.Find (item =>
item.geometry.location.lat.ToString("F") != null);
sText.Text = result.geometry.location.lat.ToString("F") + " "
+ result.geometry.location.lat.ToString("F");
coordinates = new LatLng(result.geometry.location.lat,
result.geometry.location.lng);
};
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
android:versionCode="1" android:versionName="1.0"
package="IgluHarkka2.IgluHarkka2">
<uses-sdk android:minSdkVersion="7" />
<application android:label="@string/app_name">
<uses-library android:name="com.google.android.maps" />
<!-- Put your Google Maps V2 API Key here. This key will not work for
you.-->
<!-- See
https://developers.google.com/maps/documentation/android/start#obtaining_an_api_key
-->
<meta-data android:name="com.google.android.maps.v2.API_KEY"
android:value="XXXXXXXXXX" />
</application>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<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-permission
android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-feature android:glEsVersion="0x00020000" android:required="true" />
Map.xml
<?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"
tools:context=".MainActivity">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/btn_find"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/str_btn_find"
android:layout_alignParentRight="true" />
<EditText
android:id="@+id/et_location"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="text"
android:hint="@string/hnt_et_location"
android:layout_toLeftOf="@id/btn_find" />
</RelativeLayout>
<fragment
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment" />

No comments:

Post a Comment