Issue
I'm working on an application which allows users to select a specific location on the map and save it's coordinates. I'm using a MapsActivity like this:
package com.example.getfamiliarwiththemap;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.Toast;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapView;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
import static com.example.getfamiliarwiththemap.R.id.map;
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
private GoogleMap mMap;
private Button ok;
static final LatLng HAMBURG = new LatLng(53.558, 9.927);
static final LatLng KIEL = new LatLng(53.551, 9.993);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(map);
mapFragment.getMapAsync(this);
}
/**
* Manipulates the map once available.
* This callback is triggered when the map is ready to be used.
* This is where we can add markers or lines, add listeners or move the camera. In this case,
* we just add a marker near Sydney, Australia.
* If Google Play services is not installed on the device, the user will be prompted to install
* it inside the SupportMapFragment. This method will only be triggered once the user has
* installed Google Play services and returned to the app.
*/
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
// Create pin
ImageView pin = new ImageView(MapsActivity.this);
pin.setImageResource(R.drawable.common_google_signin_btn_icon_light);
// Position pin
FrameLayout.LayoutParams parameters = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT, Gravity.CENTER);
float density = getResources().getDisplayMetrics().density;
parameters.bottomMargin = (int) (12 * density);
pin.setLayoutParams( parameters);
MapView mapView = (MapView) findViewById(R.id.map) ;
mapView.addView( pin);
ok = (Button) findViewById(R.id.ok);
ok.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
if (mMap != null){
Marker hamburg = mMap.addMarker(new MarkerOptions().position(HAMBURG)
.title("Hamburg"));
Marker kiel = mMap.addMarker(new MarkerOptions()
.position(KIEL)
.title("Kiel")
.snippet("Kiel is cool"));
mMap.setOnMarkerClickListener(new GoogleMap.OnMarkerClickListener() {
@Override
public boolean onMarkerClick(Marker marker) {
Toast.makeText(MapsActivity.this,"You clicked a marker named " + marker.getTitle().toString(),Toast.LENGTH_SHORT).show();
return false;
}
});
// Move the camera instantly to hamburg with a zoom of 15.
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(HAMBURG, 15));
// Zoom in, animating the camera.
mMap.animateCamera(CameraUpdateFactory.zoomTo(10), 2000, null);
}
}
}
But this line is obviously not working:
MapView mapView = (MapView) findViewById(R.id.map) ;
How should i cast a mapFragment to a Mapview to be able to add views? I'm pretty new to android so any simple answer would be appreciated.
****Solved**** The final XML:
P.S: My XML layout code:
<com.google.android.gms.maps.MapView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/mapView"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<fragment
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.getfamiliarwiththemap.MapsActivity" >
<LinearLayout
android:id="@+id/mLlayoutBottomButtons"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentBottom="true"
android:orientation="vertical" >
<Button
android:id="@+id/ok"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Select"
android:textColor="#ffec00"
android:background="#b600ff"
android:layout_marginBottom="5dp"
android:layout_marginTop="500dp"
android:layout_marginLeft="150dp"
android:layout_marginRight="150dp"
/>
</LinearLayout>
</fragment>
</com.google.android.gms.maps.MapView>
Solution
Add in your xml layout a MapView like this:
<com.google.android.gms.maps.MapView
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent" />
Answered By - Daniel RL
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.